【发布时间】:2019-08-11 18:30:22
【问题描述】:
我正在处理一个 django 项目,我想生成一个 pdf 文件,其中将显示用户的信息,页眉和页脚必须是图像,我使用 xhtml2pdf 库创建 pdf 文件,我已经在某处读到该库不使用“真正的”css 规则。所以图像必须是pdf格式,我尝试转换它但它仍然没有显示。 这是我的看法:
class attestation_travail(View):
def get(self, request, *args, **kwargs):
template = get_template('personnel/attestation_travail.html')
context = {
'url': static('images/1.pdf')
}
html = template.render(context)
pdf = render_to_pdf('personnel/attestation_travail.html', context)
if pdf:
response = HttpResponse(pdf, content_type='application/pdf')
n = random.randint(100, 100000)
filename = "Attestation_travail_%s.pdf" %n
content = "inline; filename='%s'" %(filename)
download = request.GET.get("download")
if download:
content = "attachment; filename='%s'" %(filename)
response['Content-Disposition'] = content
return response
return HttpResponse("Not found")
这是我的 html 文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Attestation de travail</title>
<style type="text/css">
body {
font-weight: 200;
font-size: 14px;
}
.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: left !important;
/*margin-left: 40%;*/
}
.hrItem {
border: none;
height: 1px;
/* Set the hr color */
color: #333; /* old IE */
background-color: #fff; /* Modern Browsers */
}
</style>
</head>
<body>
<div class='wrapper'>
<div class='header'>
<img src="{{ url }}">
</div>
</div>
<div class='details'>
<hr class='hrItem' />
</div>
</div>
</body>
</html>
【问题讨论】: