Skip to content

Commit

Permalink
Merge pull request #25 from kool-dev/upgrade_shrink
Browse files Browse the repository at this point in the history
Upgrade Shrink + Fix Delete File
  • Loading branch information
dbpolito committed Jun 20, 2023
2 parents 5b2c18e + c796912 commit 7661570
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pdf-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,22 @@ app.post('/from-html', async (req, res) => {
deliverPdfFile(res, pdfFilePath);
} else {
// compress PDF outoput
const cmd = `shinkpdf ${pdfFilePath} ${compressedFilePath} ${compressionResolution}`;
const cmd = `shinkpdf -o ${compressedFilePath} -r ${compressionResolution} ${pdfFilePath}`;

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);

if (! err) {
fs.unlinkSync(pdfFilePath);
}
});
}
});
Expand Down Expand Up @@ -160,14 +165,17 @@ async function generatePdf(url, media, options = {}) {
let filename = generateFileName() + '.pdf';
const pdfFilePath = path.join(storagePath, filename);

log('generatePdf: generate PDF', pdfFilePath);
await page.pdf({
options = {
path: pdfFilePath,
scale: parseFloat(1),
format: 'A4',
printBackground: true,
...options
});
};

log('generatePdf: generate PDF', pdfFilePath, options);

await page.pdf(options);

page.close();

Expand All @@ -178,10 +186,10 @@ function generateFileName() {
return crypto.randomBytes(20).toString('hex')
}

function log(message) {
function log(...messages) {
let date = new Date().toISOString().replace('T', ' ').substr(0, 19)

console.log(`[${date}] ${message}`);
console.log(`[${date}]`, ...messages);
}

function getPdfOptions(options) {
Expand Down

0 comments on commit 7661570

Please sign in to comment.