Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do wkhtmltopdf.exe produce different results on different Windows machines?

Tags:

wkhtmltopdf

I have a simple test.html file:

<html>
<div style="width: 100%; font-size: 28pt;">
This is a test to see what happens when this kind of text gets processed by wkhtmltopdf
</div>
</html>

Which I run through wkhtmltopdf.exe like this:

wkhtmltopdf.exe test.html test.pdf

On one machine I get the following result:

But on another machine I get this result:

It looks like the font size is different?

Why is this happening?

Thanks.

EDIT

I found out what causes this. Windows has a setting for changing the text size:

For some reason the default on one computer is 100% and on the other it is 125%. Are there any way to ignore this in webkit?

like image 350
smerlung Avatar asked Jun 03 '14 14:06

smerlung


People also ask

Where is Wkhtmltopdf installed Windows?

When you install wkhtmltopdf, it will go into either c:\Program Files\wkhtmltopdf\ or c:\Program Files (x86)\wkhtmltopdf. This will create the PDF in the right place. Since we're using the Windows command prompt, there's really no reason to use PHP.

What is wkhtmltopdf?

wkhtmltopdf GitHub Docs Status Support Downloads What is it? wkhtmltopdfand wkhtmltoimageare open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the Qt WebKit rendering engine. These run entirely "headless" and do not require a display or display service.

Can I use wkhtmltopdf with Untrusted HTML?

Do not use wkhtmltopdf with any untrusted HTML – be sure to sanitize any user-supplied HTML/JS, otherwise it can lead to complete takeover of the server it is running on! Please read the project status for the gory details.

How to use wkhtmltox in lambda function?

You may test it locally by unpacking the archive into the layer directory and running next commands: After that, you may find a pdf file generated from the google home page in your layer directory. To use wkhtmltox in your lambda function you may put the content of the archive together with your lambda function or create a layer.


1 Answers

In case it helps anyone, let me share the settings that worked for me, after much expirimenting.

I'm running Windows and using --disable-smart-shrinking to have full control over the margin sizes (to fit an address behind an envelope window).

Calibri seems to generally render a lot better than Arial (besides being much prettier anyway :-)), so I've set font-family: Calibri, Helvetica, Arial, sans-serif;

I get decent results using --dpi 96.

The amount of text that fits in a row is almost 1:1 between browser and PDF. (The PDF can fit slightly more text, which is less harmful than the other way around.)

We can get even more accurate by using a zoom that we then compensate in the DPI. To use zoom 1.33 without actually getting a larger result, we need to cram 1.33 times as many dots into an inch, so 96 * 1.33 = 128 DPI. Experimenting proves that the result is slightly more accurate with 130, for whatever reason, so:

--zoom 1.33 --dpi 130

like image 71
Timo Avatar answered Oct 05 '22 05:10

Timo