Get started with Python
This guide walks you through the steps to use the Pdftools SDK in a code example with Python.
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
-
Clone the sdk-examples-python repository:
git clone https://github.com/pdf-tools/sdk-examples-python.git -
Navigate to
sdk-examples-python/Pdf2ImgSimple, wherepdf2_img_simple.pyresides. -
Run the sample:
python ./pdf2_img_simple.py <input_path> <output_path>For example, to render the sample PDF file
PdfPrimerWhitePaper.pdfto a multi-page TIFF image format, run:python ./pdf2_img_simple.py PdfPrimerWhitePaper.pdf PdfPrimerWhitePaper.tiffnoteRun
python3instead on systems where only Python 3 is installed andpythonis 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.
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
-
Clone the sdk-examples-python repository and navigate to
sdk-examples-python/Pdf2ImgSimple. -
Copy files from the sample into your project’s working directory. The project directory is named here
<project-dir>as a placeholder example:-
Copy
pdf_tools_sdk_ctypes.py,utils.pyandstreams.pyinto<project-dir>. -
Copy the
includefolder into<project-dir>\include. -
Copy the
libfolder containing the native binary files into<project-dir>\lib. -
The project has the following structure:
<project-dir>├── pdf_tools_sdk_ctypes.py├── streams.py├── utils.py├── include└── lib
-
-
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.
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();
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: