【发布时间】:2020-10-09 12:37:01
【问题描述】:
我正在将 HTML 转换为 PDF,我的 HTML 包含引导类:
我尝试了两种方法:
- 使用 PDFkit
- weasyprint
以下是我的 HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="pdfcss.css">
</head>
<body>
<div class="container-fluid">
<h1>Hello World!</h1>
<p>Resize the browser window to see the effect.</p>
<p>The columns will automatically stack on top of each other when the screen is less than 768px wide.</p>
<div class="row">
<div class="col-sm-4" style="background-color:lavender;">.col-sm-4</div>
<div class="col-sm-4" style="background-color:lavenderblush;">.col-sm-4</div>
<div class="col-sm-4" style="background-color:lavender;">.col-sm-4</div>
</div>
</div>
</body>
</html>
使用 PDKkit:
import pdfkit
css = 'pdfcss.css'
pdfkit.from_file('test_test.html', '/tmp/out.pdf',css=css)
得到以下结果为 PDFenter image description here
通过使用 weasyprint:
from weasyprint import HTML, CSS
HTML(string='<!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href=""> </head> <body> <div class="container-fluid"> <h1>Hello World!</h1> <p>Resize the browser window to see the effect.</p> <p>The columns will automatically stack on top of each other when the screen is less than 768px wide.</p> <div class="row"> <div class="col-sm-4" style="background-color:lavender;">.col-sm-4</div> <div class="col-sm-4" style="background-color:lavenderblush;">.col-sm-4</div> <div class="col-sm-4" style="background-color:lavender;">.col-sm-4</div> </div> </div> </body> </html>').write_pdf("/tmp/out.pdf", stylesheets=['/var/www/html/NTAsset-Repo/NtAsset/NtAseetWepApp/website/static/pdfcss.css'])
在这两种情况下,CSS 都不会应用于 PDF,
请帮我解决这个问题。
【问题讨论】:
标签: python html python-3.x wkhtmltopdf pdfkit