Get started with Python
This guide walks you through the steps to use the Pdftools SDK in a code example with Python.
Try the Pdftools SDK without a license key.
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 -
Install the Pdftools SDK package by running the following command:
pip install https://pdftools-public-downloads-production.s3.eu-west-1.amazonaws.com/productkits/PDFSDK/latest/pdftools_sdk-latest.tar.gz -
Navigate to the
Pdf2ImgSimplecode example:cd sdk-examples-python/Pdf2ImgSimple -
To render the sample PDF file
PdfPrimerWhitePaper.pdfto a multi-page TIFF image format, run:python ./pdf2_img_simple.py PdfPrimerWhitePaper.pdf PdfPrimerWhitePaper.tiff
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 or a file path with the file name.
- Both file paths (input and output) can be relative or absolute.
Review the following snippet with a placeholder and compare it to the last step of the previous procedure:
python ./pdf2_img_simple.py INPUT_PDF IMAGE_OUTPUT_PATH
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
-
Install the Python package:
pip install https://pdftools-public-downloads-production.s3.eu-west-1.amazonaws.com/productkits/PDFSDK/latest/pdftools_sdk-latest.tar.gz -
Clone the sdk-examples-python repository and navigate to any sample directory.
-
Review the
README.mdfile with usage details. Every code example includes aREADME.mdwith different usage instructions. -
Import the following packages in the header of your Python code:
from ctypes import *from pdftools_sdk.pdf import Documentfrom pdftools_sdk.pdf2_image import Converterfrom pdftools_sdk.pdf2_image.profiles import Archive
The imports depend on the Python sample you use. Imports displayed in the last step of the previous procedure are valid for the Convert PDF to image sample. Every sample includes a single Python file from which you can copy the imports. In case you want to use another functionality, copy the correct imports into your project with the following steps:
- In the sdk-examples-python repository, navigate to a sample directory. For example:
sdk-examples-python/Decrypt. - Open the
decrypt.pyfile, and then copy its imports to the header of your Python code.
Initialize the SDK
Try the Pdftools SDK without a license key.
Without a license key, output files carry a watermark. To remove the watermark, initialize the Pdftools SDK with your license key with the following steps:
-
Copy your license key. For more information, review Find the license key.
-
Uncomment the following method, and then replace the
YOUR_LICENSE_KEYwith the value of your license key:# Sdk.initialize("YOUR_LICENSE_KEY")Use the license key in the same format you copied it. Include the less-than (
<) and greater-than (>) signs.
Without a license key, output files carry a watermark. For more information, review Pdftools SDK license management. Without a valid license key, the output files carry a watermark. Contact the Pdftools sales team through the Contact page to get a full license.
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: