Skip to main content

Convert HTML to pdf with django using nightmarejs

Project description

nightmare-pdf v0.1.10 on PyPi MIT license Stable

django-nightmare-pdf

Convert HTML to pdf with django using nightmare

Requirements

  • Python (2.7) (Need to be tested for 3.x)

  • Django (1.10, 1.9) (Need to be tested for previous versions)

Dependencies

This django app depends on Nightmare, you need to first install it using npm:

npm install nightmare

Installation

Install using pip :

pip install nightmare_pdf

Add nightmare_pdf to your INSTALLED_APPS setting.

INSTALLED_APPS = (
    ...
    'nightmare_pdf',
)

create a directory to hold pdf files created by Nightmare, default to pdf_temp :

mkdir pdf_temp

Example

Generate a pdf from an url and save it to database, or retrieve it as a ContentFile, or return it inside an HttpResponse :

from nightmare_pdf.generators import PDFGenerator

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

# Save it to database and retrieve a PdfDoc Object (database):
pdf.save(
        filename='nightmare_pdf',
        title="nightmare_pdf on github",
        description="Convert HTML to pdf with django using nightmare")

# 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')

PDFGenerator options

The PDFGenerator class accepts the following arguments :

  • url [required]

  • timeout [Optional] default to 1000, defines the timeout between the opening and the rendering of the url by nightmare

  • page_size [Optional] default to ‘A4’, accepts options are A3, A4, A5, Legal, Letter or Tabloid

  • landscape [Optional] default to 0, defines whether rendering pdf in landscape mode

  • print_background [Optional] default to 1, defines whether printing background

  • margins_type [Optional] default to 1, defines which margins to use. Uses 0 for default margin, 1 for no margin, and 2 for minimum margin.

  • script [Optional] default to DEFAULT_RENDER_SCRIPT, defines which render script to use.

  • temp_dir [Optional] default to DEFAULT_TEMP_DIR, defines which temp dir to use.

Model use 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 :

NIGHTMARE_PDF = {
    'UPLOAD_TO': 'pdfs',
    'NODE_PATH': 'node',
    'DEFAULT_RENDER_SCRIPT': os.path.join(NIGHTMARE_PDF_DIR, 'render_pdf.js'),
    'DEFAULT_TEMP_DIR': os.path.join(settings.BASE_DIR, 'pdf_temp')
}

UPLOAD_TO

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

NODE_PATH

Define the path to Node binary, default to node.

DEFAULT_RENDER_SCRIPT

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

DEFAULT_TEMP_DIR

Define the directory to use for temporarily generated pdf by Nightmare. 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

nightmare_pdf-0.1.0-py2.py3-none-any.whl (11.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file nightmare_pdf-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for nightmare_pdf-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cbd7847f065dce5ab3fc56fbc0b9df7a2eada51866c129780ab5636ed247e7b2
MD5 1727eefc4be1761e5c7893a1d4bab90d
BLAKE2b-256 af83979ab0df19013cedf8e166a681fbb01068fe3baed764f5c4907a92dab6cc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page