【问题标题】:pdfkit - An A4 html page does not print into an A4 pdfpdfkit - A4 html 页面不会打印成 A4 pdf
【发布时间】:2017-10-08 23:49:09
【问题描述】:

我在下面重现了我的问题:

  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 倍...

如果您能看一看,我将不胜感激!

【问题讨论】:

  • 你有什么操作系统?我在 macOS 上遇到了同样的问题。

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


【解决方案1】:

对我来说,手动设置 DPI 是一种解决方法:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2010-10-11
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      • 2012-03-31
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多