Package: django_pdf_generator
django-pdf-generatorConvert HTML to pdf with django using phantomjsRequirementsPython (2.7) (Need to be tested for 3.x)Django (1.10, 1.9) (Need to be tested for previous versions)PhantomJSInstallationInstall usingpip:pip install django_pdf_generatorAddpdf_generatorto your INSTALLED_APPS setting.INSTALLED_APPS=(...'pdf_generator',)Put phantomjs binary on your path or set the path manually in your
settings usingPHANTOMJS_BIN_PATHsettings (see below).ExampleGenerate a PDF with PDFGenerator classGenerate a pdf from an urlfrompdf_generator.generatorsimportPDFGeneratorpdf=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':returnpdf.get_http_response('my_pdf_file')Return a Django HttpResponse with the PDF Attached named
‘my_pdf_file.pdf’:returnpdf.get_http_response('my_pdf_file')Generate a pdf just like Djangorenderfunction :urls.pyurl(r'^invoice$',views.invoice,name='invoice'),views.pyfrompdf_generator.renderersimportrender_pdfdefinvoice(request):"""
    Render an invoice
    The invoice.pdf file is returned
    """returnrender_pdf('invoice',request,'front/invoice.html')Juste add?html=1to the url to view the HTML instead of getting the
pdf file.PDFGeneratoroptionsThePDFGeneratorclass accepts the following arguments :url [required]paperformat [Required] default to ‘A4’, examples: “5in*7.5in”,
“10cm*20cm”, “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 PDFWhen usingsave(filename,title='',description='')method ofPDFGenerator, 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'))SettingsAdd 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_TODefine the directory or the function to be used when saving PDFs,
default topdfs.PHANTOMJS_BIN_PATHDefine the path to Phantomjs binary, default tophantomjs.DEFAULT_RASTERIZE_SCRIPTDefine which render_script to use by default, default torasterize.jsinside the package.DEFAULT_TEMP_DIRDefine the directory to use for temporarily generated pdf by PhantomJS.
default topdf_temp.TEMPLATES_DIRDefine the directory to use for temporarily generated HTML files by
PhantomJS. default topdf_temp.SupportIf you are having issues, please let us know or submit a pull request.LicenseThe project is licensed under the MIT License.