|
@@ -5,10 +5,13 @@ const multer = require('multer')
|
|
|
const childProcess = require('child_process')
|
|
const childProcess = require('child_process')
|
|
|
const bodyParser = require('body-parser')
|
|
const bodyParser = require('body-parser')
|
|
|
const pdf2htmlexPath = `${__dirname}/node_modules/@alancnet/pdf2htmlex/bin-win/pdf2htmlEX.exe`
|
|
const pdf2htmlexPath = `${__dirname}/node_modules/@alancnet/pdf2htmlex/bin-win/pdf2htmlEX.exe`
|
|
|
-const phantom = require('phantom')
|
|
|
|
|
|
|
+const puppeteer = require('puppeteer')
|
|
|
const uuid = require('uuid')
|
|
const uuid = require('uuid')
|
|
|
const parseDataUrl = require('data-urls')
|
|
const parseDataUrl = require('data-urls')
|
|
|
const cors = require('cors')
|
|
const cors = require('cors')
|
|
|
|
|
+const path = require('path')
|
|
|
|
|
+
|
|
|
|
|
+const chromePromise = puppeteer.launch()
|
|
|
|
|
|
|
|
const upload = multer({
|
|
const upload = multer({
|
|
|
dest: 'temp/'
|
|
dest: 'temp/'
|
|
@@ -42,17 +45,17 @@ app.post('/edit', upload.single('document'), async (req, res) => {
|
|
|
*/
|
|
*/
|
|
|
childProcess.exec(`"${pdf2htmlexPath}" --hdpi 200 --vdpi 200 --font-format ttf --no-drm 1 "${pdfFile}" "${htmlFile}"`, (err, stdout, stderr) => {
|
|
childProcess.exec(`"${pdf2htmlexPath}" --hdpi 200 --vdpi 200 --font-format ttf --no-drm 1 "${pdfFile}" "${htmlFile}"`, (err, stdout, stderr) => {
|
|
|
if (err) {
|
|
if (err) {
|
|
|
- res.status(500).send(`<pre>${err}\n\n${stdout}\n\n${stderr}</pre>`)
|
|
|
|
|
|
|
+ res.status(500).send(`<pre>${stderr}</pre>`)
|
|
|
} else {
|
|
} else {
|
|
|
fs.readFile('public/edit.html', (err, editHtml) => {
|
|
fs.readFile('public/edit.html', (err, editHtml) => {
|
|
|
- fs.readFile(htmlFile, 'utf8', (err, data) => {
|
|
|
|
|
|
|
+ fs.readFile(htmlFile, 'utf8', async (err, data) => {
|
|
|
if (err) {
|
|
if (err) {
|
|
|
- res.status(500).send(`<pre>${err}\n\n${stdout}\n\n${stderr}</pre>`)
|
|
|
|
|
|
|
+ res.status(500).send(`<pre>${err}</pre>`)
|
|
|
} else {
|
|
} else {
|
|
|
res.status(200).send(data.replace('</body>', `${editHtml}</body>`))
|
|
res.status(200).send(data.replace('</body>', `${editHtml}</body>`))
|
|
|
}
|
|
}
|
|
|
- fs.unlink(pdfFile)
|
|
|
|
|
- fs.unlink(htmlFile)
|
|
|
|
|
|
|
+ await asfs.unlinkAsync(htmlFile)
|
|
|
|
|
+ await asfs.unlinkAsync(pdfFile)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
@@ -62,42 +65,31 @@ app.post('/edit', upload.single('document'), async (req, res) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
app.post('/save', (req, res) => {
|
|
app.post('/save', (req, res) => {
|
|
|
- console.log(req.body.html)
|
|
|
|
|
-
|
|
|
|
|
const tmpUuid = uuid()
|
|
const tmpUuid = uuid()
|
|
|
- const htmlPath = `temp/${tmpUuid}.html`
|
|
|
|
|
- const pdfPath = `temp/${tmpUuid}.pdf`
|
|
|
|
|
|
|
+ const htmlFile = `temp/${tmpUuid}.html`
|
|
|
|
|
+ const htmlUrl = `file://${path.join(process.cwd(), htmlFile)}`
|
|
|
|
|
+ const pdfFile = `temp/${tmpUuid}.pdf`
|
|
|
|
|
|
|
|
const pageWidth = /\.w0{width:([\d\.]*)pt/.exec(req.body.html)[1]
|
|
const pageWidth = /\.w0{width:([\d\.]*)pt/.exec(req.body.html)[1]
|
|
|
const pageHeight = /\.h0{height:([\d\.]*)pt/.exec(req.body.html)[1]
|
|
const pageHeight = /\.h0{height:([\d\.]*)pt/.exec(req.body.html)[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
- fs.writeFile(htmlPath, req.body.html, 'utf8', async (err) => {
|
|
|
|
|
- const instance = await phantom.create()
|
|
|
|
|
-
|
|
|
|
|
- const page = await instance.createPage()
|
|
|
|
|
- page.property('paperSize', {
|
|
|
|
|
- width: pageWidth,
|
|
|
|
|
- height: pageHeight,
|
|
|
|
|
- margin: '0px'
|
|
|
|
|
|
|
+ fs.writeFile(htmlFile, req.body.html, 'utf8', async (err) => {
|
|
|
|
|
+ const chrome = await chromePromise
|
|
|
|
|
+ const page = await chrome.newPage()
|
|
|
|
|
+ await page.goto(htmlUrl)
|
|
|
|
|
+ await page.pdf({
|
|
|
|
|
+ path: pdfFile,
|
|
|
|
|
+ preferCSSPageSize: true
|
|
|
})
|
|
})
|
|
|
- page.open(htmlPath)
|
|
|
|
|
- page.on('onLoadFinished', function(status) {
|
|
|
|
|
- if (status === 'success') {
|
|
|
|
|
- page.render(pdfPath, {format: 'pdf'})
|
|
|
|
|
- setTimeout(() => {
|
|
|
|
|
- res.download(pdfPath, 'todo-preserve-filename.pdf', (err) => {
|
|
|
|
|
- if (err) {
|
|
|
|
|
- console.error(err)
|
|
|
|
|
- res.status(500).send(err)
|
|
|
|
|
- }
|
|
|
|
|
- //fs.unlink(htmlPath)
|
|
|
|
|
- //fs.unlink(pdfPath)
|
|
|
|
|
- })
|
|
|
|
|
- }, 5000)
|
|
|
|
|
- } else {
|
|
|
|
|
- res.status(500).send('Failed: ' + status)
|
|
|
|
|
|
|
+ await page.close()
|
|
|
|
|
+ res.download(pdfFile, 'todo-preserve-filename.pdf', async (err) => {
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ console.error(err)
|
|
|
|
|
+ res.status(500).send(err)
|
|
|
}
|
|
}
|
|
|
|
|
+ await asfs.unlinkAsync(htmlFile)
|
|
|
|
|
+ await asfs.unlinkAsync(pdfFile)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|