Snippet List
Simplified version of the snippet that renders model to PDF [http://djangosnippets.org/snippets/2540/](http://djangosnippets.org/snippets/2540/)
This PDF view mixin for Django Class Based Views.
See working project example: https://github.com/elena/django-pdfmixin-example
---
This is based on the case scenario where you have a model which has a `DetailView`.
You then construct a bespoke PDF for the same model that is unrelated in any way to the `DetailView`.
The PDF needs to be returned as a `HTTPResponse` object. The model object is provided.
- pdf
- mixin
- reportlab
- class-based-views
- cbv
View mixin and utils to generate PDF documents from html using *xhtml2pdf*.
The most interesting thing here is *PDFTemplateResponseMixin*.
Adding this mixin to class based views allows automatic pdf generation using
the view context and a customized template.
There is also the lower level function *render_to_pdf*, similar to what can be
seen in [snippet 659](http://djangosnippets.org/snippets/659/).
See the docstrings for a detailed explanation.
- pdf
- mixin
- class-based-views
This is a django view that can return a PDF made using rml2pdf from reportlab. This RML is written with django templating system, to view the rml code and download a fully working version visit [reportlab](https://www.reportlab.com/software/documentation/sample-projects/rml-with-django/)
This is "pyText2Pdf" - Python script to convert plain text into PDF file.
Originally written by Anand B Pillai.
It is taken from http://code.activestate.com/recipes/189858/
Modified to work with streams.
Example: produce PDF document from text and output it as HTTPResponse object.
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
- django
- tools
- python
- pdf
- converter
- text2pdf
- adobe
- acrobat
- processing
This is form field for PDF or Microsoft Word Document (both .doc and .docx)
It will validate the file uploaded as a valid PDF and MS Word Document.
It extends a forms.FileField, so you can put all the arguments relevant to FileField.
IMPORTANT NOTE: The method of validation is actually run thru *nix OS shell command 'file', therefore,
1. only *nix system can use this class.
2. The file uploaded must be saved on disk, meaning you need to set your upload handler to use TempoaryFileUploadHandler Only.
# (i.e. put this in your settings.py)
FILE_UPLOAD_HANDLERS = (
"django.core.files.uploadhandler.TemporaryFileUploadHandler",
)
- pdf
- microsoft-word-document
- form-field
This should work as a `django.views.generic.list_detail` generic view but will produce PDF version of given template.
This code is merged code from perenzo's [example](http://www.djangosnippets.org/snippets/659/) and code from `django.views.generic.list_detail` module.
`pisa` package is required from (http://www.htmltopdf.org/download.html) with `html5lib` package and Reportlab Toolkit 2.1+
NOTE: this is code for Django 0.96. In Django 1.0 change in line 3: ObjectPaginator to Paginator
- generic-views
- pdf
- html
- css
This is an extract of an example for use of "pisa" <http://www.htmltopdf.org> in "django". It shows the easiest way possible to create PDF documents just using HTML and CSS. In "index" we see the definition of the output of a form in which HTML code can be typed in and then on the fly a PDF will be created. In "ezpdf_sample" we see the use of templates and contexts. So adding PDF to your existing Django project could be just a matter of some lines of code.
You need htmldoc, rst2html, the Python Imaging Libraray, BeautfiulSoup
and spoon.
The Debian/Ubuntu-packages are called htmldoc, python-docutils, python-imaging and python-beautifulsoup
You can get spoon.py http://beautifulspoon.googlecode.com/svn/trunk/spoon.py
To create the pdf files you have to call the script from django_src/docs
Here is an example output: http://henning.cco-ev.de/django/essential.pdf
It allows you to create pdf much like normal html code taking advantage of django's template engine. It requires the trml2pdf module from [OpenReport](http://sourceforge.net/projects/openreport) and can be used like `render_to_response()`. *prompt* specifies if a "save as" dialog should be shown to the user while *filename* is the name that the browser will suggest in the save dialog.
This is an example of how I am providing downloads of dynamic images in either PNG or PDF formats. The PDF format requires ImageMagick's `convert`, and temporary disk space to save the intermediary image. If anyone knows a way to avoid writing to disk, I'd be happy to include it here.
I realize there may be uses where it isn't necessary to use the PIL Image class if the image is already stored as a file. This is used for downloading dynamic images without saving them to disk (unless pdf format is used).
- pdf
- png
- custom-response
- subprocess
**The code is a bit messy and may include bugs ;-) Anyway, I want to clean it up and add features (see below).**
The `process_latex()` function will read the given template, render it with a custom context and convert it into the desired output format by running it through pdflatex.
Depending on the `outfile` parameter, the output will either be returned (for a response object) or written to the path specified in `outfile` (useful for `save()` in models which generate static, unique files).
**TODO**
* Error handling in case pdflatex breaks (non-zero return value)
* Optionally returning a HttpResponse with the correct mimetype and Content-Disposition set to attachement and an appropriate filename
* RequestContext instead of Context (passing the Context object instead of a dict)
**CREDITS**
Credits also go to ronny for the names dict :-P
- template
- django
- pdf
- dvi
- png
- latex
- pdflatex
12 snippets posted so far.