Python 使用xhtml2pdf和下载链接添加保存路径?
我使用xhtml2pdf将表单转换为PDF文件。 默认情况下,它将此PDF保存到与manage.py文件相同的位置 我在问如何修改保存路径,以便在我的桌面上发送PDF(例如,使用MacOSX) 这是我的职责:Python 使用xhtml2pdf和下载链接添加保存路径?,python,html,django,pdf,Python,Html,Django,Pdf,我使用xhtml2pdf将表单转换为PDF文件。 默认情况下,它将此PDF保存到与manage.py文件相同的位置 我在问如何修改保存路径,以便在我的桌面上发送PDF(例如,使用MacOSX) 这是我的职责: def BirthCertificate_PDF(request, id) : birthcertificate = get_object_or_404(BirthCertificate, pk=id) data = {"birthcertificate" : birt
def BirthCertificate_PDF(request, id) :
birthcertificate = get_object_or_404(BirthCertificate, pk=id)
data = {"birthcertificate" : birthcertificate}
template = get_template('BC_raw.html')
html = template.render(Context(data))
#filename = 'acte_de_naissance_' + str(BirthCertificate.lastname)
filename = 'Acte_Naissance_' + str(BirthCertificate.objects.get(pk=id).lastname) + '_' + str(BirthCertificate.objects.get(pk=id).firstname) + '_' + str(BirthCertificate.objects.get(pk=id).birthday) + '.pdf'
file = open(filename, "w+b")
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8')
file.seek(0)
pdf = file.read()
if pdf :
context = {"birthcertificate":birthcertificate}
return render(request, 'BC_resume.html', context)
file.close()
return HttpResponse(pdf, 'application/pdf')
我阅读了这个文档:但我没有任何解决方案
----------------------------------------------------------------------------------------------------------------
然后我还有一个问题(也许我应该创建一个新问题?)。我放了一个HTML按钮,可以保存PDF。但我希望具有以下功能:
def BirthCertificate_PDF(request, id) :
birthcertificate = get_object_or_404(BirthCertificate, pk=id)
data = {"birthcertificate" : birthcertificate}
template = get_template('BC_raw.html')
html = template.render(Context(data))
#filename = 'acte_de_naissance_' + str(BirthCertificate.lastname)
filename = 'Acte_Naissance_' + str(BirthCertificate.objects.get(pk=id).lastname) + '_' + str(BirthCertificate.objects.get(pk=id).firstname) + '_' + str(BirthCertificate.objects.get(pk=id).birthday) + '.pdf'
file = open(filename, "w+b")
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8')
file.seek(0)
pdf = file.read()
if pdf :
context = {"birthcertificate":birthcertificate}
return render(request, 'BC_resume.html', context)
file.close()
return HttpResponse(pdf, 'application/pdf')
我单击按钮-->会得到一个窗口,可以打开或保存PDF
这是我的html模板,用于保存pdf:
<h2 align="center"> Votre formulaire a été validé </align> </h2>
{% block content %}
L'enfant a été enregistré sous le numéro : {{ birthcertificate.id }}
<h3> Récapitulatif des données enregistrées : </h3>
<li> Nom : {{birthcertificate.lastname}}</li>
<li> Prénom : {{birthcertificate.firstname}}</li>
<li> Sexe : {{birthcertificate.sex}}</li>
<li> Date de Naissance : {{birthcertificate.birthday}}</li>
<li> Ville de Naissance : {{birthcertificate.birthhour}}</li>
<li> Pays de Naissance : {{birthcertificate.birthcity}}</li>
<li> Nationalité : {{birthcertificate.birthcountry}}</li>
<li> Père : {{birthcertificate.fk_parent1}}</li>
<li> Mère : {{birthcertificate.fk_parent2}}</li>
{% endblock %}
<br></br>
<form method='POST' action="{% url "BChome" %}"> {% csrf_token %}
<input type ="submit" value="Retour gestion des actes de naissances" />
</form>
<form method='POST' action="{% url "PDF" birthcertificate.id %}"> {% csrf_token %}
<input type ="submit" value="Générer PDF acte de naissance" />
</form>
<form method='POST' action='/Identity/accueil'> {% csrf_token %}
<input type ="submit" value="Retour gestion des fiches identités" />
</form>
Votre formulaire aétévalidé
{%block content%}
L'enfant aétéenregistrésous le numéro:{{{birthcertificate.id}
在登记册上签字:
名字:{{birthcertificate.lastname}}
名称:{{birthcertificate.firstname}
性别:{{出生证.性别}
出生日期:{{出生证.生日}
Ville de Naissance:{{出生证.出生时间}
出生地:{{出生证.出生城市}
国籍:{{出生证.出生国}
Père:{{birthcertificate.fkèu parent1}}
Mère:{{birthcertificate.fkèu parent2}}
{%endblock%}
{%csrf_令牌%}
{%csrf_令牌%}
{%csrf_令牌%}
如果您对这两个问题都有建议,请向您表示感谢:)在这一行中,您可以定义要保存文件的位置:
file = open(filename, "w+b")
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8')
只需将文件名更改为您要保存它的目标,对于桌面:“/Users/userName/desktop/”+filename
因此,您的代码看起来像:
destination = "/Users/userName/Desktop/"
file = open(destination + filename, "w+b")
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8')
当然,您可以将目标设置为函数的参数