dwm8
dwm8

Reputation: 307

Any Way to Reduce File Size Using wkhtmltopdf?

I am using wkhtml in OS X to convert an HTML file to a PDF. I am able to do this successfully using the code

/usr/local/bin/wkhtmltopdf sample.com --zoom 0.65 /Users/dwm8/Desktop/sample.pdf

My problem is that the file is rather large, coming in at about 500 KB. Is there any way to shrink the file size? All I am interested is the visual contents of the page (I would be fine taking a screenshot of the website if I didn't have to do it manually), and I don't care about all of the hyperlinks or other website features showing up in my PDF.

Upvotes: 1

Views: 8658

Answers (3)

Peter Palvolgyi
Peter Palvolgyi

Reputation: 11

--lowquality

This param reduced my PDF from 2.6MB to just 102kb without (almost) any quality loss. If you have the newest version (0.12.6 or higher), then you likely won't have any filesize issues. I had to downgrade to 0.12.2.1 because of problems with transparent PNG's showing up as grayscale.

Upvotes: 0

Paul Gregory
Paul Gregory

Reputation: 1753

The flag that works best for me in reducing file size is

--image-dpi 300

As the default DPI is 600, this can reduce the filesize of an image-heavy PDF by 75% and is still good enough for most purposes.

There are two other flags you can use:

--lowquality - supposedly makes it smaller

--image-quality 80 - jpeg compression, default is 94

but overall I find the DPI makes the biggest difference, and if you aren't printing to 600dpi or zooming then you're not really losing anything.

For your use case also try

--no-outline - as you likely don't need a contents section

and the self-explanatory

--disable-internal-links

--disable-external-links

but these are likely to be smaller wins.

Upvotes: 8

Tom Folk
Tom Folk

Reputation: 133

Well if you are just after an image, you could instead use wkhtmltoimage. This may end up with a reduced filesize, not sure though.

Also a couple of other solutions to generating images from pages listed here:

Website screenshots using PHP

Upvotes: 0

Related Questions