Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pdfkit - An A4 html page does not print into an A4 pdf

I have reproduced my problem below :

  1. I draw a 210x297 rectangle on a web page

    <!DOCTYPE html>
    <html>
    <style>
    div.rectangle {
      border: solid 1px;
      width: 210mm;
      height: 297mm;
    }
    </style>
    <head>
    </head> 
    <body> 
    <div class="rectangle">
      <img/>
    </div>
    </body>
    </html>
    
  2. I transform this html document to pdf with pdfkit in Python

    import pdfkit
    
    options = {
        'page-size':'A4',
        'encoding':'utf-8', 
        'margin-top':'0cm',
        'margin-bottom':'0cm',
        'margin-left':'0cm',
        'margin-right':'0cm'
    }
    
    pdfkit.from_file('test.html', 'test.pdf', options=options)
    
  3. I obtain a pdf file with a rectangle at the top left corner whose size is roughly 5 times too small...

I would really appreciate if you can have a look !

like image 987
Laurent Avatar asked May 10 '17 11:05

Laurent


2 Answers

For me manual set up of the DPI works as workaround:

options={'page-size':'A4', 'dpi':400}
like image 184
Vladimir Vlasov Avatar answered Oct 03 '22 16:10

Vladimir Vlasov


you can try to disable shrinking through the options as below

options = { 'disable-smart-shrinking': ''}
like image 22
Hiran Avatar answered Oct 03 '22 14:10

Hiran