46👍
Fixed by:
Add base_url=request.build_absolute_uri()
so that
html = HTML(string=html_string)
becomes
html = HTML(string=html_string, base_url=request.build_absolute_uri())
That will allow for relative URLs in the HTML file.
For the images, only PNG images seems to work for some reason.
For the HTML styles to show on the PDF, add presentational_hints=True as per the Weasyprint docs:
pdf = html.write_pdf(stylesheets=[CSS(settings.STATIC_ROOT + '/css/detail_pdf_gen.css')], presentational_hints=True);
8👍
Setup static for the path of your image as:
{% load static %}
<img src="{% static 'images/your_image.png %}" alt="" />
and then you have to pass the base_url in HTML class of Weasyprint as:
HTML(string=html_string, base_url=request.build_absolute_uri())
- [Django]-How to stop autopep8 not installed messages in Code
- [Django]-How do I turn MongoDB query into a JSON?
- [Django]-Raise 404 and continue the URL chain
7👍
After adding HTML(string=html_string, base_url=request.build_absolute_uri())
to my config images were still not loading. Had to use below logging to identify real issue.
import logging
logger = logging.getLogger('weasyprint')
logger.addHandler(logging.FileHandler('/tmp/weasyprint.log'))
Then check /tmp/weasyprint.log
log file for errors.
Real issue for me was:
urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate
Fix was to disable ssl verification:
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
- [Django]-Cancel saving model when using pre_save in django
- [Django]-In celery 3.1, making django periodic task
- [Django]-How to select a record and update it, with a single queryset in Django?
0👍
I don’t know Weasyprint, but I’m using Pisa and it works very well with pictures into PDF output.
For example :
def PDFGeneration(request) :
var1 = Table1.objects.last()
var2 = Table2.objects.last()
data = {"key1" : variable1, "key2" : variable2}
html = get_template('My_template_raw.html').render(data)
result = StringIO()
with open(path, "w+b") as file :
pdf = pisa.pisaDocument(StringIO(html), file, link_callback=link_callback)
file.close()
image_data = open(path, "rb").read()
return HttpResponse(image_data, content_type="application/pdf")
return render(request, 'HTML template', context)
and
def link_callback(uri, rel):
if uri.find('chart.apis.google.com') != -1:
return uri
return os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
My PDF is generated from an .html file and I have my picture like this :
<html>
<head>
{% load staticfiles %}
{% load static %}
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="{% static 'css/MyFile.css' %}"/>
<style>
body {
font-family: Courier New, Courier, monospace;
text-align: justify;
list-style-type: none;
}
</style>
</head>
<body>
<img src="{{MEDIA_ROOT}}Logo/logo.jpeg" width="250" height="66"/>
<br></br>
...
- [Django]-Django object multiple exclude()
- [Django]-Django.core.servers.basehttp.FileWrapper disappears in Django 1.9
- [Django]-How to do "insert if not exist else update" with mongoengine?
0👍
I was having this problem. I added "base_url=’base_url’" to the following line and it finally worked.
html = weasyprint.HTML(string=rendered_string, base_url=’base_url’)
- [Django]-Django Rest Framework – Get related model field in serializer
- [Django]-Django: how save bytes object to models.FileField?
- [Django]-How do I use pagination with Django class based generic ListViews?