【问题标题】:How to add a static file in the pdf file using xhtml2pdf?如何使用xhtml2pdf在pdf文件中添加静态文件?
【发布时间】:2022-07-05 22:29:27
【问题描述】:

我正在尝试使用 xhtml2pdf 在我的 pdf 文件的文件夹中添加一个静态图像。

我的app.py 看起来像这样。

from xhtml2pdf import pisa
from pathlib import Path

output_filename = "output.pdf"
source_html = Path('template.html').read_text()
print(source_html)

result_file = open(output_filename, "w+b")

pisaStatus = pisa.CreatePDF(
    source_html,
    dest=result_file)
result_file.close()

而我的template.html 看起来像这样。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>Title of the document.</h1>
    <img src="img/logo.png" />
  </body>
</html>

我的 logo.png 位于 app.py 旁边的 img 文件夹中。

app.py
img/
   logo.png
template.html

当我跑步时

python app.py      

这确实会生成 pdf 文件,但是缺少图像。它给了我以下错误。

Extract data form local file
Need a valid file name!
'<img src="img/logo.png"/>'

如何解决?

【问题讨论】:

    标签: python html templates xhtml2pdf


    【解决方案1】:

    使 png 文件对所有人都可读。

    chmod 444 img/logo.png

    【讨论】:

      猜你喜欢
      • 2022-06-30
      • 2011-04-25
      • 2014-07-31
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 2019-03-05
      相关资源
      最近更新 更多