{"id":9689,"library":"django-wkhtmltopdf","title":"django-wkhtmltopdf","description":"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.","status":"active","version":"3.4.0","language":"en","source_language":"en","source_url":"https://github.com/incuna/django-wkhtmltopdf","tags":["django","pdf","html-to-pdf","wkhtmltopdf","reporting"],"install":[{"cmd":"pip install django-wkhtmltopdf","lang":"bash","label":"Install Python package"}],"dependencies":[],"imports":[{"symbol":"PDFTemplateView","correct":"from wkhtmltopdf.views import PDFTemplateView"},{"symbol":"PDFView","correct":"from wkhtmltopdf.views import PDFView"},{"symbol":"wkhtmltopdf","correct":"from wkhtmltopdf.utils import wkhtmltopdf"}],"quickstart":{"code":"import os\nfrom django.conf import settings\nfrom django.urls import path\nfrom django.views.generic import TemplateView\nfrom wkhtmltopdf.views import PDFTemplateView\n\n\n# Minimal Django settings for demonstration\nsettings.configure(\n    DEBUG=True,\n    INSTALLED_APPS=[\n        'django.contrib.staticfiles',\n        'wkhtmltopdf'\n    ],\n    TEMPLATES=[\n        {\n            'BACKEND': 'django.template.backends.django.DjangoTemplates',\n            'DIRS': [os.path.join(os.path.dirname(__file__), 'templates')],\n            'APP_DIRS': True,\n        },\n    ],\n    STATIC_URL='/static/',\n    WKHTMLTOPDF_CMD=os.environ.get('WKHTMLTOPDF_CMD', '/usr/local/bin/wkhtmltopdf') # IMPORTANT: Set this to your wkhtmltopdf binary path\n)\n\n# Create a dummy template file for the quickstart\nos.makedirs(os.path.join(os.path.dirname(__file__), 'templates'), exist_ok=True)\nwith open(os.path.join(os.path.dirname(__file__), 'templates', 'hello_pdf.html'), 'w') as f:\n    f.write('<h1>Hello, PDF!</h1><p>Generated by django-wkhtmltopdf.</p>')\n\nurlpatterns = [\n    path(\n        'pdf-view/', \n        PDFTemplateView.as_view(\n            template_name='hello_pdf.html', \n            filename='my_document.pdf',\n            header_html='<p>Header</p>',\n            footer_html='<p>Footer</p>',\n            show_as_attachment=True,\n            cmd_options={'enable-local-file-access': True}\n        ),\n        name='pdf_view'\n    ),\n    path(\n        'html-view/', \n        TemplateView.as_view(template_name='hello_pdf.html'), \n        name='html_view'\n    )\n]\n\n# To run this minimal example (for testing):\n# from django.core.management import execute_from_command_line\n# if __name__ == '__main__':\n#     execute_from_command_line(['manage.py', 'runserver', '--noreload'])\n\n# To access: Navigate to /pdf-view/ after setting up Django and wkhtmltopdf\n","lang":"python","description":"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."},"warnings":[{"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`.","message":"The `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.","severity":"breaking","affected_versions":">=3.0.0"},{"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`.","message":"Failure 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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"Relative 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.","severity":"gotcha","affected_versions":"All"},{"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`.","message":"There 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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install `wkhtmltopdf` and ensure `WKHTMLTOPDF_CMD` in `settings.py` points to its correct absolute path. Verify the binary is executable.","cause":"The `wkhtmltopdf` binary is not found at the path specified in `WKHTMLTOPDF_CMD` or is not in the system's PATH.","error":"OSError: [Errno 2] No such file or directory: 'wkhtmltopdf'"},{"fix":"Check the `Output:` message for more details. Ensure all URLs in your HTML are accessible. Verify static files are correctly referenced. Try adding `--enable-javascript`, `--no-stop-slow-scripts` to `cmd_options` to diagnose JavaScript issues.","cause":"This generic error indicates `wkhtmltopdf` failed during processing. Common causes include inaccessible URLs (if using remote HTML), missing assets (CSS/images), JavaScript errors, or malformed HTML.","error":"wkhtmltopdf exited with non-zero code 1. Output: ... Error: Failed to load URL ... (exit code: 1)"},{"fix":"Ensure you are importing `wkhtmltopdf` from `wkhtmltopdf.utils` and calling it correctly, or using `PDFTemplateView` for class-based views. Do not try to execute the binary directly from Python in this manner.","cause":"You might be trying to call `wkhtmltopdf` as a function directly from a file handle or a non-callable object, or mistaking the utility for a Python function.","error":"TypeError: 'file' object is not callable"},{"fix":"Ensure `'django.contrib.staticfiles'` is included in your `INSTALLED_APPS` in `settings.py`. Double-check `{% load static %}` at the top of your template and correct usage of `{% static 'path/to/file.css' %}`.","cause":"This error occurs when the template renderer can't find the `staticfiles` app, usually because it's missing from `INSTALLED_APPS` or the template tags are incorrect.","error":"django.template.exceptions.TemplateSyntaxError: Invalid block tag on line X: 'load static', expected 'empty' or 'endif'"}]}