Using disable-smart-shrinking with wkhtmltopdf doesn't effect header font size

20,716

This patch will disable smart intelligent shrinking for header/footer:

    diff --git a/src/lib/pdfconverter.cc b/src/lib/pdfconverter.cc
    index e815758..1504a14 100644
    --- a/src/lib/pdfconverter.cc
    +++ b/src/lib/pdfconverter.cc
    @@ -482,6 +482,7 @@ void PdfConverterPrivate::endPage(PageObject & object, bool hasHeaderFooter, int

            if (!object.headers.empty()) {
                    QWebPage * header = object.headers[objectPage];
    +                updateWebSettings(header->settings(), object.settings.web);
                    painter->save();
                    painter->resetTransform();
                    double spacing = s.header.spacing * printer->height() / printer->heightMM();
    @@ -506,6 +507,7 @@ void PdfConverterPrivate::endPage(PageObject & object, bool hasHeaderFooter, int

            if (!object.footers.empty()) {
                    QWebPage * footer = object.footers[objectPage];
    +                updateWebSettings(footer->settings(), object.settings.web);
                    painter->save();
                    painter->resetTransform();
                    double spacing = s.footer.spacing * printer->height() / printer->heightMM();
     }

     /*!

Applying patch: patch -l -p1 < ../wkhtmltopdf_smart_shrinking_header_footer_disable_fix.patch

This solution works on windows/linux. To get precise (1mm in html equal to 1mm on pdf) rendering you need to run wkhtmltopdf with options "--disable-smart-shrinking --dpi 96"

On Mac OS X precise rendering can be reached with "--enable-smart-shrinking" and css style on html body "body { width: 210mm }". This solution works only on mac os x.

Share:
20,716
ds99jove
Author by

ds99jove

Updated on July 09, 2022

Comments

  • ds99jove
    ds99jove almost 2 years

    I'm having a font size issue with wkhtmltopdf when using --disable-smart-shrinking.

    I'm not sure if this flag actually enlargens the content or shrinks it. What's clear however is that it's not doing the same thing with the header.

    As we're letting our users design their own forms it's essential that a certain font size is the same whether it's in the header or the body. We also find it very important to use --disable-smart-shrinking as this makes our content fill the entire page nicely.

    I've produced a simple samle html with the follow code

    <html>
     <head>
     </head>
     <body style='margin: 0; border: 0'>
      <h3>My H3 text</h3>
     </body>
    </html>
    

    Save it as test.html and run wkhtmltopdf with the disable-smart-shrinking flag

    wkhtmltopdf.exe --disable-smart-shrinking --no-outline --header-spacing 10 -T 20 --header-html test.html test.html c:\temp\disable_smart_shrinking.pdf

    Result (disable-smart-shrinking):

    disable-smart-shrinking http://img138.imageshack.us/img138/5143/disablesmartshrinking.th.png

    Notice that H3 in the body is alot larger than H3 in the header.

    wkhtmltopdf.exe --enable-smart-shrinking --no-outline --header-spacing 10 -T 20 --header-html test.html test.html c:\temp\disable_smart_shrinking.pdf

    Result (enable-smart-shrinking):

    enable-smart-shrinking

    The font size of the H3-element now stays the same between header and body

    Question: We want to create a pdf that uses the entire width of the page, which --disable-smart-shrinking lets us. But we want the same size on our fonts to be applied to the header. Does anyone have a solution to do this? Are we doing anything wrong?

  • Joel Peltonen
    Joel Peltonen about 10 years
    Hey! Have you done a pull request for this in github for wkhtmltopdf 0.12? I think it would be a good addition!