问题描述
我想使用xhtml2pdf将阿拉伯字符导出为pdf,因为我在Django应用中使用了它。
我知道他们进行了更新,说他们已修复它,但是它对我不起作用,我使用了自定义字体,但仍然无法正常工作。
因此,请任何人知道为此或围绕它正确使用的加密技术,请帮助我。
invoice.html:
{% load static %}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<head>
<title>{{title}}</title>
<style type="text/css">
@font-face {
font-family: Amiri,"Amiri Font";
src: url("{% static 'font/Amiri-Italic.ttf'%}");
}
body {
font-weight: 200;
font-size: 14px;
}
</style>
</head>
<body><pdf:language name="arabic"/>
<div class='wrapper'>
<div class='header'>
<p class='title'>Invoice # {{ invoice_ID }} </p>
</div>
<div>
<div class='details'>
<pdf:language name="arabic"/>
<p> تجربة</p>
customername : {{customer_name}} <br><hr>
<hr class='hrItem' />
</div>
</div>
</body>
</html>
utils.py:
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("UTF-8")),result)
if not pdf.err:
return HttpResponse(result.getvalue(),content_type='application/pdf')
return None
views.py:
from django.shortcuts import render,redirect,HttpResponse
import pandas as pd
from .models import SaleInvoice,TransferWays,User,InstallingCities
from siteManagment.models import MarketersManagment,WareHouses
from .utils import render_to_pdf
from django.core.validators import ValidationError
def renderPDF(request):
if request.GET:
invoice_ID = request.GET.get('invoice_ID')
if invoice_ID:
result = SaleInvoice.objects.all().filter(invoice_ID=invoice_ID).values()
df = pd.DataFrame(result)
#theUser = User.objects.all().filter(id=(df['User_id'])).values('username')
#df['total'].values
data = {
"title" : ' الفاتورة رقم {}'.format(invoice_ID),"invoice_ID" : invoice_ID,"customer_name" : df['customer_name'],}
pdf = render_to_pdf('invoice.html',data)
return HttpResponse(pdf,content_type='application/pdf')
else:
raise ValidationError
else:
return redirect('admin/')
解决方法
我解决了这个问题,文档实际上没有错,问题出在静态..因为当您使用{%static'whatever'%}时,它将搜索静态文件,但是由于某些原因,它无法正常工作我认为问题出在django本身,也许您无法从pdf文件中获取静态信息。这是我的新invoice.html的任何人(我已经将字体的整个路径都放在了其中):
{% load static %}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<head>
<title>{{title}}</title>
<style type="text/css">
.header {
font-size: 20px;
font-weight: 100;
text-align: center;
color: #007cae;
}
.title {
font-size: 22px;
font-weight: 100;
/* text-align: right;*/
padding: 10px 20px 0px 20px;
}
.title span {
color: #007cae;
}
.details {
padding: 10px 20px 0px 20px;
text-align: right !important;
/*margin-left: 40%;*/
}
.hrItem {
border: none;
height: 1px;
/* Set the hr color */
color: #333;
/* old IE */
background-color: #fff;
/* Modern Browsers */
}
@font-face {font-family: RTLFont; src: url('C:\\Users\\BigBoy\\Desktop\\MyProjects\\ToysSite\\static\\font\\Amiri-Italic.ttf')} // Here
body {
font-weight: 200;
font-size: 14px;
font-family: RTLFont;
}
/* @page {
background-image: url('C:\Users\BigBoy\Desktop\MyProjects\ToysSite\static\background.png');
} */
</style>
</head>
<body>
<div class='wrapper'>
<div class='header'>
</div>
<div>
<div class='details'>
<pdf:language name="arabic"/>
اسم العميل : {{customer_name}} <br><hr>
<hr class='hrItem' />
</div>
</div>
</div>
</body>
</html>