{"id":4201,"library":"pypdftk","title":"PyPDFtk","description":"PyPDFtk is a Python wrapper for the powerful command-line tool PDFtk (PDF Toolkit). It provides a Pythonic interface to manipulate PDF documents, enabling operations such as filling forms with data, concatenating multiple PDFs, splitting a single PDF into individual pages, extracting specific page ranges, replacing pages, generating XFDF data, stamping, and adding backgrounds. The library is currently at version 0.5, with its last release in April 2021, and primarily follows a maintenance release cadence.","status":"active","version":"0.5","language":"en","source_language":"en","source_url":"http://github.com/revolunet/pypdftk","tags":["pdf","pdftk","form-filling","document-processing","wrapper"],"install":[{"cmd":"pip install pypdftk","lang":"bash","label":"Install PyPDFtk"}],"dependencies":[{"reason":"PyPDFtk is a wrapper for the external 'pdftk' binary, which must be installed separately on your system.","package":"pdftk","optional":false}],"imports":[{"symbol":"pypdftk","correct":"import pypdftk"}],"quickstart":{"code":"import pypdftk\nimport os\nimport tempfile\n\n# NOTE: You must have the 'pdftk' binary installed on your system.\n# You may also need to set the PDFTK_PATH environment variable if pdftk is not in your system's PATH.\n# For example: os.environ['PDFTK_PATH'] = '/usr/local/bin/pdftk' on macOS or '/usr/bin/pdftk' on Linux.\n# For Windows, it might be 'C:/Program Files (x86)/PDFtk/bin/pdftk.exe'\n\n# Create a dummy PDF file for demonstration (in a real scenario, this would be an existing PDF form)\n# This example can't create a fillable PDF, so it will demonstrate other functions.\n# We'll use get_num_pages and concat for a runnable example.\n\n# Create a dummy PDF file (simplified for quickstart, typically this would be a pre-existing PDF)\n# In a real scenario, you'd use an actual PDF path.\ndummy_pdf_content = b'%PDF-1.4\\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Count 1/Kids[3 0 R]>>endobj 3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 612 792]/Contents 4 0 R>>endobj 4 0 obj<</Length 16>>stream\\nBT /F1 12 Tf 100 700 Td (Hello PyPDFtk!) Tj ET\\nendstream\\nendobj\\nxref\\n0 5\\n0000000000 65535 f\\n0000000009 00000 n\\n0000000055 00000 n\\n0000000107 00000 n\\n0000000194 00000 n\\ntrailer<</Size 5/Root 1 0 R>>startxref\\n240\\n%%EOF'\n\nwith tempfile.NamedTemporaryFile(suffix=\".pdf\", delete=False) as f1:\n    f1.write(dummy_pdf_content)\n    pdf_path1 = f1.name\n\nwith tempfile.NamedTemporaryFile(suffix=\".pdf\", delete=False) as f2:\n    f2.write(dummy_pdf_content)\n    pdf_path2 = f2.name\n\ntry:\n    # Get number of pages\n    num_pages = pypdftk.get_num_pages(pdf_path1)\n    print(f\"Number of pages in {pdf_path1}: {num_pages}\")\n\n    # Concatenate two dummy PDFs\n    output_concat_pdf = pypdftk.concat([pdf_path1, pdf_path2])\n    print(f\"Concatenated PDFs to: {output_concat_pdf}\")\n\n    # Example of filling a form (requires a real fillable PDF and pdftk to be correctly configured)\n    # To run this, replace 'path/to/model.pdf' with a path to an actual fillable PDF\n    # and ensure PDFTK_PATH is correctly set.\n    # datas = {'firstname': 'John', 'lastname': 'Doe'}\n    # try:\n    #     filled_pdf = pypdftk.fill_form('path/to/model.pdf', datas)\n    #     print(f\"Filled form saved to: {filled_pdf}\")\n    # except Exception as e:\n    #     print(f\"Could not fill form (requires a real fillable PDF and pdftk setup): {e}\")\n\nfinally:\n    # Clean up temporary files\n    os.remove(pdf_path1)\n    os.remove(pdf_path2)\n    if 'output_concat_pdf' in locals() and os.path.exists(output_concat_pdf):\n        os.remove(output_concat_pdf)\n    # if 'filled_pdf' in locals() and os.path.exists(filled_pdf):\n    #     os.remove(filled_pdf)\n","lang":"python","description":"This quickstart demonstrates basic operations like getting the number of pages and concatenating PDFs using `pypdftk`. It highlights the necessity of the `pdftk` binary and provides comments on how to set `PDFTK_PATH`. A form-filling example is commented out as it requires a specific fillable PDF and proper `pdftk` configuration."},"warnings":[{"fix":"Install 'pdftk' (e.g., `sudo apt-get install pdftk` on Debian/Ubuntu, or download from pdflabs.com for other OSes). Ensure it's in your system's PATH, or set `os.environ['PDFTK_PATH'] = '/path/to/pdftk'` in your Python code.","message":"The 'pdftk' binary is a mandatory external dependency and must be installed separately on your operating system. PyPDFtk is merely a Python wrapper and will not function without 'pdftk' being accessible in the system's PATH or explicitly configured via `PDFTK_PATH`.","severity":"breaking","affected_versions":"All versions"},{"fix":"Explicitly set the `PDFTK_PATH` environment variable in your script or shell to point to the `pdftk` executable, e.g., `os.environ['PDFTK_PATH'] = '/usr/local/bin/pdftk'`.","message":"PyPDFtk might not automatically find the 'pdftk' executable on all systems, especially Windows or when 'pdftk' is installed in a non-standard location. This often leads to `FileNotFoundError` or `subprocess.CalledProcessError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always ensure the `out_dir` argument passed to `pypdftk.split()` is an empty directory or let the function use a temporary directory by omitting `out_dir`.","message":"The `pypdftk.split()` function, if provided with an `out_dir` parameter, warns that it 'may destroy your files and return incorrect results' if the specified output directory is not empty.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For operations involving image fields, consider alternative PDF manipulation libraries or tools that explicitly support image embedding, or pre-process PDFs with images before using PyPDFtk for form filling.","message":"PyPDFtk currently does not support handling or inserting image fields into PDF forms. Its form-filling capabilities are limited to text, checkboxes, and radio buttons.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}