I am converting Html into pdf using xhtml2pdf using Django and this is my code
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("utf-8")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
def myView(request):
pdf = render_to_pdf(pdf_template, params)
return HttpResponse(pdf, content_type='application/pdf')
everything's working fine till now, but I also want to show the company logo and some checkboxes on pdf and that is not working.
no error, no wrong output, just blank space in place of image and checkboxes.
Any idea of how can I get images and checkboxes in my pdf?
this is my HTML file
<tr>
<td colspan="12" align="center" style="font-size:larger;">
<img src="/static/assets/img/theme/vue.jpg">
<input type="checkbox"> Orginal Receipt</td>
<u>Some Company</u>
Phone: 123-456
Website: www.example.com
</td>
</tr>
image and checkbox are showing in normal html template but not in pdf
First, if you would like to refer to some image that considered as static files in your repo, you will have to add the "link callback" method. also, probably you might need to run "python manage.py collectstatic". you can read more info about it here in the documentation.
I had some problems with placing images inside table elements as well. Try to figure out another way to place an images where you would like to (div etc...) - it worked for me