I get a little problem with my function letting to get a PDF file
from my HTML view
.
我的函数让我从HTML视图中获取PDF文件有一点问题。
My script comes from : Convert a HTML page to PDF using Django
我的脚本来自:使用Django将HTML页面转换为PDF
From a form, I generate a resume which could be exportable to PDF file.
从一个表单中,我生成了一份可以导出为PDF文件的简历。
My function looks like :
我的函数是这样的:
def BirthCertificate_PDF(request, id) :
birthcertificate = get_object_or_404(BirthCertificate, pk=id)
data = {}
template = get_template('BC_raw.html')
html = template.render(Context(data))
file = open('test.pdf', "w+b")
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file,
encoding='utf-8')
file.seek(0)
pdf = file.read()
file.close()
return HttpResponse(pdf, 'application/pdf')
And my template looks like :
我的模板是这样的:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<h2 align="center"> ACTE DE NAISSANCE </align> </h2>
{% block content %}
<h3 align = "right"> Acte de Naissance n° {{birthcertificate.id}} </align> </h3>
<h3> Informations concernant l'enfant : </h3>
<li> Nom de l'enfant : {{birthcertificate.lastname}}</li>
<li>Prénom(s) de l'enfant : {{birthcertificate.firstname}}</li>
<li>Est né(e) à : {{birthcertificate.birthcity}}</li>
<li>Le : {{birthcertificate.birthday}} à {{birthcertificate.birthhour}}</li>
<li>De sexe : {{birthcertificate.sex}}</li>
<hr width = "50%"></hr>
<h3> Informations concernant le père : </h3>
<li>Fils ou fille du nommé : {{birthcertificate.fk_parent1.lastname}} {{birthcertificate.fk_parent1.firstname}}</li> <!-- Get only firstname and lastname element -->
<li>Né le : {{birthcertificate.fk_parent1.birthday}}</li> <!-- Get only birthday element -->
<li>A : {{birthcertificate.fk_parent1.birthcity}}</li> <!-- Get only birthcity element -->
<li>De nationalité : {{birthcertificate.fk_parent1.nationality}}</li> <!-- Get only nationality element -->
<li>Domicilié au : {{birthcertificate.fk_parent1.adress}}</li>
<li>A : {{birthcertificate.fk_parent1.city}} ({{birthcertificate.fk_parent1.zip}}) - {{birthcertificate.fk_parent1.country}}</li> <!-- Get only adress, zip, city, country element -->
<li>Exerçant la profession de : {{birthcertificate.fk_parent1.job}}</li> <!-- Get only job element -->
<hr width = "50%"></hr>
<h3> Informations concernant la mère : </h3>
<li>Fils ou fille de la nommée : {{birthcertificate.fk_parent2.lastname}} {{birthcertificate.fk_parent2.firstname}}</li> <!-- Get only firstname and lastname element -->
<li>Née le : {{birthcertificate.fk_parent2.birthday}}</li> <!-- Get only birthday element -->
<li>A : {{birthcertificate.fk_parent2.birthcity}}</li> <!-- Get only birthcity element -->
<li>De nationalité : {{birthcertificate.fk_parent2.nationality}}</li> <!-- Get only nationality element -->
<li>Domiciliée au : {{birthcertificate.fk_parent2.adress}}</li>
<li>A : {{birthcertificate.fk_parent2.city}} ({{birthcertificate.fk_parent2.zip}}) - {{birthcertificate.fk_parent2.country}}</li>
<li>Exerçant la profession de : {{birthcertificate.fk_parent2.job}}</li> <!-- Get only job element -->
<br></br>
<h3> Par la présente, l'Officier d'Etat Civil certifie la sincérité de la présente déclaration </h3>
<h3 align = "right"> L'Officier d'Etat Civil </align> </h3>
<h3 align = "right"> Signature et tampon </align> </h3>
{% endblock %}
But, even if I get a good PDF file, all Python variables missed from this file.
但是,即使我得到一个很好的PDF文件,所有的Python变量也会从这个文件中丢失。
So my question is : How I can display my HTML background and variables on my PDF ?
我的问题是:如何在PDF中显示HTML背景和变量?
My PDF looks like :
我的PDF格式如下:
1 个解决方案
#1
1
You need to pass the birthcertificate object in the data dictionary. It is empty the way you are sending it now.
您需要在数据字典中传递birthcertificate对象。你现在发送的方式是空的。
Change the line:
改变:
data = {}
to
来
data = {'birthcertificate': birthcertificate, }
#1
1
You need to pass the birthcertificate object in the data dictionary. It is empty the way you are sending it now.
您需要在数据字典中传递birthcertificate对象。你现在发送的方式是空的。
Change the line:
改变:
data = {}
to
来
data = {'birthcertificate': birthcertificate, }