@@ -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,14 +165,17 @@ 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 ) ;
164- await page . pdf ( {
168+ options = {
165169 path : pdfFilePath ,
166170 scale : parseFloat ( 1 ) ,
167171 format : 'A4' ,
168172 printBackground : true ,
169173 ...options
170- } ) ;
174+ } ;
175+
176+ log ( 'generatePdf: generate PDF' , pdfFilePath , options ) ;
177+
178+ await page . pdf ( options ) ;
171179
172180 page . close ( ) ;
173181
@@ -178,10 +186,10 @@ function generateFileName() {
178186 return crypto . randomBytes ( 20 ) . toString ( 'hex' )
179187}
180188
181- function log ( message ) {
189+ function log ( ... messages ) {
182190 let date = new Date ( ) . toISOString ( ) . replace ( 'T' , ' ' ) . substr ( 0 , 19 )
183191
184- console . log ( `[${ date } ] ${ message } ` ) ;
192+ console . log ( `[${ date } ]` , ... messages ) ;
185193}
186194
187195function getPdfOptions ( options ) {
0 commit comments