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.
我在Django有一个Web应用程序。这是存储账单和发票的平台。现在我正试图以PDF格式导出这些账单。
I'm using xhtml2pdf but it's not working.
我正在使用xhtml2pdf,但它不起作用。
I'm using this code for testing: http://obroll.com/generate-pdf-with-xhtml2pdf-pisa-in-django-examples/
我正在使用此代码进行测试:http://obroll.com/generate-pdf-with-xhtml2pdf-pisa-in-django-examples/
It doesnt give any errors but doesnt generate the PDF documentos.
它不会给出任何错误,但不生成PDF文档。
1 个解决方案
#1
13
Try using this code. It works for me. Change "template_testing.html" for your template and add your data to render on "data = {}"
尝试使用此代码。这个对我有用。更改模板的“template_testing.html”并添加数据以在“data = {}”上呈现
views.py:
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')
#1
13
Try using this code. It works for me. Change "template_testing.html" for your template and add your data to render on "data = {}"
尝试使用此代码。这个对我有用。更改模板的“template_testing.html”并添加数据以在“data = {}”上呈现
views.py:
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')