Skip to main content
Version: 1.5

Get started with Python

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

info

The Python interface is implemented as a wrapper around the Pdftools SDK's 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 sample project and convert a PDF file to an image.

Download and run the sample

  1. Download a sample project, and then unzip the file.

  2. Navigate to the root directory of the unzipped sample project, 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.

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. Download a Python sample package, and then unzip the file.

  2. Copy files from the sample package 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 *

Initialize the SDK

Initialize the Pdftools SDK with your license key. Substitute the <PDFSDK,V1,include-your-key-here> in the following function with the value of your license key:

# Initialize library
initialize()

# Optional: Without a license key the Pdftools SDK returns watermarked results.
# Initialize the Pdftools SDK with a license key to remove the watermark.
# The Pdftools SDK returns an error if you use an invalid license key.
if sdk_initialize("<PDFSDK,1,insert-license-key-here>", None) == 0:
print_error_message("Failed to set the license key.")
exit(1)
tip

Without a valid license key the output files are watermarked. Get in touch with the Pdftools sales team through the Contact page to get a full license.

Uninitialize the SDK

After processing files with the Pdftools SDK, call the Uninitialize method to unload the library correctly:

# Uninitialize library
uninitialize();
tip

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