我使用Django和我的代码来渲染PDF实在是典型的:Django的 - 比萨生成的PDF没有空格
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")
而且receipt.html什么不寻常:
<!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>>
但是没有呈现pdf中的任何空格。所有的单词都紧挨着。我试过正常的空格和“& nbsp”,结果是一样的。例如,上述内容将在pdf中显示为“YourReceipt”。
当我尝试使用命令行版本的比萨时,它生成的PDF就好了,单词之间有空格。
有什么想法?
将t.render(c).encode(“UTF-8”)的HTML输出保存到文件中,并将该文件提供给命令行版本。它产生了什么? – akonsu
html文件与预期的一样,并将该文件传递到cmd行比萨可产生预期结果。目前我正在使用django开发工具测试这个,但我也用apache上的wsgi测试了它。我想不出任何可能影响到这一点的事情。 – Mark
尝试将pisa.pisaDocument()的结果保存到文件中并在PDF查看器中打开该文件。可以吗? – akonsu