Integrate pdfCropMargins for cropping PDFs (#29)
This commit is contained in:
@@ -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 ./
|
||||
|
||||
|
||||
52
index.js
52
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
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
<tr><td class="right"><label for="urlinput"><strong>Send url</strong></label></td><td><input type="url" name="url" id="urlinput" autocomplete="off" style="width: 100%"></td></tr>
|
||||
<tr><td class="right"><input type="checkbox" name="kepubify" id="kepubify" checked /></td><td><label for="kepubify"><strong>Kepubify</strong> <em>Kobo only</em></label></td></tr>
|
||||
<tr><td class="right"><input type="checkbox" name="kindlegen" id="kindlegen" checked /></td><td><label for="kindlegen"><strong>KindleGen</strong> <em>Kindle only</em></label></td></tr>
|
||||
<tr><td class="right"><input type="checkbox" name="pdfcropmargins" id="pdfcropmargins" /></td><td><label for="pdfcropmargins"><strong>Crop margins of pdfs</strong></label></td></tr>
|
||||
<tr><td class="right"><input type="checkbox" name="transliteration" id="transliteration" /></td><td><label for="transliteration"><strong>Transliteration of filename</strong></label></td></tr>
|
||||
<tr class="center"><td colspan="2"><input type="submit" value="Upload and send" /></td></tr>
|
||||
</table>
|
||||
@@ -31,7 +32,7 @@
|
||||
</div>
|
||||
<hr/>
|
||||
<div class="center">
|
||||
Created by djazz. Powered by <a href="https://koajs.com/" target="_blank">Koa</a>, <a href="https://pgaskin.net/kepubify/" target="_blank">Kepubify</a> and <a href="https://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000765211" target="_blank">KindleGen</a><br/>
|
||||
Created by djazz. Powered by <a href="https://koajs.com/" target="_blank">Koa</a>, <a href="https://pgaskin.net/kepubify/" target="_blank">Kepubify</a>, <a href="https://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000765211" target="_blank">KindleGen</a> and <a href="https://github.com/abarker/pdfCropMargins" target="_blank">pdfCropMargins</a><br/>
|
||||
Source code on <a href="https://github.com/daniel-j/send2ereader" target="_blank">Github</a> - <a id="siteurl">https://send.djazz.se</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user