我不知道为什么会这样。每当我添加阿拉伯语内容时,它都会显示上面的错误,否则可以很好地使用英语*相同的代码在另一个项目中运行得很好,但在这里不行。*views.py
def generate_pdf_for_gift_and_add_to_cart(request):
cart = Cart(request)
if request.method == 'POST':
id = request.POST.get('project_id')
selectedAmount = request.POST.get('amount')
senderNameDonatedDonationPage = request.POST.get(
'senderNameDonatedDonationPage')
receiverNameDonatedDonationPage = request.POST.get(
'receiverNameDonatedDonationPage')
phoneNumberDonatedDonationPage = request.POST.get(
'phoneNumberDonatedDonationPage')
emailDonatedDonationPage = request.POST.get('emailDonatedDonationPage')
params = {
"project_id": id,
"selectedAmount": selectedAmount,
}
pdf = render_to_pdf('pdfs/gift_pdf.html', params)
if pdf:
response = HttpResponse(pdf, content_type='application/pdf')
filename = f"Invoice_{emailDonatedDonationPage}_{datetime.now()}.pdf"
content = "inline; filename='%s'" % filename
download = request.POST.get("download")
if download:
content = "filename='%s'" % filename
response['Content-Disposition'] = content
receipt_file = BytesIO(pdf.content)
return response
实用程序。py我还尝试了utf-8编码,但在生成的pdf页面上显示了空白框
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
gift.pdf。下面的html是示例模板,将在其中呈现该模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gift PDF</title>
<style type="text/css">
@page {
size: 6in;
}
body {
margin: 0;
}
.page_body {
background-image: url("");
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
position: relative;
width: 100%;
}
.page_body .content_row {
display: flex;
justify-content: center;
align-items: center;
grid-gap: 127px;
margin: 630px 0;
padding: 0 78px;
flex-direction: column;
}
.page_body .ehdaa {
position: absolute;
top: 33px;
left: 20%;
width: 625px;
height: 495px;
}
.page_body .flowers {
position: absolute;
top: 0;
right: 0;
}
.page_body p {
line-height: 10px;
font-size: 15px;
font-weight: 700;
text-align: center;
}
.page_body .headings {
color: #B1915A;
}
.page_body .paragraph {
color: #1F858F;
}
.page_body .hands {
position: absolute;
left: 0;
bottom: 0;
width: 370px;
height: auto;
}
</style>
</head>
<body>
<div class="page_body container-fluid">
<img class='ehdaa' src="" alt="ehda">
<img class='flowers' src=""
alt="flowers">
<div class="content_row">
<div class="headings">{{ receiverNameDonatedDonationPage }}إلي</div>
</div>
<img class="hands" src=""
alt="hands">
</div>
</body>
</html>
2条答案
按热度按时间xlpyo6sf1#
您的代码需要进行一些调整,才能在输出中不生成块字符。
1.添加支持阿拉伯语的字体
默认情况下,
xhtml2pdf
仅提供少数字体,这些字体都不支持阿拉伯语:Times Roman:Times New Roman,Times,Georgia,衬线
Helvetica:Arial,Verdana,日内瓦,sansserif,sans
Courier:Courier新的,单空间,单空间
Zapf丁巴茨
象征
因此,您想在HTML文件中添加一种字体,以创建一个
font-face
1.将编码更改为
utf-8
这与您的产品线有关:
假设您有兼容的字体,您也可以使用
ISO-8859-6
,但我在示例中使用了Arial Unicode
。这是一个非常精简的代码版本,它只加载HTML文件并在
xhtml2pdf.pisa
中运行输出如下:
小时
xyhw6mcr2#
如果你想下载html页面的pdf
您需要引用两个JS库,
然后调用下面的函数,