| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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 puppeteer = require('puppeteer')
- const uuid = require('uuid')
- const parseDataUrl = require('data-urls')
- const cors = require('cors')
- const path = require('path')
- const chromePromise = puppeteer.launch()
- 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>${stderr}</pre>`)
- } else {
- fs.readFile('public/edit.html', (err, editHtml) => {
- fs.readFile(htmlFile, 'utf8', async (err, data) => {
- if (err) {
- res.status(500).send(`<pre>${err}</pre>`)
- } else {
- res.status(200).send(data.replace('</body>', `${editHtml}</body>`))
- }
- await asfs.unlinkAsync(htmlFile)
- await asfs.unlinkAsync(pdfFile)
- })
- })
- }
- })
- })
- app.post('/save', (req, res) => {
- const tmpUuid = uuid()
- 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 pageHeight = /\.h0{height:([\d\.]*)pt/.exec(req.body.html)[1]
- 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
- })
- 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)
- })
- })
- })
- app.listen(process.env.PORT || 3000)
|