Registry / web-framework / django-wkhtmltopdf

django-wkhtmltopdf

JSON →
library3.4.0pypypiunverified

django-wkhtmltopdf is a Django library that allows you to render HTML templates to PDF documents using the external `wkhtmltopdf` utility. It provides class-based views and utility functions for PDF generation within Django projects. The current version is 3.4.0, and it maintains a steady release cadence with a focus on stability and compatibility with newer Django versions.

pip install django-wkhtmltopdf
INSTALL
IMPORT
SIG · DJANGO-WKHTMLTOPDF
D
django-wkhtmltopdf
web-frameworkpythonv3.4.0
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.6s · import 0.000s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PDFTemplateView
from wkhtmltopdf.views import PDFTemplateView
from wkhtmltopdf.views import PDFTemplateView

This quickstart demonstrates how to create a PDF view using `PDFTemplateView`. Ensure the `wkhtmltopdf` binary is installed on your system and its path is set in `WKHTMLTOPDF_CMD` in your Django settings. It renders `hello_pdf.html` into a downloadable PDF.

import os from django.conf import settings from django.urls import path from django.views.generic import TemplateView from wkhtmltopdf.views import PDFTemplateView # Minimal Django settings for demonstration settings.configure( DEBUG=True, INSTALLED_APPS=[ 'django.contrib.staticfiles', 'wkhtmltopdf' ], TEMPLATES=[ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(os.path.dirname(__file__), 'templates')], 'APP_DIRS': True, }, ], STATIC_URL='/static/', WKHTMLTOPDF_CMD=os.environ.get('WKHTMLTOPDF_CMD', '/usr/local/bin/wkhtmltopdf') # IMPORTANT: Set this to your wkhtmltopdf binary path ) # Create a dummy template file for the quickstart os.makedirs(os.path.join(os.path.dirname(__file__), 'templates'), exist_ok=True) with open(os.path.join(os.path.dirname(__file__), 'templates', 'hello_pdf.html'), 'w') as f: f.write('<h1>Hello, PDF!</h1><p>Generated by django-wkhtmltopdf.</p>') urlpatterns = [ path( 'pdf-view/', PDFTemplateView.as_view( template_name='hello_pdf.html', filename='my_document.pdf', header_html='<p>Header</p>', footer_html='<p>Footer</p>', show_as_attachment=True, cmd_options={'enable-local-file-access': True} ), name='pdf_view' ), path( 'html-view/', TemplateView.as_view(template_name='hello_pdf.html'), name='html_view' ) ] # To run this minimal example (for testing): # from django.core.management import execute_from_command_line # if __name__ == '__main__': # execute_from_command_line(['manage.py', 'runserver', '--noreload']) # To access: Navigate to /pdf-view/ after setting up Django and wkhtmltopdf
Debug
Known issues
breakingThe `wkhtmltopdf` binary is no longer automatically downloaded or managed by the library as of version 3.0.0. It must be installed separately on your system.
fix
Manually install the `wkhtmltopdf` executable for your operating system (e.g., via apt, brew, or direct download). Ensure it's in your system's PATH or configure its full path in `settings.WKHTMLTOPDF_CMD`.
affects: >=3.0.0
gotchaFailure to set `WKHTMLTOPDF_CMD` in Django settings or setting it to an incorrect path will result in `OSError: [Errno 2] No such file or directory` or similar errors when attempting to generate a PDF.
fix
Verify the absolute path to your `wkhtmltopdf` binary (e.g., `/usr/local/bin/wkhtmltopdf` on Linux/macOS, `C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe` on Windows) and set `WKHTMLTOPDF_CMD = '/path/to/wkhtmltopdf'` in your `settings.py`.
affects: All
gotchaRelative paths for static files (CSS, images) in your HTML templates may not render correctly in the generated PDF due to `wkhtmltopdf`'s security restrictions or inability to resolve Django's static URLs.
fix
Use absolute URLs for static files (e.g., `{{ request.build_absolute_uri(STATIC_URL) }}path/to/my.css`). For local files, you might need to use `file://` paths directly in your template or pass `cmd_options={'enable-local-file-access': True}` to your view or utility function.
affects: All
gotchaThere are known rendering differences between `wkhtmltopdf` versions. Specifically, `wkhtmltopdf` 0.12.6 is often recommended for stability and feature completeness, while newer versions (e.g., 0.13.0-alpha) might introduce regressions or require different flags.
fix
If experiencing rendering issues, try installing `wkhtmltopdf` version 0.12.6. You may also need to experiment with specific `cmd_options` for `wkhtmltopdf` such as `--enable-javascript`, `--no-stop-slow-scripts`, or `--print-media-type`.
affects: All
Upgrade
Version history
3.4.0latest on PyPI · released Feb 24, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
20 hits · last 30 days
node
20
Resources