Reputation: 183
I am trying to export my html pages into pdf using Pisa library in django, and here is the code that I use to do that:
def fetch_resources(uri, rel):
path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, ""))
return path
def write_pdf(template_src, context_dict):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")),dest = result, encoding='UTF-8', link_callback=fetch_resources)
if not pdf.err:
return http.HttpResponse(result.getvalue(), mimetype='application/pdf')
return http.HttpResponse('Sorry, no pdf! %s' % cgi.escape(html))
def article(request):
return write_pdf('Storehouse/ReceiptPrint-v2.html',{'pagesize':'A4'})
But I have two problems when I get the pdf output: first, it does not render my css files and all of my styles are missed in the pdf (my css files are very large, so please don't suggest to have the style in the html file) and second it does not understand the specified unicode (UTF-8), my html pages are in Farsi, but when it turns into pdf, it changes into some meaningless squares.
Any help would be appreciated a lot!
Upvotes: 2
Views: 626
Reputation: 876
Pisa's converter uses the ReportLab Toolkit, which has a difficult time rendering Farsi and other Arabic characters straight out of the box. I found that using the DejaVuSansMono.ttf font allows you to render the characters properly.
Upvotes: 1