marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.56k stars 545 forks source link

a simple pdf is 900KB, after adding custom fonts (calibri) it increase PDF size 10X. #658

Open krupalimakadiya opened 2 years ago

krupalimakadiya commented 2 years ago

when i embedded 'calibri' font in my PDF, Surprisely increase size of PDF 900KB to 3MB, i'dont know why it's happening if you have solution recommended here,

krupalimakadiya commented 2 years ago

After spending lots of time found :roll_eyes: fixes like compress PDF via ghost script using shell.

First npm install shelljs; // (^0.8.5)

then, `const shell = require('shelljs');

const uncompressed_inputFile = '../input.pdf'; // directory path with file name const compressed_outputFile = '../output.pdf' // directory path with file name

shell.exec('gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.2 -dPDFSETTINGS=/prepress -dColorImageResolution=144 -dNOPAUSE -dQUIET -dBATCH -sPAPERSIZE=a4 -sOutputFile=${compressed_outputFile} ${uncompressed_inputFile}'); `

for more options of ghostscript : https://www.ghostscript.com/doc/current/VectorDevices.htm#PDF -> (7.4 PDF file output).

P.S: Using above code i reduced PDF size from 3.0MB to 731KB.