From 30449bddff95d804d215c8f2ef3b2275ae17a793 Mon Sep 17 00:00:00 2001 From: sbkaleci Date: Fri, 21 Jun 2024 20:15:57 +0300 Subject: [PATCH] Integrate pdfCropMargins for cropping PDFs --- index.js | 25 ++++++++++++++++++++++++- static/upload.html | 3 ++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 994e77f..4827394 100644 --- a/index.js +++ b/index.js @@ -10,7 +10,7 @@ const mount = require('koa-mount') 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 FileType = require('file-type') const { transliterate } = require('transliteration') @@ -313,6 +313,29 @@ router.post('/upload', upload.single('file'), async ctx => { resolve(outname) }) }) + } else if (mimetype === 'application/pdf' && ctx.request.body.pdfCropMargins) { + + data = await new Promise((resolve, reject) => { + const pdfcropmargins = spawn('pdfcropmargins', ['-s', '-u', basename(ctx.request.file.path)], { + stdio: 'inherit', + cwd: dirname(ctx.request.file.path) + }) + 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) + return + } + const dir = dirname(ctx.request.file.path); + const base = basename(ctx.request.file.path, '.pdf'); + + resolve(join(dir, `${base}_cropped.pdf`)) + }) + }) + } else { // No conversion data = ctx.request.file.path diff --git a/static/upload.html b/static/upload.html index 9f53880..6743bff 100644 --- a/static/upload.html +++ b/static/upload.html @@ -18,6 +18,7 @@
EPUB, MOBI, PDF,
TXT, CBZ, CBR


+
@@ -30,7 +31,7 @@
- Created by djazz. Powered by Koa, Kepubify and KindleGen
+ Created by djazz. Powered by Koa, Kepubify, pdfCropMargins and KindleGen
Source code on Github - https://send.djazz.se