【发布时间】: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