I'm using the Weasyprint library for Python in an attempt to print out an html file to pdf. I am attempting to embed an image into the background of my page. Here is the code:
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")}')])
The output I get to this code is the following:
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
I have tried to search Stackoverflow for solutions to this problem, and have read the documentation, but the closest thing I could find to an answer is the following post regarding an identical problem but for Django: Django WeasyPrint CSS integration warning: Relative URI reference without a base URI: <link href="/static/css/bootstrap.min.css"> at line None
I also tried using document.baseURI in my code:
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")}')])
but this still yielded an error:
Parse error at 1:24, unexpected BAD_URI token in property value
Any suggestions on how to handle a problem, or perhaps a command similar to Django's request.build_absolute_uri()
for either regular Python or for Flask?