I'm using Django and my code to render the PDF is really typical:
t = loader.get_template('back/templates/content/receipt.html')
c = RequestContext(request, {
'pagesize': 'A4',
'invoice': invoice,
'plan': plan,
})
html = t.render(c)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), mimetype="application/pdf")
And the receipt.html is nothing unusual:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Squizzal Receipt</title>
<style type="text/css">
@page {
size: {{pagesize}};
margin: 1cm;
word-spacing 1cm;
@frame footer {
-pdf-frame-content: footerContent;
bottom: 0cm;
margin-left: 9cm;
margin-right: 9cm;
height: 1cm;
}
}
</style>
</head>
<body>
<h1>Your Receipt</h1>
<<SNIP>>
but none of the spaces in the pdf are rendered. All the words are right next to each other. I've tried normal spaces and " " and the result is the same. For example the above would appear as "YourReceipt" in the pdf.
When I try using the command line version of pisa, it generates the pdf just fine with spaces between the words.
Any thoughts?
Ok thanks to akonsu the problem seems to be how Django's HttpResponse is being treated (either on the server side or on the browser side).
Instead of
return HttpResponse(result.getvalue(), mimetype="application/pdf")
Use:
resp = HttpResponse(result.getvalue(), mimetype="application/pdf")
resp['Content-Disposition'] = 'attachment; filename=receipt.pdf'
return resp
This at least produces a result without spaces. Still no idea why the first way wasn't working.