Search code examples
wkhtmltopdf

Different font size rendering between Linux and OSX builds


For a "font-size: 1cm" css rule, Wkhtmltopdf for OSX renders a 1cm font size, whereas Linux build renders a ~ 0.77cm size.

Here is my HTML code :

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title></title>
        <style>
            body {
                font-size: 1cm;
                line-height: 1cm;
                font-family: sans-serif;
            }
        </style>
    </head>
    <body>
        TEST ME 1<br />
        TEST ME 2<br />
        TEST ME 3<br />
        TEST ME 4<br />
        TEST ME 5<br />
        TEST ME 6<br />
        TEST ME 7<br />
        TEST ME 8<br />
        TEST ME 9<br />
        TEST ME 10<br />
        TEST ME 11<br />
        TEST ME 12<br />
        TEST ME 13<br />
        TEST ME 14<br />
        TEST ME 15<br />
        TEST ME 16<br />
        TEST ME 17<br />
        TEST ME 18
    </body>
</html>

Command :

wkhtmltopdf testPdf.html testPdf.pdf

Any idea? Thanks!


Solution

  • Found the answer here : http://blog.gluga.com/2012/05/wkhtmltopdf-font-and-sizing-issues.html

    Meter/inch units doesn't seem to be consistent as wkhtmltopdf renders the html inside a Webkit rendering engine, which can be configurated with different screen resolutions.

    So, the solution is to set fix CSS width and height to the page container.

    In my case :

        body {
            font-size: 1cm;
            line-height: 1cm;
            font-family: sans-serif;
            width: 1600px;
            height: 1050px;
        }
    

    did the trick.

    NOTE: In my case, in order to have a accurate dpi rendering on a A4 landscape orientation on OSX, I have to set low values (width: 1120px; with 1cm left and right margin): that led to a smaller rendering on Linux, which I fixed with --zoom 1.8 (I guess any high value works, it will only make it match full page's width, not more).