[英]Is there a way to generate pdf containing non-ascii symbols with pisa from django template?
我正在嘗試使用以下代碼段從模板生成 pdf:
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")), result)
if not pdf.err:
return http.HttpResponse(result.getvalue(), mimetype='application/pdf')
except Exception('PDF error')
所有非拉丁符號都沒有正確顯示,模板和視圖使用 utf-8 編碼保存。
我試過將視圖保存為 ANSI,然后保存給用戶 unicode(html,"UTF-8"),但它拋出 TypeError。
我還認為可能是因為默認字體不支持 utf-8,所以根據 pisa 文檔,我嘗試在樣式部分的模板正文中設置字體。
那仍然沒有結果。
有沒有人有一些想法如何解決這個問題?
這對我有用:
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result, encoding='UTF-8')
嘗試更換
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), result)
和
pdf = pisa.pisaDocument(StringIO.StringIO(html), result, encoding='UTF-8')
您需要修改 django 模板。 在樣式表中添加一個新字體,該字體將鏈接到包含文檔中使用的字符的字體文件。 並且該字體文件必須可以從您的服務器訪問(在 Ubuntu 下,您可以在 /usr/share/fonts/truetype/ 目錄中找到帶有字體的文件)。 例如:
@font-face {
font-family: DejaMono;
src: url(font/DejaVuSansMono.ttf);
}
然后,如果您有下一個 HTML 代碼:
<div>Some non-latin characters</div>
您可以使用以下 CSS 規則以 DejaMono 字體顯示該文本:
div { font-family: DejaMono; }
當我生成帶有西里爾字符的 PDF 文檔時,這對我有用。
我在使用西里爾字符時遇到了同樣的問題。
該解決方案包含兩個步驟: 1. 指出 HTML 文件中的字體文件
<style type="text/css">
@font-face {
font-family: Arial; src: url("files/arial.ttf");
}
body {
font-family: Arial;
}
</style>
2.在我的例子中給出“pisa”根路徑(以便它通過相對路徑找到字體文件)它是這樣的
pdf = pisa.pisaDocument(html, result, path=PATH_TO_DJANGO_PROJECT)
因為字體被放置在 PATH_TO_DJANGO_PROJECT/files/arial.ttf
如果您正在調用 createPDF 而不是 pisaDocument 方法,您可以使用
pisa.CreatePDF(html.encode('UTF-8'), response, link_callback=fetch_resources, encoding='UTF-8')
我使用的是 xhtml2pdf 版本 0.2.4 我遇到了同樣的問題,我能夠使用以下編碼解決:
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
# PDF
pdf = pisa.pisaDocument(BytesIO(html.encode("utf-8")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
然后在我看來,我定義如下:
def download_pdf(request, pk):
item = get_object_or_404(object, id=pk)
if item:
context ={
'item': item
}
template_name = 'template.html'
pdf = render_to_pdf(template_name, context)
return HttpResponse(pdf, content_type='application/pdf')
else:
messages.info(request, 'Item not found')
return redirect('home')
我的 HTML:
{% load static %}
{% load humanize %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/gif" href="{% static 'images/profile.png' %}" />
<title>{{ item.scientific_name}}</title>
<link rel="stylesheet" href="{% static 'style/Bootstrap/css/bootstrap.min.css' %}">
<style>
@page {
size: letter;
margin: 2cm;
@frame footer_frame {
/* Another static Frame */
-pdf-frame-content: footer_content;
left: 50pt;
width: 512pt;
top: 760pt;
height: 50px;
}
table {
-pdf-keep-with-next: true;
}
}
ul {
list-style-type: none;
}
ul li span {
font-weight: bold;
}
</style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.