Skip to main content
Version: Version 1.6

Get started with Python

This guide walks you through the steps to use the Pdftools SDK in a code example with Python.

info

The Python interface wraps the Pdftools SDK C API.

Prerequisites​

The Pdftools SDK for Python requires Python 3.6 or higher.

Getting started with a sample project​

Learn how to use the Pdftools SDK using a Python code example. The Pdf2ImgSimple example converts a PDF file to an image. Use similar steps to run any other code example from the sdk-examples-python repository.

Clone and run the sample​

  1. Clone the sdk-examples-python repository:

    git clone https://github.com/pdf-tools/sdk-examples-python.git
  2. Navigate to sdk-examples-python/Pdf2ImgSimple, where pdf2_img_simple.py resides.

  3. Run the sample:

    python ./pdf2_img_simple.py <input_path> <output_path>

    For example, to render the sample PDF file PdfPrimerWhitePaper.pdf to a multi-page TIFF image format, run:

    python ./pdf2_img_simple.py PdfPrimerWhitePaper.pdf PdfPrimerWhitePaper.tiff
    note

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

The sample runs without a license key, and the output carries a watermark. To remove the watermark, open pdf2_img_simple.py and uncomment the Sdk.initialize method call in the if __name__ == "__main__": block. Replace the "insert-license-key-here" placeholder with your license key. Include the less-than (<) and greater-than (>) signs.

note

The Pdftools SDK code samples on GitHub are not versioned per SDK release — they always reflect the latest SDK API. The integration instructions in the next section describe the v1.6-specific ctypes workflow, which differs from the modern Sdk.initialize class method used in the cloned sample.

Integrate the SDK into your application​

Integrate and initialize the Pdftools SDK into your application by following the instructions in the next sections.

Add the SDK to your project​

  1. Clone the sdk-examples-python repository and navigate to sdk-examples-python/Pdf2ImgSimple.

  2. Copy files from the sample into your project’s working directory. The project directory is named here <project-dir> as a placeholder example:

    1. Copy pdf_tools_sdk_ctypes.py, utils.py and streams.py into <project-dir>.

    2. Copy the include folder into <project-dir>\include.

    3. Copy the lib folder containing the native binary files into <project-dir>\lib.

    4. The project has the following structure:

      <project-dir>
      ├── pdf_tools_sdk_ctypes.py
      ├── streams.py
      ├── utils.py
      ├── include
      └── lib
  3. Import the following packages in the header of your Python code:

    from ctypes import *
    from streams import *
    from pdf_tools_sdk_ctypes import *

Optional: Initialize the SDK​

The Pdftools SDK runs without an explicit sdk_initialize call by auto-activating an internal test license that watermarks the output. Call sdk_initialize with your license key to remove the watermark.

Getting a license key

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

Call sdk_initialize once at application startup, before any other Pdftools SDK function call:

# Initialize library
initialize()

# Set the license key to remove the watermark.
if sdk_initialize("YOUR_LICENSE_KEY", None) == 0:
print_error_message("Failed to set the license key.")
exit(1)

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

The Pdf2ImgSimple Python sample uses the modern Sdk.initialize class method, not the v1.6 ctypes API described above. The sample is not a working reference for v1.6 integration code.

Uninitialize the SDK​

After processing files with the Pdftools SDK, call the uninitialize function to unload the library correctly:

# Uninitialize library
uninitialize();
tip

Find other code examples that show you how to implement specific use cases with the Pdftools SDK using Python. See our Code samples page. The samples include: