Skip to main content

Django app for seamlessly displaying and downloading PDF documents in browser

Project description

Django PDF View

Build Status PyPI version License GitHub stars GitHub issues codecov

Purpose

The primary purpose of this Django package is to streamline the creation of PDF documents and allow for easy viewing or downloading in the browser. The package provides a solid foundation of HTML and CSS for PDF file layout. To use it, simply create an HTML template for your PDF document, then create a view that inherits from PDFView and define the necessary class attributes to generate the PDF.

This approach enables developers to focus on crafting the content and styles of their PDF pages without having to deal with complex layout issues.

Key Features

  • Predefined Page Layout: Includes built-in base HTML and CSS to help structure your PDF pages efficiently.

  • Flexible PDF Content and Styling: Easily create your own HTML template for the PDF document and customize the appearance by applying your own CSS.

  • PDF View Response Options: Seamlessly switch between viewing the PDF, displaying the HTML content, and downloading the PDF file.

Prerequisites

Installation

pip install django-pdf-view

or

poetry add django-pdf-view

Configuration

Add django-pdf-view to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    ...
    'pdf_view',
    ...
]

Usage

In order to create your own PDF, you need to implement your own view and template.

  1. Create a new view to render the PDF:

    # my_app/views.py    
        
    from pdf_view.views.pdf_view import PDFView
        
        
    class MyPDFView(PDFView):
        template_name = 'my_app/my_pdf.html'
        title = 'My PDF Document' # optional
        filename = 'My PDF.pdf' # optional
        css_paths = [ # optional
            'my_pdf/css/my_pdf.css',
        ]
    
  2. Create a new template to define the content of your PDF page:

    <!-- my_app/templates/my_app/pdf_page.html -->
    
    <div class="page">
         <h1 class="my-title">{{ title }}</h1>
         <p class="my-text">{{ text }}</p>
         <p class="my-text">Additional PDF page text.</p>
    </div>
    
    <!-- Define more <div class="page"> elements for additional pages -->
    
  3. Add the new URL patterns to your project's urls.py:

    # my_app/urls.py    
    
    from django.urls import path
    from my_app.views import MyPDFView
    
    urlpatterns = [
        path('pdf-file/', MyPDFView.as_view(response_type='pdf'), name='pdf-file'),
        path('pdf-html/', MyPDFView.as_view(response_type='html'), name='pdf-html'),
        path('pdf-download/', MyPDFView.as_view(response_type='download'), name='pdf-download'),
    ]
    

  4. Visit the belows listed URLs:

Default template context

  • PDF document:
    • title: Title of the PDF document (used in <title> tag).
    • css: Content of the CSS files specified in the css_paths attribute.
    • content: Content of the HTML template.
    • response_type: Type of the response (pdf, html or download).

svg template tag

svg template tag is provided by the django-pdf-view package. This template tag can be used to include SVG images in the PDF document.

Usage example:

{% load svg %}

<!-- Some HTML content -->

{% svg 'path/to/image.svg' %}

<!-- Some more HTML content -->

You can also dynamically set a custom color to the SVG by adding data-dynamic-color="true" to elements in the SVG file that contain the fill attribute.

Example:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
    <path fill="white" data-dynamic-color="true" .../>
</svg>

and then in the template:

{% load svg %}

<!-- Some HTML content -->

{% svg 'path/to/image.svg' 'black' %}

<!-- Some more HTML content -->

In the above example, the fill color of the SVG will be changed from white to black.

Bugs/Requests

If you encounter any bugs or have any requests, please use GitHub issue tracker to report them.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

django_pdf_view-3.1.3.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pdf_view-3.1.3-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file django_pdf_view-3.1.3.tar.gz.

File metadata

  • Download URL: django_pdf_view-3.1.3.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/6.8.0-57-generic

File hashes

Hashes for django_pdf_view-3.1.3.tar.gz
Algorithm Hash digest
SHA256 3dda4a5b909bde6a86ab1dcb8b683e77184eb6afdf42c6a90380d940fd3ef490
MD5 c0e3c04eb24c423fa3f19465c06b368f
BLAKE2b-256 236330df22097e6692b73ab5275647f51f3e5ec25a6344540d1ae6dbf2aa97f0

See more details on using hashes here.

File details

Details for the file django_pdf_view-3.1.3-py3-none-any.whl.

File metadata

  • Download URL: django_pdf_view-3.1.3-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.3 Linux/6.8.0-57-generic

File hashes

Hashes for django_pdf_view-3.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cc532d30d79b0e8d6ff68c24b853837abded69d58583341978ac0425758306a1
MD5 60139428da4044f6d93f08dc0c6907c7
BLAKE2b-256 f2d465baeb59b1dd42d80144b11651204481d7d9e7bb0372bb2c0f379a9d1991

See more details on using hashes here.

Supported by

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