【问题标题】:PDFkit is not applying bootsrap CSSPDFkit 没有应用引导 CSS
【发布时间】:2020-10-09 12:37:01
【问题描述】:

我正在将 HTML 转换为 PDF,我的 HTML 包含引导类:

我尝试了两种方法:

  1. 使用 PDFkit
  2. 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


    【解决方案1】:

    !important 添加到背景样式。例如:

    <div class="col-sm-4" style="background-color:lavender !important;">.col-sm-4</div>
    

    【讨论】:

      猜你喜欢
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      相关资源
      最近更新 更多