Skip to main content

Get started with Python

This guide walks you through the steps to use the Toolbox add-on in a code example with Python.

info

The Python interface wraps the Toolbox add-on C API. For more details about specific functions and properties, review C API reference documentation.

Request trial or full license

Unlike the Pdftools SDK or the Pdftools SDK Shell Tool, the Toolbox add-on requires a license key for both evaluation and full use. To request a license key, follow these steps:

  1. Contact the sales team through the Contact page and mark the Toolbox add-on as the product of your interest for a trial license.

If you already have a license key and you need to copy it, review Find the license key.

Prerequisites

The Toolbox add-on for Python requires Python 3.6 or higher.

Get started with a sample project

Learn how to use the Toolbox add-on using a Python code example. The AddText example adds text to a PDF. Use similar steps to run any other code example from the toolbox-examples-python repository.

Clone and run the sample

  1. Clone the toolbox-examples-python repository:

    git clone https://github.com/pdf-tools/toolbox-examples-python.git
  2. Navigate to the AddText code example:

    cd toolbox-examples-python/AddText
  3. Find your license key. For more information, review Find the license key.

  4. In the add_text.py file, replace the "insert-license-key-here" placeholder in the Sdk.initialize method call with your license key:

    Sdk.initialize("insert-license-key-here", None)

    Use the license key in the same format you copied it. Include the less-than (<) and greater-than (>) signs.

  5. Install the package for the Toolbox add-on by executing:

    pip install pdftools_toolbox
  6. To add the text string “Hello World!” to the first page of the PDF file BlankNoneNoTP.pdf, run:

    python ./add_text.py BlankNoneNoTP.pdf "Hello World!" output.pdf
    note

    Run python3 instead on systems where only Python 3 is installed and python is not aliased.

The code example takes:

  • The input and output files represented as a file name, a file path with the file name, or the output directory.
  • Both file paths (input and output) can be relative or absolute.
  • The text to be added to the output PDF, written between double quotation marks.

Review this snippet with a placeholder and compare it to the last step of the previous procedure:

python ./add_text.py INPUT_FILE "TEXT_TO_BE_ADDED" OUTPUT_FILE

Integrate the Toolbox add-on into your application

Integrate and initialize the Toolbox add-on into your application by following the instructions in the next sections.

Add the Toolbox add-on to your project

To add the Toolbox add-on to your project, follow these steps:

  1. Install the package for the Toolbox add-on by executing:

    pip install pdftools_toolbox
  2. Clone the toolbox-examples-python repository and navigate to any sample directory.

  3. Review the README.md file with usage details. Every code example includes a README.md with different usage instructions.

  4. Import these packages in the header of your Python code:

    from pdftools_toolbox.sdk import Sdk
    from pdftools_toolbox.geometry.real import Point
    from pdftools_toolbox.pdf import Document, FileReference, Metadata, PageCopyOptions, Page, PageList
    from pdftools_toolbox.pdf.content import Font, ContentGenerator, IccBasedColorSpace, Text, TextGenerator
    from pdftools_toolbox.pdf.navigation import ViewerSettings
Imports depend on the sample you use

The imports depend on the Python sample you use. Imports displayed in the last step of the previous procedure are valid for the Add text to PDF sample. Every sample includes a single Python file from which you can copy the imports. To use a different feature, copy the correct imports into your project:

  1. In the toolbox-examples-python repository, navigate to a sample directory. For example: toolbox-examples-python/MultipleUp.
  2. Open the multiple_up.py file, and then copy its imports to the header of your Python code.

Initialize the Toolbox add-on

The Toolbox add-on always requires a license key to operate (unlike the Pdftools SDK). Without a valid license, every Toolbox add-on method call fails with LicenseError: No license key was set.

Getting a license key

Contact the Pdftools sales team through the Contact page to get a trial or full license.

To initialize the Toolbox add-on with your license key, follow these steps:

  1. Find your license key. For more information, review Find the license key.

  2. Call Sdk.initialize once at application startup, before any other Toolbox add-on method call:

    Sdk.initialize("YOUR_LICENSE_KEY", None)

    Replace YOUR_LICENSE_KEY with your license key. Use the license key in the same format you copied it. Include the less-than (<) and greater-than (>) signs.

To get a working reference with the Sdk.initialize method, follow these steps:

  1. Clone the toolbox-examples-python repository and navigate to a sample directory. For example, toolbox-examples-python/AddText.
  2. Open the main Python file and find the Sdk.initialize call. For example, the AddText Python sample includes it in add_text.py.
  3. Replace the "insert-license-key-here" placeholder with your license key.

Implement your use case

Find other code examples that show you how to implement specific use cases with the Toolbox add-on using Python. Review the Toolbox add-on code examples page.