【发布时间】:2015-10-13 02:19:02
【问题描述】:
我正在使用 Python 的 Weasyprint 库来尝试将 html 文件打印为 pdf。我正在尝试将图像嵌入到页面的背景中。这是代码:
HTML(string='''
<h1>The title</h1>
<p>Content goes here
''', base_url=os.path.dirname(os.path.realpath(__file__))).write_pdf("hello.pdf", stylesheets=[CSS(string='body{background-image: url("example_image.png")}')])
我得到此代码的输出如下:
Ignored `background-image: url("example_image.png")` at 1:6, Relative URI reference without a base URI: 'example_image.png'.
blah@blah:~/Dropbox/Terraverde/annual_reports$ python3 test_excel.py
我已尝试在 Stackoverflow 上搜索此问题的解决方案,并已阅读文档,但我能找到的最接近答案的是以下关于 Django 的相同问题的帖子:Django WeasyPrint CSS integration warning: Relative URI reference without a base URI: <link href="/static/css/bootstrap.min.css"> at line None
我也尝试在我的代码中使用 document.baseURI:
base_url=os.path.dirname(os.path.realpath(__file__))).write_pdf("hello.pdf", stylesheets=[CSS(string='body{background-image: url(document.baseURI + "example_image.png")}')])
但这仍然产生了错误:
Parse error at 1:24, unexpected BAD_URI token in property value
关于如何处理问题的任何建议,或者对于常规 Python 或 Flask 可能类似于 Django 的 request.build_absolute_uri() 的命令?
【问题讨论】:
标签: python css flask weasyprint