如何使用Django将HTML页面转换为PDF [英] How can I convert a HTML Page to PDF using Django
问题描述
我使用的是xhtml2pdf,但不起作用。
我正在使用这个代码进行测试:
http://obroll.com/generate-pdf-with-xhtml2pdf-pisa-in-django-examples/
它没有给出任何错误,但不生成PDF文档。
尝试使用此代码。它适用于我
为模板更改template_testing.html,并将数据添加到data = {}上呈现
views.py:
导入os
从django.conf导入设置
from django.http import HttpResponse
from django.template import上下文
from django.template.loader import get_template
import datetime
from xhtml2pdf import pisa
def generate_PDF(request):
data = {}
template = get_template('template_testing.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')
I have a web app in Django. It's a plataform to store bills and invoices. Now i'm trying to export those bills un PDF.
I'm using xhtml2pdf but it's not working.
I'm using this code for testing: http://obroll.com/generate-pdf-with-xhtml2pdf-pisa-in-django-examples/
It doesnt give any errors but doesnt generate the PDF documentos.
Try using this code. It works for me. Change "template_testing.html" for your template and add your data to render on "data = {}"
views.py:
import os
from django.conf import settings
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
import datetime
from xhtml2pdf import pisa
def generate_PDF(request):
data = {}
template = get_template('template_testing.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')
这篇关于如何使用Django将HTML页面转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!