我使用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中的任何空格都不会被渲染。所有的单词都是紧挨着的。我试过正常的空格和“”,结果是一样的。例如,上面的代码在pdf中显示为"YourReceipt“。
当我尝试使用pisa的命令行版本时,它生成的pdf文件在单词之间留有空格。
有什么想法吗?
发布于 2011-09-12 07:47:28
好的,多亏了akonsu,问题似乎是Django的HttpResponse是如何被对待的(无论是在服务器端还是在浏览器端)。
而不是
return HttpResponse(result.getvalue(), mimetype="application/pdf")使用:
resp = HttpResponse(result.getvalue(), mimetype="application/pdf")
resp['Content-Disposition'] = 'attachment; filename=receipt.pdf'
return resp这至少会产生一个没有空格的结果。仍然不知道为什么第一种方法不起作用。
发布于 2012-02-02 13:33:05
我也遇到了同样的问题,我不想强制从浏览器下载PDF。这被证明是一个特定于平台的问题:当没有安装微软TrueType字体时,Google Chrome原生的PDF浏览器插件无法在某些Linux发行版上的特定文档中呈现空间。详情请参见http://www.google.com/support/forum/p/Chrome/thread?tid=7169b114e8ea33c7&hl=en。
我通过在bash中运行以下命令修复了这个问题(根据您的发行版进行调整;这是在Ubuntu上):
$ sudo apt-get install msttcorefonts(在安装过程中接受EULA )
$ fc-cache -fv重新启动Chrome后(重要!),本机PDF查看器正确地显示了带空格的PDF。
https://stackoverflow.com/questions/7379913
复制相似问题