wkhtmltopdf scales down css dimensions

1.8k Views Asked by At

I'm experiencing a similar issue that this one with the latest update of the knp Snappy Bundle on Symfony 4.4.5.

I've set the following options and they do not help to fix the error.

options:
    - { name: 'no-outline', value: true }
    - { name: 'encoding', value: 'UTF-8' }
    - { name: 'dpi', value: 300 }
    - { name: 'orientation', value: 'Portrait' }
    - { name: 'page-size', value: 'A4' }
    - { name: 'page-width', value: '210mm' }
    - { name: 'page-height', value: '297mm' }
    - { name: 'margin-top', value: '0' }
    - { name: 'margin-bottom', value: '0' }
    - { name: 'margin-left', value: '0' }
    - { name: 'margin-right', value: '0' }
    - { name: 'disable-smart-shrinking', value: true }

Note: Changing 'disable-smart-shrinking' to 'enable-smart-shrinking' changes nothing. Adding the zoom option changes nothing either.

I have checked if the options do have an effect at all and I can confirm that they have. As en example, I changed 'Portrait' to 'Landscape', which indeed resulted in a landscape document.

Here is a test document showing the issue:

enter image description here

The blue box is the PDF that is generated by the snappy bundle. It has the correct dimensions of 210mm x 297mm, the red-border box is the result of a div with the CSS dimensions of 210mm x 297mm. There is clearly something off.

How can this be fixed?

1

There are 1 best solutions below

1
On

Short answer:

You are probably using a version of wkhtmltopdf without the qt patches To fix this, you need a patched version of wkhtmltopdf and use the same parameters you wrote in your post


Long answer:

To know if you are running a version with or without the patches.

Just run wkhtmltopdf command. If you see something like this:

Reduced Functionality:

This version of wkhtmltopdf has been compiled against a version of QT without the wkhtmltopdf patches. Therefore some features are missing, if you need these features please use the static version.

Currently the list of features only supported with patch QT includes:

  • Printing more than one HTML document into a PDF file.
  • Running without an X11 server.
  • Adding a document outline to the PDF file.
  • Adding headers and footers to the PDF file.
  • Generating a table of contents.
  • Adding links in the generated PDF file.
  • Printing using the screen media-type.
  • Disabling the smart shrink feature of WebKit.

That means that you are running a non-patched version of wkhtmltopdf.

To be able to use smart-shrinking or zoom feature, you need a patched version.


PS: I know the thread is old but I hope this will help someone