Skip to content

Commit

Permalink
Merge pull request #18 from kool-dev/compression
Browse files Browse the repository at this point in the history
Add compression by default
  • Loading branch information
b-lopes authored Jun 7, 2022
2 parents 7e1f01f + a4612f1 commit b8f2acf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM kooldev/puppeteer:latest
FROM kooldev/puppeteer:1.0

WORKDIR /app
COPY --chown=pptruser:pptruser . /app
Expand Down
26 changes: 24 additions & 2 deletions pdf-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const crypto = require('crypto');
const app = express();
const bodyParser = require('body-parser');
const compression = require('compression');
const { exec } = require('child_process');

app.use(compression());
app.use(bodyParser.urlencoded({
Expand Down Expand Up @@ -45,6 +46,8 @@ app.post('/from-html', async (req, res) => {
calls.fromHtml++;

const html = req.body.html;
const disableCompression = req.body.disableCompression || false;
const compressionResolution = req.body.compressionResolution || 100;
const options = getPdfOptions(req.body.options);
const htmlFile = generateFileName() + '.html';
const fullHtmlPath = path.join(storagePath, htmlFile);
Expand All @@ -54,15 +57,34 @@ app.post('/from-html', async (req, res) => {
let pdfFilePath;
try {
pdfFilePath = await generatePdf(`file://${fullHtmlPath}`, req.query.media, options);

fs.unlinkSync(fullHtmlPath);
} catch (err) {
log('/from-html: error generating PDF', e);
deliverJson(res, {msg: 'failure generating PDF', err}, 500);

return;
}

deliverPdfFile(res, pdfFilePath);
fs.unlinkSync(fullHtmlPath);
const compressedFilePath = `${pdfFilePath}_compressed.pdf`;

if (disableCompression) {
deliverPdfFile(res, pdfFilePath);
} else {
// compress PDF outoput
const cmd = `shinkpdf ${pdfFilePath} ${compressedFilePath} ${compressionResolution}`;
exec(cmd, (err, stdout, stderr) => {
if (err) {
console.log('Compressing - err:', err);
}
if (stdout || stderr) {
console.log('Compressing - out/err:', stdout, stderr);
}

deliverPdfFile(res, err ? pdfFilePath : compressedFilePath);
fs.unlinkSync(pdfFilePath);
});
}
});

app.get('/from-url', async (req, res) => {
Expand Down

0 comments on commit b8f2acf

Please sign in to comment.