pdfkit - A4 html页面不会打印成A4 pdf

时间:2017-05-10 11:03:01

标签: python wkhtmltopdf pdfkit html-to-pdf python-pdfkit

我在下面转载了我的问题:

  1. 我在网页上绘制一个210x297的矩形

    <!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. 我用Python中的pdfkit将这个html文档转换为pdf

    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. 我获得了一个pdf文件,左上角有一个矩形,大小只有5倍......

  4. 如果你能看看我真的很感激!

2 个答案:

答案 0 :(得分:2)

对我来说,手动设置DPI可以解决方法:

options={'page-size':'A4', 'dpi':400}

答案 1 :(得分:0)

您可以尝试通过以下选项禁用收缩

options = { 'disable-smart-shrinking': ''}