Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wkhtml2pdf rendering size issue

Tags:

wkhtmltopdf

Taking care of removing any paddings or margins, I have created a basic html page containing a single table whose height is supposed to be 297mm (A4 paper height):

<!DOCTYPE html>
<html>
  <head>
  <style>
    body 
    {
      padding: 0; /* Padding for content */
      margin: 0 auto; /* Margin from container */		
    }    	
    table
    {
      padding: 2.5mm 5mm; /* Padding for content */		  
      margin:0; /* Margin from container */

      width:100%;
      border-spacing:0;
      background-color:yellow;
				
      height:297mm;
    }
  </style>
  </head>
  <body>
    <table>
      <tr style="height:3%"><td style="background-color:RGB(235, 105, 11)">1</td></tr>
      <tr style="height:30%"><td style="background-color:RGB(47,52,57)">2</td></tr>
      <tr style="height:17%"><td style="background-color:RGB(94,98,102)">3</td></tr>
      <tr style="height:auto"><td style="background-color:RGB(255,255,255)">4</td></tr>
      <tr style="height:13%"><td style="background-color:RGB(47,52,57)">5</td></tr>
    </table>
  </body>
</html>

And I'm trying to render this in A4 PDF format using wkhtml2pdf utility (again taking care of removing any margin and/or padding:

wkhtmltopdf.exe --page-size A4 -L 0 -R 0 -T 0 -B 0 .\testsize.html testsize.pdf

Anyway rendered table height does not seem to be 297mm in PDF document. It appears to be smaller:

Rendering

It is only when modifying table height to height: 371mm that I can have it to fill full A4 paper height in the PDF document (NB: 371mm/291mm is almost a x1.25 ratio).

Do you have any idea for where this ratio in rendering height comes from and how i may fix it ?

  • NB1: As far as I know wkhtml2pdf uses webkit as rendering engine
  • NB2: When displaying html code in chrome with zoom=x1, displayed height already doesn't match with A4 PDF document placed side-by-side and zoom also set to x1.
like image 755
CitizenInsane Avatar asked Oct 13 '16 13:10

CitizenInsane


1 Answers

I'm not sure wkhtmltopdf respects the mm CSS unit. That said, you could try to adjust some command line options that modify the ratio between the HTML document sizes and the rendered PDF:

  • --zoom <float>: I think this one is the most important in your case. Try to set to --zoom 1.25, the ratio you estimated.

  • --disable-smart-shrinking: In my experience, this command often prevents element sizes to have unexpected values.

  • --dpi <number>: I'm not sure if this will change something, but try 300 or 600.

like image 138
lorenzo-s Avatar answered Oct 19 '22 13:10

lorenzo-s