pdfkit - A4 html 页面无法打印成 A4 pdf

Lau*_*ent 5 python pdfkit html-to-pdf wkhtmltopdf 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>
    
    Run Code Online (Sandbox Code Playgroud)
  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)
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我获得了一个 pdf 文件,左上角有一个矩形,其大小大约小了 5 倍......

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

Vla*_*sov 5

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

options={'page-size':'A4', 'dpi':400}
Run Code Online (Sandbox Code Playgroud)