Skip to content

ianvieira/django-pdf-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

django-pdf-generator v0.1.3 on PyPi MIT license Stable

django-pdf-generator

Convert HTML to pdf with django using phantomjs

Requirements

  • Python (2.7) (Need to be tested for 3.x)
  • Django (1.10, 1.9) (Need to be tested for previous versions)
  • PhantomJS

Installation

Install using pip :

pip install django_pdf_generator

Add pdf_generator to your INSTALLED_APPS setting.

    INSTALLED_APPS = (
        ...
        'pdf_generator',
    )

Add the pdf_generator urls below to your main urls.py

    urlpatterns = [
    ...
    url(r'^pdf-generator/', include('pdf_generator.urls', namespace='pdf_generator')),
    ...

Put phantomjs binary on your path or set the path manually in your settings using PHANTOMJS_BIN_PATH settings (see below).

Example

Generate a PDF with PDFGenerator class

Generate a pdf from an url

	from pdf_generator.generators import PDFGenerator

	pdf = PDFGenerator(url="https://github.com/charlesthk/django-pdf-generator",

Save it to the database using PdfDoc models :

    pdf.save(
            filename='pdf_generator',
            title="pdf_generator on github",
            description="Convert HTML to pdf with django using phantomjs")

Get the PDF as a Django ContentFile named 'my_pdf_file.pdf' :

    pdf_content_file = pdf.get_content_file('my_pdf_file') 

    # Return a Django HttpResponse with the PDF Attached named 'my_pdf_file.pdf':
    return pdf.get_http_response('my_pdf_file')

Return a Django HttpResponse with the PDF Attached named 'my_pdf_file.pdf':

    return pdf.get_http_response('my_pdf_file')

Generate a pdf just like Django render function :

urls.py

    url(r'^invoice$', views.invoice, name='invoice'),

views.py

    from pdf_generator.renderers import render_pdf

    def invoice(request):
        """
        Render an invoice
        The invoice.pdf file is returned
        """
        return render_pdf('invoice', request, 'front/invoice.html')

Juste add ?html=1 to the url to view the HTML instead of getting the pdf file.

PDFGenerator options

The PDFGenerator class accepts the following arguments :

  • url [required]
  • paperformat [Required] default to 'A4', examples: "5in7.5in", "10cm20cm", "A4", "Letter"
  • zoom [Optional] default to 1.
  • script [Optional] default to DEFAULT_RASTERIZE_SCRIPT, defines which render script to use.
  • temp_dir [Optional] default to DEFAULT_TEMP_DIR, defines which temp dir to use.

Model used for saving PDF

When using save(filename, title='', description='') method of PDFGenerator, the following model is used:

class PdfDoc(models.Model):
	"""
	Store each generated pdf
	"""
	title = models.CharField(verbose_name=_("Title"), max_length=255, blank=True)
	description = models.TextField(verbose_name=_("Description"), blank=True)
	document = models.FileField(verbose_name=_("Document PDF"), upload_to=pdf_settings.UPLOAD_TO)
	created_at = models.DateTimeField(auto_now=False, auto_now_add=True, verbose_name=_('Creation'))
	updated_at = models.DateTimeField(auto_now=True, auto_now_add=False, verbose_name=_('Update'))

Settings

Add your settings to your main django settings file. The settings are set by default to :

PDF_GENERATOR = {
    'UPLOAD_TO': 'pdfs',
    'PHANTOMJS_BIN_PATH': 'phantomjs',
    'DEFAULT_RASTERIZE_SCRIPT': os.path.join(PDF_GENERATOR_DIR, 'rasterize.js'),
    'DEFAULT_TEMP_DIR': os.path.join(PDF_GENERATOR_DIR, 'temp'),
    'TEMPLATES_DIR': os.path.join(PDF_GENERATOR_DIR, 'templates/pdf_generator')
}

UPLOAD_TO

Define the directory or the function to be used when saving PDFs, default to pdfs.

PHANTOMJS_BIN_PATH

Define the path to Phantomjs binary, default to phantomjs.

DEFAULT_RASTERIZE_SCRIPT

Define which render_script to use by default, default to rasterize.js inside the package.

DEFAULT_TEMP_DIR

Define the directory to use for temporarily generated pdf by PhantomJS. default to pdf_temp.

TEMPLATES_DIR

Define the directory to use for temporarily generated HTML files by PhantomJS. default to pdf_temp.

Support

If you are having issues, please let us know or submit a pull request.

License

The project is licensed under the MIT License.

About

Convert HTML to pdf with django using phantomjs

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published