In current project we have a task to produce PDF documents. This is various 1-page reports with a piece of textual information. The thing, common for all this documents: they contain only text. In general we take some text from database, produce PDF and output it to user.
Django and Python have a rich bucket of powerful tools to produce PDFs: Reportlab, PDFlib, Pisa HTML2PDF, forge_fdf in Python etc. This tools allow to create complex PDF documents with images and forms.
But we found an easier way that meets our task. My friend has shown me a link with code snippet of "pyText2Pdf". This is a command-line tool to convert plain text into PDF files. It is really easy tool. The surprise - it's fully written in Python! I have done some modifications and it become usable with Django.
The way of producing PDF is quite simple: you take a text and pyText2Pdf returns you file-like object with PDF data. Here is an example:
import StringIO
input_stream = StringIO.StringIO(text)
result = StringIO.StringIO()
pdfclass = pyText2Pdf(input_stream, result, "PDF title")
pdfclass.Convert()
response = HttpResponse(result.getvalue(), mimetype="application/pdf")
response['Content-Disposition'] = 'attachment; filename=pdf_report.pdf'
return response
-------------------------------------
pyText2Pdf on Django Snippets web-site: http://www.djangosnippets.org/snippets/1778/
