Skip to main content

A simplistic/very extendable pdf renderer.

Project description

DRF PDF Renderer

A simplistic/very extendable pdf renderer.

If you use or like the project, click Star and Watch to generate metrics and i evaluate project continuity.

OBS

This project is a Beta version, however is used in production in a big project (with custom pdf template, and manually fields instead of automatic).
however, due to my low availability, updates may take some time. but i will keep an eye on the PR.

Install:

pip install drf-pdf-renderer

Usage:

  1. Add to your INSTALLED_APPS, in settings.py:

    INSTALLED_APPS = [  
        ...
        'drf_pdf_renderer',
        ...
    ]
    
  2. In your file:

    from rest_framework.settings import api_settings
    from drf_pdf_renderer.renderer import PDFRendererPaginated
    
    class YourView(...)
        renderer_classes = (*api_settings.DEFAULT_RENDERER_CLASSES, PDFRendererPaginated)
        pdf_display_fields = (['id', 'Label for ID'], )  # used only in automatic field (caution: refactor planned in futures versions)
        pdf_display_fields = ''  # contains two built-in templates ['pdf/list_landscape.html', 'pdf/list_portrait.html']
        ...
    ...
    
    
  3. Mixin for paginated results

    • if you have a pagination on DRF, but require a PDF with all registries, you can use this Mixin
    from rest_framework.settings import api_settings
    from drf_pdf_renderer.mixin import PdfAllResultsMixin
    
    class YourView(PdfAllResultsMixin, ...)
        pdf_display_fields = (['id', 'Label for ID'], )  # used only in automatic field (caution: refactor planned in futures versions)
        pdf_display_fields = ''  # contains two built-in templates ['pdf/list_landscape.html', 'pdf/list_portrait.html']
        ...
    ...
    
    

Advanced

  1. Custom PDF Template

    • this project use xhtml2pdf, check documentation of html constructor here.
  2. Changing PDF title

    # First way
    class YourView(...)
        pdf_title = 'My Title'
    
    # Second way
    class YourView(...)
        def pdf_get_title(self, data, context)
            return ''
    
  3. Changing PDF download name

    # First way
    class YourView(...)
        pdf_filename = 'My Title'
    
    # Second way
    class YourView(...)
        def pdf_get_filename(self, pdf, data)
            return ''
    
  4. Custom data to render context

    - By default `data`, `request`, `title` and `fields` will always be present (but can be rewrited)
    # Second way
    class YourView(...)
        def pdf_mount_context(data)
            return {'adm': True}
    

Project details


Download files

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

Source Distribution

drf-pdf-renderer-0.1.2.tar.gz (8.1 kB view hashes)

Uploaded Source

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