I'm using pdftk and python to pass http post data to a request handler that generates and returns a pdf file as an http response.
Here is my current code:
import os
from fdfgen import forge_fdf
from django.views.generic.edit import FormView
from django.http import HttpResponse
from myapp.forms import PdfForm
class PdfAutofillFormView(FormView):
form_class = PdfForm
def form_valid(self, form):
fields = [('first_field', form.cleaned_data.get('blah', '')),
('second_field', form.cleaned_data.get('blahblah', '')),]
fdf = forge_fdf("", fields, [], [], [])
fdf_file = open("/tmp/data.fdf", "wb")
fdf_file.write(fdf)
fdf_file.close()
cmd = 'pdftk %s fill_form %s output %s dont_ask' % \
('/home/myuser/original.pdf',
'/tmp/data.fdf',
'/tmp/output.pdf',)
os.system(cmd)
return HttpResponse(open('/tmp/output.pdf', 'r'),
mimetype="application/pdf")
Although this works, it creates two tmp files on disk (data.fdf and output.pdf).
I'm looking for a way to pipe data.fdf and output.pdf to in memory files (not on disk).