Skip to content

Commit 9af71ca

Browse files
committed
Upgrade Shrink + Fix Delete File
1 parent 5b2c18e commit 9af71ca

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

pdf-service.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,22 @@ app.post('/from-html', async (req, res) => {
7272
deliverPdfFile(res, pdfFilePath);
7373
} else {
7474
// compress PDF outoput
75-
const cmd = `shinkpdf ${pdfFilePath} ${compressedFilePath} ${compressionResolution}`;
75+
const cmd = `shinkpdf -o ${compressedFilePath} -r ${compressionResolution} ${pdfFilePath}`;
76+
7677
exec(cmd, (err, stdout, stderr) => {
7778
if (err) {
7879
console.log('Compressing - err:', err);
7980
}
81+
8082
if (stdout || stderr) {
8183
console.log('Compressing - out/err:', stdout, stderr);
8284
}
8385

8486
deliverPdfFile(res, err ? pdfFilePath : compressedFilePath);
85-
fs.unlinkSync(pdfFilePath);
87+
88+
if (! err) {
89+
fs.unlinkSync(pdfFilePath);
90+
}
8691
});
8792
}
8893
});
@@ -160,7 +165,16 @@ async function generatePdf(url, media, options = {}) {
160165
let filename = generateFileName() + '.pdf';
161166
const pdfFilePath = path.join(storagePath, filename);
162167

163-
log('generatePdf: generate PDF', pdfFilePath);
168+
options = {
169+
path: pdfFilePath,
170+
scale: parseFloat(1),
171+
format: 'A4',
172+
printBackground: true,
173+
...options
174+
};
175+
176+
log('generatePdf: generate PDF', pdfFilePath, options);
177+
164178
await page.pdf({
165179
path: pdfFilePath,
166180
scale: parseFloat(1),
@@ -178,10 +192,10 @@ function generateFileName() {
178192
return crypto.randomBytes(20).toString('hex')
179193
}
180194

181-
function log(message) {
195+
function log(...messages) {
182196
let date = new Date().toISOString().replace('T', ' ').substr(0, 19)
183197

184-
console.log(`[${date}] ${message}`);
198+
console.log(`[${date}]`, ...messages);
185199
}
186200

187201
function getPdfOptions(options) {

0 commit comments

Comments
 (0)