简体   繁体   中英

How to get editable fields(form fields) when using xhtml2pdf/pisa?

I am trying to create a django project that converts an html file to a pdf. I am using xhtml2pdf and specifically pisa. I can get the pdf but there are no form fields in the pdf. Inside my view I have a method that contains the following code:

views.py

from django.template.loader import get_template
from django.template import Context
from django.views.generic import TemplateView
from xhtml2pdf import pisa
from django.http import HttpResponse
from api.models import Api

def MyPDF(request):
 nameOfFormToDisplay = request.POST['nameOfFormToDisplay']
 current_number_to_use = Api.objects.filter(formName=nameOfFormToDisplay)[0].currentNumberToUse
 current_date_to_use = Api.objects.filter(formName=nameOfFormToDisplay)[0].currentDate
 currentApiSelected = Api.objects.filter(formName=nameOfFormToDisplay)[0]
 currentApiSelected.currentNumberToUse = current_number_to_use +1
 currentApiSelected.save()

 templateString = (nameOfFormToDisplay + ".html")
 template = get_template(templateString)
 myContextObject = {'incrementingNumber': current_number_to_use, 'currentDate':current_date_to_use}
 html = template.render(Context(myContextObject))

 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')

pip freeze:

Django==1.8.7
django-easy-pdf==0.1.0
django-wkhtmltopdf==3.0.0
djangorestframework==3.3.1
html5lib==0.9999999
pep8==1.6.2
Pillow==2.9.0
psycopg2==2.6.1
PyPDF2==1.25.1
raven==5.8.1
reportlab==2.7
six==1.10.0
wheel==0.24.0
xhtml2pdf==0.0.6

The pdf is opened in the browser window, not acrobat. I've searched and tried other software like django-wkhtmltopdf and pdfcrowd. I am developing on a mac. I am not sure if there is a specific tag I can place in the pdf to make the 'form field' show up in the pdf.
Any guidance is appreciated. Thanks.

As mentioned in the comments, xhtml2pdf does not support it. The library uses reportlab to generate the PDF file, and reportlab itself does not support it officially. In fact even their commercial version supports only the filling of existing forms .

However, as hinted in this answer , reportlab has some experimental work on this. I checked the current source , and it seems it's still being worked on. So if you (but it seems you don't anymore) or someone else need to do that, you can give it a try. But that will be a lot more complex that turning the HTML into PDF...

Even looking at other libraries, it seems that at the moment there isn't really an open-source / free option to generate editable PDF forms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM