| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- const fs = require('fs')
- const asfs = require('asfs')
- const express = require('express')
- const multer = require('multer')
- const childProcess = require('child_process')
- const bodyParser = require('body-parser')
- const pdf2htmlexPath = `${__dirname}/node_modules/@alancnet/pdf2htmlex/bin-win/pdf2htmlEX.exe`
- const phantom = require('phantom')
- const uuid = require('uuid')
- const parseDataUrl = require('data-urls')
- const cors = require('cors')
- const upload = multer({
- dest: 'temp/'
- })
- const app = express()
- app.use(cors({
- origin: '*'
- }))
- app.use(bodyParser.urlencoded({extended: false, limit: '100mb'}))
- app.use(express.static('./public'))
- app.post('/edit', upload.single('document'), async (req, res) => {
- let pdfFile, htmlFile
- if (req.file) {
- pdfFile = req.file.path
- htmlFile = `${req.file.path}.html`
- } else if (req.body.url) {
- pdfFile = `temp/${uuid()}.pdf`
- htmlFile = `${pdfFile}.html`
- const pdf = parseDataUrl(req.body.url)
- await asfs.writeFileAsync(pdfFile, pdf.body)
- }
- /*
- Executes pdf2htmlex[.exe] with:
- - 200 horizontal and vertical DPI
- - TrueType Font format (because woff does not render in PhantomJS)
- - No DRM, overriding any PDF settings forbidding copying or modifying
- - The path to the source PDF file
- - The path to the output HTML file
- */
- childProcess.exec(`"${pdf2htmlexPath}" --hdpi 200 --vdpi 200 --font-format ttf --no-drm 1 "${pdfFile}" "${htmlFile}"`, (err, stdout, stderr) => {
- if (err) {
- res.status(500).send(`<pre>${err}\n\n${stdout}\n\n${stderr}</pre>`)
- } else {
- fs.readFile('public/edit.html', (err, editHtml) => {
- fs.readFile(htmlFile, 'utf8', (err, data) => {
- if (err) {
- res.status(500).send(`<pre>${err}\n\n${stdout}\n\n${stderr}</pre>`)
- } else {
- res.status(200).send(data.replace('</body>', `${editHtml}</body>`))
- }
- fs.unlink(pdfFile)
- fs.unlink(htmlFile)
- })
- })
- }
- })
- })
- app.post('/save', (req, res) => {
- console.log(req.body.html)
- const tmpUuid = uuid()
- const htmlPath = `temp/${tmpUuid}.html`
- const pdfPath = `temp/${tmpUuid}.pdf`
- const pageWidth = /\.w0{width:([\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'
- })
- 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)
- }
- })
- })
- })
- app.listen(process.env.PORT || 3000)
|