Get started with Python
This guide walks you through the steps to use the Toolbox add-on in a code example with Python.
The Python interface wraps the Toolbox add-on C API. For more details about specific functions and properties, review 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:
- Contact 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.
Get started with a sample project
Learn how to use the Toolbox add-on using a Python code example. The AddText example adds text to a PDF. Use similar steps to run any other code example from the toolbox-examples-python repository.
Clone and run the sample
-
Clone the toolbox-examples-python repository:
git clone https://github.com/pdf-tools/toolbox-examples-python.git -
Navigate to the
AddTextcode example:cd toolbox-examples-python/AddText -
Find your license key. For more information, review Find the license key.
-
In the
add_text.pyfile, replace the"insert-license-key-here"placeholder in theSdk.initializemethod call with your license key:Sdk.initialize("insert-license-key-here", None)Use the license key in the same format you copied it. Include the less-than (
<) and greater-than (>) signs. -
Install the package for the Toolbox add-on by executing:
pip install pdftools_toolbox -
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.pdfnoteRun
python3instead on systems where only Python 3 is installed andpythonis not aliased.
The code example 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 this 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 pdftools_toolbox -
Clone the toolbox-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 these packages in the header of your Python code:
from pdftools_toolbox.sdk import Sdkfrom pdftools_toolbox.geometry.real import Pointfrom pdftools_toolbox.pdf import Document, FileReference, Metadata, PageCopyOptions, Page, PageListfrom pdftools_toolbox.pdf.content import Font, ContentGenerator, IccBasedColorSpace, Text, TextGeneratorfrom 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. To use a different feature, copy the correct imports into your project:
- In the toolbox-examples-python repository, navigate to a sample directory. For example:
toolbox-examples-python/MultipleUp. - Open the
multiple_up.pyfile, 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, every Toolbox add-on method call fails with LicenseError: No license key was set.
Contact the Pdftools sales team through the Contact page to get a trial or full license.
To initialize the Toolbox add-on with your license key, follow these steps:
-
Find your license key. For more information, review Find the license key.
-
Call
Sdk.initializeonce at application startup, before any other Toolbox add-on method call:Sdk.initialize("YOUR_LICENSE_KEY", None)Replace
YOUR_LICENSE_KEYwith your license key. Use the license key in the same format you copied it. Include the less-than (<) and greater-than (>) signs.
To get a working reference with the Sdk.initialize method, follow these steps:
- Clone the toolbox-examples-python repository and navigate to a sample directory. For example,
toolbox-examples-python/AddText. - Open the main Python file and find the
Sdk.initializecall. For example, the AddText Python sample includes it inadd_text.py. - Replace the
"insert-license-key-here"placeholder with your license key.
Implement your use case
Find other code examples that show you how to implement specific use cases with the Toolbox add-on using Python. Review the Toolbox add-on code examples page.