@@ -6,6 +6,7 @@ const crypto = require('crypto');
66const app = express ( ) ;
77const bodyParser = require ( 'body-parser' ) ;
88const compression = require ( 'compression' ) ;
9+ const { exec } = require ( 'child_process' ) ;
910
1011app . use ( compression ( ) ) ;
1112app . use ( bodyParser . urlencoded ( {
@@ -45,6 +46,8 @@ app.post('/from-html', async (req, res) => {
4546 calls . fromHtml ++ ;
4647
4748 const html = req . body . html ;
49+ const disableCompression = req . body . disableCompression || false ;
50+ const compressionResolution = req . body . compressionResolution || 100 ;
4851 const options = getPdfOptions ( req . body . options ) ;
4952 const htmlFile = generateFileName ( ) + '.html' ;
5053 const fullHtmlPath = path . join ( storagePath , htmlFile ) ;
@@ -54,15 +57,34 @@ app.post('/from-html', async (req, res) => {
5457 let pdfFilePath ;
5558 try {
5659 pdfFilePath = await generatePdf ( `file://${ fullHtmlPath } ` , req . query . media , options ) ;
60+
61+ fs . unlinkSync ( fullHtmlPath ) ;
5762 } catch ( err ) {
5863 log ( '/from-html: error generating PDF' , e ) ;
5964 deliverJson ( res , { msg : 'failure generating PDF' , err} , 500 ) ;
6065
6166 return ;
6267 }
6368
64- deliverPdfFile ( res , pdfFilePath ) ;
65- fs . unlinkSync ( fullHtmlPath ) ;
69+ const compressedFilePath = `${ pdfFilePath } _compressed.pdf` ;
70+
71+ if ( disableCompression ) {
72+ deliverPdfFile ( res , pdfFilePath ) ;
73+ } else {
74+ // compress PDF outoput
75+ const cmd = `shinkpdf ${ pdfFilePath } ${ compressedFilePath } ${ compressionResolution } ` ;
76+ exec ( cmd , ( err , stdout , stderr ) => {
77+ if ( err ) {
78+ console . log ( 'Compressing - err:' , err ) ;
79+ }
80+ if ( stdout || stderr ) {
81+ console . log ( 'Compressing - out/err:' , stdout , stderr ) ;
82+ }
83+
84+ deliverPdfFile ( res , err ? pdfFilePath : compressedFilePath ) ;
85+ fs . unlinkSync ( pdfFilePath ) ;
86+ } ) ;
87+ }
6688} ) ;
6789
6890app . get ( '/from-url' , async ( req , res ) => {
0 commit comments