I have an app that allows you to upload an image and I need to generate a pdf with that image.
I use weasyprint to generate the pdf but it does not show the image.
In an html file the image does show, but in a pdf file it does not. What am I doing wrong?
In view.py I include: from weasyprint import HTML
In settings.py I have DEBUG = True
the version of python is 3.12.3
django version 5.0.6
WeasyPrint version 63.0
I have changed render to render_to_string and the pdf does not show the image.
When the pdf is generated it does not show the photo, it shows me the alt value of the tag “Foto subida”
I think that WeasyPrint can’t access the image using the relative URL. When you use {{student.picture.url}}, it generates a URL like /media/picture/image.jpg, but WeasyPrint needs either an absolute URL or the actual file path on the system.
Try to use absolute urls:
def Export_pdf(request, student_id):
student = get_object_or_404(Student, id=student_id)
# Build the absolute URL for the image
base_url = request.build_absolute_uri('/')[:-1] # Gets domain without trailing slash
context = {
'student': student,
'base_url': base_url
}
html_string = render_to_string('sendInfo/home_page.html', context)
pdf = HTML(string=html_string, base_url=request.build_absolute_uri('/')).write_pdf()
response = HttpResponse(pdf, content_type='application/pdf')
response['Content-Disposition'] = f'attachment; filename="{student.name}.pdf"'
return response