Get started with Python
This guide walks you through the steps to use the Toolbox add-on in a sample project with Python.
The Python interface is implemented as a wrapper around the Toolbox add-on C API. For more details about specific functions and properties, review the 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:
- Reach out to 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.
Getting started with a sample project
Learn how to use the Toolbox add-on using a Python sample project and add text to a PDF.
Download and run the sample
-
Download a sample project, and then unzip the file.
-
Locate your license key. For more information, review Find the license key.
-
In the code sample folder, open the Python file (for example
add_text.py
). -
In the
add_text.py
file, replace the string"insert-license-key-here"
with your license key:# Set and check license key. If the license key is not valid, an exception is thrown.
Sdk.initialize("insert-license-key-here", None)Use the license key in the same format as you copied it. Include the less-than (
<
) and greater-than (>
) signs. -
Install the package for the Toolbox add-on by executing:
pip install https://pdftools-public-downloads-production.s3.eu-west-1.amazonaws.com/productkits/PDFSDKXT/latest/pdftools_toolbox-latest.tar.gz
-
In the command line, navigate to the root directory of the unzipped sample project, where
add_text.py
resides. -
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
noteRun
python3
instead on systems where only Python 3 is installed andpython
is not aliased.
The code sample 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 the following 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:
-
Install the package for the Toolbox add-on by executing:
pip install https://pdftools-public-downloads-production.s3.eu-west-1.amazonaws.com/productkits/PDFSDKXT/latest/pdftools_toolbox-latest.tar.gz
-
From the Toolbox add-on code samples page, download any Python code sample.
-
Unzip a code sample and review the
README.md
file with usage details. Every code sample includes aREADME.md
with different usage instructions. -
Import the following 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
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. In case you want to use another functionality, copy the correct imports into your project with the following steps:
- Open the Toolbox add-on code samples.
- Download a sample. For example: Place multiple pages on one page
- Unzip the downloaded sample.
- 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 key the Toolbox add-on returns an error. Get in touch with the Pdftools sales team through the Contact page to get a full license.
To initialize the Toolbox add-on with your license key, follow these steps:
-
Locate your license key. For more information, review Find the license key.
-
Before you call any function of the Toolbox add-on, first call the
Sdk.initialize
method.Sdk.initialize("insert-license-key-here", None)
Replace
insert-license-key-here
with the value of your license key. Use the license key in the same format as you copied it. Include the less-than (<
) and greater-than (>
) signs.
To get a code sample with the Sdk.Initialize
method that you can use as a reference for your code, follow these steps:
- On the Code samples page, download and unzip a Python code sample. For example: Download Add text to PDF sample.
- Unzip the file, and then find the
Sdk.initialize
method in the Python file of the sample. For example: Add text to PDF includes theSDK.initialize
method in theadd_text.py
file. - Replace the
insert-license-key-here
with your license key.
Implement your use case
Download other sample projects that show you how to implement specific use cases with the Toolbox add-on using Python. Review the Toolbox add-on code samples page.