Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF Display Errors with wkhtmltopdf / tables / acrobat-reader

I generated a PDF file using wkhtmltopdf from a html page. The html page uses tables which have 1 pixel borders. If I open the PDF with Acrobat or Foxit they randomly miss to draw vertical borders, but they appear if I zoom in. So I guess it's some kind of rounding error, because the lines are too thin?

If I print the PDF it looks good.

And I just realized, it only happens if I set a background-color.

How can I fix this?


Here's a sample PDF. The border separating the characters "a" and "b" disappears depending on the zoom factor. I generated this file like this:

echo "
 <html><body>
  <span style="border: 1px solid black; background-color:red;">a</span>
  <span style="background-color:red">b</span>
 </body></html>"
| wkhtmltopdf.exe - test.pdf
like image 693
duedl0r Avatar asked Dec 21 '22 05:12

duedl0r


2 Answers

Your line is not missing, it is just too small to render on the screen.

This is because PDFs are rendered according to their page size, not according to how large the features on the page are. Everything on the page is scaled up or down to make it fit into the PDF page, and so your line is being scaled down and thus disappearing: 1 / 3 = 0.333 = no line.

To fix this you have the following options:

  1. Reduce the page size on wkhtml2pdf using: --page-size A4
  2. Reduce the width & height of the page exactly using: --page-width 9in --page-height 6in
  3. Make your line thicker.

Option 3 is probably preferable in this case. It's not a very elegant fix, but you don't have many options to play with. If you were writing the PDF on low-level then things could be done, but since you're using wkhtml2pdf then you are limited to what it allows you to set.

like image 98
Alasdair Avatar answered Feb 23 '23 16:02

Alasdair


I had similar problem with borders of a table. What helped me was use of pt instead of px in my css.

See W3C:

But in general you would use a different set of units for display on screen than for printing on paper.

like image 29
Ytus Avatar answered Feb 23 '23 16:02

Ytus