From 6bab3da52647b34e8d7aaab5d3a680dd1375bebc Mon Sep 17 00:00:00 2001 From: daniel-j Date: Sun, 21 Jul 2024 17:18:31 +0200 Subject: [PATCH] Integrate pdfCropMargins for cropping PDFs (#29) --- Dockerfile | 6 ++++++ index.js | 52 +++++++++++++++++++++++++++++++++++++++++++++- static/upload.html | 3 ++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5c67c33..c16ac37 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,12 @@ RUN wget https://web.archive.org/web/20150803131026if_/https://kindlegen.s3.amaz chmod +x /usr/local/bin/kindlegen && \ rm -rf kindlegen +RUN apk add --no-cache pipx + +ENV PATH="$PATH:/root/.local/bin" + +RUN pipx install pdfCropMargins + # Copy files needed by npm install COPY package*.json ./ diff --git a/index.js b/index.js index 7901275..ebca70b 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,8 @@ const serve = require('koa-static') const { mkdirp } = require('mkdirp') const fs = require('fs') const { spawn } = require('child_process') -const { extname, basename, dirname } = require('path') +const { join, extname, basename, dirname } = require('path') +const resolvepath = require('path').resolve const FileType = require('file-type') const { transliterate } = require('transliteration') const sanitize = require('sanitize-filename') @@ -418,6 +419,55 @@ router.post('/upload', async (ctx, next) => { }) return } + + } else if (mimetype == 'application/pdf' && ctx.request.body.pdfcropmargins) { + const dir = dirname(ctx.request.file.path) + const base = basename(ctx.request.file.path, '.pdf') + const outfile = resolvepath(join(dir, `${base}_cropped.pdf`)) + let p = new Promise((resolve, reject) => { + let stderr = '' + const pdfcropmargins = spawn('pdfcropmargins', ['-s', '-u', '-o', outfile, basename(ctx.request.file.path)], { + // stdio: 'inherit', + cwd: dirname(ctx.request.file.path) + }) + pdfcropmargins.once('error', function (err) { + fs.unlink(ctx.request.file.path, (err) => { + if (err) console.error(err) + else console.log('Removed file', ctx.request.file.path) + }) + reject('pdfcropmargins error: ' + err) + }) + pdfcropmargins.once('close', (code) => { + fs.unlink(ctx.request.file.path, (err) => { + if (err) console.error(err) + else console.log('Removed file', ctx.request.file.path) + }) + if (code !== 0) { + reject('pdfcropmargins error code: ' + code + '\n' + stderr) + return + } + + resolve(outfile) + }) + pdfcropmargins.stdout.on('data', function (str) { + stderr += str + console.log('pdfcropmargins: ' + str) + }) + pdfcropmargins.stderr.on('data', function (str) { + stderr += str + console.log('pdfcropmargins: ' + str) + }) + }) + try { + data = await p + } catch (err) { + flash(ctx, { + success: false, + message: err.replaceAll(basename(ctx.request.file.path), "infile.pdf").replaceAll(outfile, "outfile.pdf") + }) + return + } + } else { // No conversion data = ctx.request.file.path diff --git a/static/upload.html b/static/upload.html index e1f6589..07f5f5d 100644 --- a/static/upload.html +++ b/static/upload.html @@ -18,6 +18,7 @@ + @@ -31,7 +32,7 @@
- Created by djazz. Powered by Koa, Kepubify and KindleGen
+ Created by djazz. Powered by Koa, Kepubify, KindleGen and pdfCropMargins
Source code on Github - https://send.djazz.se