VOOZH about

URL: https://docs.apryse.com/core/guides/get-started/python3


Product:

Section:

Product:

Release notes
Viewer
Basic operations
Learn more
Annotation
MS Office
Generate via template
Conversion
Smart Data Extraction
Augmenting LLMs with Smart Data Extraction
PDF/A
Accessibility
Forms
Create
Page manipulation
PDF Editing
OCR
Digital signature
Search
Comparison
Bookmark
Optimization
Layer (OCG)
Print
Redaction
Sanitization
Security
Portfolio
Low-level PDF API
Handwriting ICR
Changelogs

Get Started with the Apryse Server SDK Python 3 PDF Library Integration

Use our Apryse Server SDK with Python3 and pip3. We include Python examples to integrate Server.

The Apryse Server SDK is cross-platform and supported on Windows, Linux, and macOS. The base package is robust, allowing you to programmatically do the following with PDFs:

Additional functionality

If you’re looking for additional capabilities, we offer add-on packages, such as Smart Data Extraction, and add-on modules, such as Office Conversion. For a complete list of add-ons, refer to the following:

Solutions and benefits

Using the Server SDK allows you to build server-side document processing solutions at scale without relying on client devices or cloud APIs. Since the Server runs entirely on-premises (or in your private cloud), you can ensure security and compliance with full control over access, storage, and encryption. You can also easily integrate with web apps, backend systems, document management systems, and content workflows.

Steps and samples

This guide walks you through steps to work through samples we give you, create a project, and integrate the Apryse Server SDK free trial into your project. We’ll use a Python project as our example project throughout this guide. By the end, you’ll have run through at least one sample, created a project, and built an "Apryse Hello World" within your application.

How to use Apryse with Python

There are two ways to use Apryse with Python:

  • Use pip3 which installs third party Python packages (for example, the Apryse SDK for Python 3). It’s the precompiled library for Python 3.x.
  • Use PDFNet bindings to build your own wrappers (less common).

This guide will get you started using the precompiled Python for 3.x through pip3.

Choose your preferred platform from the tabs below.

Precompiled Python3 and macOS PDF library integration

You'll install or update pip3 and Python3, then get started with our samples to see the output you can create when you integrate the Apryse Server SDK into your application. Last, you'll create a project and integrate the Server SDK into your Python 3 application, all while using the macOS platform.

Prerequisites

Before you start:

  • Install Visual Studio Code (or your preferred application) to write, edit, and debug source code.
  • Get your Apryse trial key.

License Key

Apryse collects some data regarding your usage of the SDK for product improvement.


The data that Apryse collects include:

  • The names and number of API calls
  • The number of pages in a document
  • The version of the SDK
  • The language of the SDK

For clarity, no other data is collected by the SDK and Apryse has no access to the contents of your documents.


If you wish to continue without data collection, contact us and we will email you a no-tracking trial key for you to get started.

Run Apryse SDK in production

A commercial license key is required for use in a production environment. Please fill out our licensing form if you do not have a valid license key.

Keep your commercial license key confidential.

License keys are uniquely generated. Please make sure that it is not publicly available (e.g. in your public GitHub).

1. Install/Update Python3 and pip3

You’ll need Python3 in your system. You’ll need pip3 to eventually install the Apryse Server SDK.

  1. Check to see if Python3 and pip3 are installed on your system:

sh

1python3 --version
2python3 -m pip --version

2. Complete one of the following, using Homebrew, based on whether pip3 and Python3 are already installed:

sh

1brew install python3
  • OPTION 2: Update Python3 and pip3, through Homebrew, if there are versions already installed on your device:
    • Add the following to the command line to ensure Homebrew is up to date:

sh

1brew update
  • Add the following to the command line:

sh

1brew upgrade python3

2. Run a sample

You'll download and run the OfficeToPDFTest sample which allows you to explore and validate the conversion feature offered by the SDK. By running the sample, you can quickly:

  • Verify the capabilities.
  • Understand how to interact with the MS Office to Conversion PDF API.
  • Use it as a reference for integrating similar functionality into your own application.
  1. Download the zip file for macOS which includes all of the Apryse samples.
  2. Go to your Downloads folder and double-click the file to unzip it.
  3. Select Documents, File > New Folder to add a new folder, then enter ApryseSamples as the folder name, and press Enter to create the new folder. You can create a folder at a different location if you prefer, but for this guide’s instructions, we used the location and name above.
  4. Use your Mac Finder to navigate to the Downloads folder, copy the unzipped folder, and paste it into the new folder you made, ApryseSamples.
  5. Navigate to the folder  /Documents/ApryseSamples/PDFNetPython3/Samples/OfficeToPDFTest/Python, right-click on the Python folder, then select New Terminal at Folder.
  6. The macOS terminal opens to the directory you specified. On the command line, enter the following, then press Enter to install the apryse-sdk library from Apryse’s S3 private repository (You need this to run samples successfully):

We recommend running this in a virtual environment, to guarantee the location of where your library will be installed. Henceforth, if your library needs to be imported, be sure to activate the same virtual environment before running a Python script that imports from apryse-sdk.

The PDFNetPython3 library from the default Python Package Index (PyPI) is not used in the code below because it’s no longer being updated. You’ll use the Apryse PyPI instead in the code.

sh

1python3 -m pip install apryse-sdk --extra-index-url=https://pypi.apryse.com

or

sh

1pip3 install apryse-sdk --extra-index-url=https://pypi.apryse.com

If no errors occur, Successfully installed apryse-sdk... will display in terminal.

7. Navigate via your Mac Finder to /Documents/ApryseSamples/PDFNetPython3/Samples/LicenseKey/Python/LicenseKey.py, and double-click to open so you can add your license key to the LicenseKey.py file before running the sample.

8. Scroll down to the line containing ‘LicenseKey = “YOUR_PDFTRON_LICENSE_KEY”’ and replace the words in quotes with the copy of your trial license key. Leave the ‘if LicenseKey ==YOUR_PDFTRON_LICENSE_KEY’ as is with its default text.

9. Save your changes and close the file.

10. To run the sample, navigate, through the Mac Finder, to the folder you created:  /Home/ApryseSamples/PDFNetPython3/Samples/OfficeToPDFTest/Python, right-click on the Python folder, then select New Terminal at Folder.

11. The Mac terminal opens the OfficeToPDFTest sample. From the project directory, on the command line, enter the following, then press Enter:

sh

1sh RunTest.sh

When you run the command, the sample code loads an Office document, converts the Office document to PDF, saves the resulting PDF, and outputs status messages to the console. For more details, you can look at additional OfficeToPDFsample code and the Convert MS Office (Word, Excel, PowerPoint) to PDF overview.

Once the sample finishes running, you'll see the status Done in the terminal.

12. At the command prompt, type Exit, then press Enter.

13. Go to your Mac Finder and navigate to: /Documents/ApryseSamples/Samples/TestFiles/Output. You will see three output PDF files (Fisherman.pdf, the_rime_of_the_ancient_mariner.pdf, and the factsheet_Arabic.pdf):

14. Open the PDF files to see the converted output. You have successfully run the OfficeToPDFTest conversion sample!

3. Create a new project

Next, you’ll create your own Python3 project and add the Apryse Server SDK so you can later integrate the Apryse Server SDK into the project and run it.

  1. In your Mac Finder, navigate to the Documents folder, and add a new folder.
  2. Enter the folder name, NewApryseProject, then press Enter.

4. Integrate the Apryse Server SDK into your application

Next, you’ll open a blank PDF page within your application.

  1. Open Visual Studio Code (or your preferred application) and from the File menu, select Open Folder.
  2. Navigate to the Documents folder from the Mac Finder, then, select the NewApryseProject folder and click Select Folder.

The NewApryseProject opens in Visual Studio Code.

3. Create a file in the project named HelloWorld.py.

4. Copy the following Python code and paste it into Line 1 of the HelloWorld.py file, then Save your file:

Python

1# You can add the following line to integrate apryse-sdk
2# into your solution from anywhere on your system so long as
3# the library was installed successfully via pip
4
5from apryse_sdk import *
6
7def main():
8
9 # You need to initialize the PDFNet library
10 # Before calling any PDF related methods
11 PDFNet.Initialize("demo:1763580498695:601269ef03000000007505554501519c80287620321ec3184e959426e1")
12
13 # This example creates a new document
14 # and a new page, then adds the page
15 # in the page sequence of the document
16 doc = PDFDoc()
17
18 page1 = doc.PageCreate()
19 doc.PagePushBack(page1)
20
21 # We save the document in a linearized
22 # format which is the most popular and
23 # effective way to speed up viewing PDFs
24 doc.Save(("linearized_output.pdf"), SDFDoc.e_linearized)
25
26 doc.Close()
27
28if __name__ == "__main__":
29 main()

Keep in mind the following about the code above:

  • You’re importing the Apryse SDK.
  • You’re initializing PDFNet using your Apryse license key. Scroll down to the line containing PDFNet.Initialize(“YOUR_LICENSE_KEY”); and replace the words in quotes with the copy of your trial license key. If you’re logged in to Apryse.com, your license key will automatically replace YOUR_LICENSE_KEY in the code above.
  • You’re doing various work with PDFNet. In this case, with a PDFDoc object.

5. To test that your code works, run the script in the folder containing HelloWorld.py in the command prompt, then press Enter.

sh

1python -u HelloWorld.py

6. The code executes and you will see “PDFNet is running in demo mode.”

7. Exit out of the command line and navigate to the Documents > NewApryseProject folder from Mac Finder.

8. Open the linearized_output.pdf which you created by integrating the Apryse Server SDK!

9. Now that you have successfully run an OfficeToPDFTest sample and integrated the Apryse Server SDK Python PDF Library into your application, you can try out 50+ samples depending on your needs.

10. To try additional samples, go to section 2. Run a sample > Step 5 above and choose a different sample to run with a different URL path.

Additional modules are required for some samples functionality, For example, OCR, CAD and PDF to Office. You can get these from our additional module downloads page.

Precompiled Python3 and Windows PDF library integration

You'll install or update pip3 and Python3, then get started with our samples to see the output you can create when you integrate the Apryse Server SDK into your application. Last, you'll create a project and integrate the Server SDK into your Python 3 application, all while using the Windows platform.

Prerequisites

Before you start:

  • Install Visual Studio Code (or your preferred application) to write, edit, and debug source code.
  • Get your Apryse trial key.

License Key

Apryse collects some data regarding your usage of the SDK for product improvement.


The data that Apryse collects include:

  • The names and number of API calls
  • The number of pages in a document
  • The version of the SDK
  • The language of the SDK

For clarity, no other data is collected by the SDK and Apryse has no access to the contents of your documents.


If you wish to continue without data collection, contact us and we will email you a no-tracking trial key for you to get started.

Run Apryse SDK in production

A commercial license key is required for use in a production environment. Please fill out our licensing form if you do not have a valid license key.

Keep your commercial license key confidential.

License keys are uniquely generated. Please make sure that it is not publicly available (e.g. in your public GitHub).

1. Install/Update Python3 and pip3

You’ll need Python3 in your system. You’ll need pip3 to eventually install the Apryse Server SDK.

  1. Check to see if Python3 and pip3 are installed on your system:

sh

1python --version
2pip --version

2. Complete one of the following based on whether pip3 and Python3 are already installed:

  • OPTION 1: Install Python3 and pip3 if they're not installed:
    • To download Python3, go to the Python Windows download page, then select the file you need. You can use any Python3 version Python supports.
    • Click on the executable in your Downloads folder to start installation. Make sure to check the box before the actual install to set Python to the path.
  • OPTION 2: Update Python3 and pip3 if there are versions already installed on your device:
    • Add the following to the command line:

sh

1python -m pip install --upgrade pip

2. Run a sample

You'll download and run the OfficeToPDFTest sample which allows you to explore and validate the conversion feature offered by the SDK. By running the sample, you can quickly:

  • Verify the capabilities.
  • Understand how to interact with the MS Office to Conversion PDF API.
  • Use it as a reference for integrating similar functionality into your own application.
  1. Download the zip file for Windows which includes all of the Apryse samples.
  2. Select Documents, New Folder, then enter ApryseSamples, and press Enter to create a new folder to extract the zip file into for running the samples. You can create a folder at a different location if you prefer, but for this guide’s instructions, we used the location and name above.
  3. Use your Windows file explorer to navigate to the Downloads folder, right-click on the zip file, then select Extract all.
  4. Click Browse.
  5. Go to the new folder you made, highlight it, then click Select Folder.
  6. Click Extract.

The file is extracted to the folder.

7. Navigate to the folder /Documents/ApryseSamples/Samples/OfficeToPDFTest/Python, right-click on the Python folder, then select Open in Terminal.

8. The Windows terminal opens to the directory you specified. On the command line, enter the following, then press Enter to install the apryse-sdk library from Apryse’s S3 private repository (You need this to run samples successfully):

We recommend running this in a virtual environment, to guarantee the location of where our library will be installed. Henceforth, if our library needs to be imported, be sure to activate the same virtual environment before running a Python script that imports from apryse-sdk.

The PDFNetPython3 library from the default Python Package Index (PyPI) is not used in the code below because it’s no longer being updated. You’ll use the Apryse PyPI instead in the code.

sh

1pip install apryse-sdk --extra-index-url=https://pypi.apryse.com

If no errors occur, Successfully installed apryse-sdk... will display in terminal.

9. Navigate via your Windows file explorer to /Documents/ApryseSamples/Samples/LicenseKey/Python/LicenseKey.py and double-click to open so you can add your license key to the LicenseKey.py file before running the sample.

10. Scroll down to the line containing ‘LicenseKey = “YOUR_PDFTRON_LICENSE_KEY”' and replace the words in quotes with the copy of your trial license key. Leave the ‘if LicenseKey ==YOUR_PDFTRON_LICENSE_KEY’ as is with its default text.

11. Save your changes and close the file.

12. To run the sample, navigate, through the Windows file explorer, to the folder you created:  /Home/ApryseSamples/Samples/OfficeToPDFTest/Python, right-click on the Python folder, then select Open in Terminal.

13. The Windows terminal opens the OfficeToPDFTest sample. From the project directory, on the command line, enter the following, then press Enter:

sh

1python –u OfficeToPDFTest.py

When you run the command, the sample code loads an Office document, converts the Office document to PDF, saves the resulting PDF, and outputs status messages to the console. For more details, you can look at additional OfficeToPDFsample code and the Convert MS Office (Word, Excel, PowerPoint) to PDF overview.

Once the sample finishes running, you'll see the status Done in the terminal.

14. At the command prompt, type exit, then press Enter.

15. Go to your Windows file explorer and navigate to: /Documents/ApryseSamples/Samples/TestFiles/Output. You will see three output PDF files (Fisherman.pdf, the_rime_of_the_ancient_mariner.pdf, and the factsheet_Arabic.pdf):

16. Open the PDF files to see the converted output. You have successfully run the OfficeToPDFTest conversion sample!

3. Create a new project

Next, you’ll create your own Python3 project and add the Apryse Server SDK so you can later integrate the Apryse Server SDK into the project and run it.

  1. In your Windows file explorer, navigate to the Documents folder, and add a new folder.
  2. Enter the folder name, NewApryseProject, then press Enter.

4. Integrate the Apryse Server SDK into your application

Next, you’ll open a blank PDF page within your application.

  1. Open Visual Studio Code (or your preferred application) and from the File menu, select Open Folder.
  2. Navigate to the Documents folder from Windows file explorer, then, select the NewApryseProject folder and click Select Folder.

The NewApryseProject opens in Visual Studio Code.

3. Create a file in the project named HelloWorld.py.

4. Copy the following Python code and paste it into Line 1 of the HelloWorld.py file, then Save your file:

Python

1# You can add the following line to integrate apryse-sdk
2# into your solution from anywhere on your system so long as
3# the library was installed successfully via pip
4
5from apryse_sdk import *
6
7def main():
8
9 # You need to initialize the PDFNet library
10 # Before calling any PDF related methods
11 PDFNet.Initialize("YOUR_LICENSE_KEY")
12
13 try:
14 # This example creates a new document
15 # and a new page, then adds the page
16 # in the page sequence of the document
17 doc = PDFDoc()
18
19 page1 = doc.PageCreate()
20 doc.PagePushBack(page1)
21
22 # We save the document in a linearized
23 # format which is the most popular and
24 # effective way to speed up viewing PDFs
25 doc.Save(("linearized_output.pdf"), SDFDoc.e_linearized)
26
27 doc.Close()
28 except Exception as e:
29 print("Unable to create PDF document, error: " + str(e))
30
31 PDFNet.Terminate()
32
33if __name__ == "__main__":
34 main()

Keep in mind the following about the code above:

  • You’re importing the Apryse SDK.
  • You’re initializing PDFNet using your Apryse license key. Scroll down to the line containing PDFNet.Initialize(“YOUR_LICENSE_KEY”); and replace the words in quotes with the copy of your trial license key. If you’re logged in to Apryse.com, your license key will automatically replace YOUR_LICENSE_KEY in the code above.
  • You’re doing various work with PDFNet. In this case, with a PDFDoc object.
  • You’re calling PDF.Terminate which releases any resources in use.

5. To test that your code works, run the script in the folder containing HelloWorld.py in the command prompt, then press Enter.

sh

1python.exe -u HelloWorld.py

6. The code executes and you will see “PDFNet is running in demo mode.”

7. Exit out of the command line and navigate to the Documents > NewApryseProject folder from Windows file explorer.

8. Open the linearized_output.pdf which you created by integrating the Apryse Server SDK!

9. Now that you have successfully run an OfficeToPDFTest sample and integrated the Apryse Server SDK Python PDF Library into your application, you can try out 50+ samples depending on your needs.

10. To try additional samples, go to section 2. Run a sample > Step 7 above and choose a different sample to run with a different URL path.

Additional modules are required for some samples functionality, For example, OCR, CAD and PDF to Office. You can get these from our additional module downloads page.


Precompiled Python3 and Linux PDF library integration 

You'll install or update pip3 and Python 3, then get started with our samples to see the output you can create when you integrate the Apryse Server SDK into your application. Last, you'll create a project and integrate the Server SDK into your Python 3 application, all while using the Linux platform.

Prerequisites

Before you start:

  • Install Visual Studio Code (or your preferred application) to write, edit, and debug source code.
  • Get your Apryse trial key.

License Key

Apryse collects some data regarding your usage of the SDK for product improvement.


The data that Apryse collects include:

  • The names and number of API calls
  • The number of pages in a document
  • The version of the SDK
  • The language of the SDK

For clarity, no other data is collected by the SDK and Apryse has no access to the contents of your documents.


If you wish to continue without data collection, contact us and we will email you a no-tracking trial key for you to get started.

Run Apryse SDK in production

A commercial license key is required for use in a production environment. Please fill out our licensing form if you do not have a valid license key.

Keep your commercial license key confidential.

License keys are uniquely generated. Please make sure that it is not publicly available (e.g. in your public GitHub).

1. Install/Update Python 3 and pip3

You’ll need Python 3 on your system. You’ll need pip3 to eventually install the Apryse Server SDK.

We used Ubuntu, version 25.10, for creating this guide.

  1. Check to see if Python 3 and pip3 are installed on your system:

sh

1python3 --version
2python3 -m pip –version

2. Complete one of the following based on whether pip3 and Python3 are already installed:

  • OPTION 1: Install Python 3 and pip3 if they’re not installed.
    • To download Python 3, go to the Python home page, hover over Downloads at the top of the page, then select the file you need based on your operating system and device processor. You can use any Python 3 version that’s supported.
    • To install pip3 from the command line:
      • Install pip3 to your package lists:

sh

1sudo apt update
  • Enable the universe repository, if not already enabled:

sh

1sudo add-apt-repository universe
2sudo apt update
  • Install pip3:

sh

1sudo apt install python3-pip
  • OPTION 2: Update Python 3 and pip3 if there are versions already installed on your device:
    • Add the following to the command line:

sh

1sudo apt install python3 python3-pip

2. Run a sample

You’ll download and run the OfficeToPDFTest sample which allows you to explore and validate the conversion features offered by the SDK. By running the sample, you can quickly:

  •  Verify the capabilities.
  • Understand how to interact with the MS Office to Conversion PDF API.
  • Use it as a reference for integrating similar functionality into your own application.
  1. Download the tarball file for Linux file which includes all of the Apryse samples.
  2. Use your Linux file explorer to navigate to the Downloads folder, right-click on the tar.gz tarball file, then select Extract to...
  3. Select Home, New Folder, then enter ApryseSamples, and click Create to create a new folder to extract the tarball file into for running the samples. You can create a folder at a different location if you prefer, but for this guide’s instructions, we used the location and name above.
  4. Select the new folder you created, then click the Select button to cause the file to extract to your new folder.

The ApryseSamples folder is created and the tarball is extracted to the folder.

5. Navigate to the folder /Home/ApryseSamples/PDFNetPython3/Samples/OfficeToPDFTest/Python, right-click on the Python folder, then select Open in Terminal.

6. The Linux terminal opens to the directory you specified. On the command line, enter the following, then press Enter to install the apryse-sdk library from Apryse’s S3 private repository (You need this to run samples successfully):

The PDFNet Python 3 library from the default Python Package Index (PyPI) is not used in the code below because it’s no longer being updated. You’ll use the Apryse PyPI instead in the code.

sh

1python3 -m pip install apryse-sdk --extra-index-url=https://pypi.apryse.com

or

sh

1pip3 install install apryse-sdk --extra-index-url=https://pypi.apryse.com

If no errors occur, Successfully installed apryse-sdk ... will display in terminal.

7. Navigate via your Linux file explorer to /Home/ApryseSamples/PDFNetPython3/Samples/LicenseKey/Python/LicenseKey.py and double-click to open so you can add your license key to the LicenseKey.py file before running the sample.

8. Scroll down to the line containing ‘LicenseKey = “YOUR_PDFTRON_LICENSE_KEY” and replace the words in quotes with the copy of your trial license key. Leave the ‘if LicenseKey ==YOUR_PDFTRON_LICENSE_KEY’ as is with its default text.

9. Save your changes and close the file.

10. To run the sample, navigate, through the Linux file explorer, to /Home/ApryseSamples/PDFNetPython3/Samples/OfficeToPDFTest/Python, right-click on the Python folder, then select Open in Terminal.

11. The Linux terminal opens the OfficeToPDFTest sample. From the project directory, on the command line, enter the following, then press Enter:

sh

1sh RunTest.sh

When you run the sh RunTest.sh command, the sample code loads an Office document, converts the Office document to PDF, saves the resulting PDF, and outputs status messages to the console. For more details, you can look at additional OfficeToPDF sample code and the Convert MS Office (Word, Excel, PowerPoint) to PDF overview.

Once the sample finishes running, you'll see the status Done in the terminal.

12. At the command prompt, type Exit, then press Enter.

13. Go to your Linux file explorer and navigate to: /Home/ApryseSamples/PDFNetPython3/Samples/TestFiles/Output. You will see three output PDF files (Fisherman.pdf, the_rime_of_the_ancient_mariner.pdf, and the factsheet_Arabic.pdf).

14. Open the PDF files to see the converted output. You have successfully run the OfficeToPDFTest conversion sample!

3. Create a new project

Next, you’ll create your own Python 3 project and add the Apryse Server SDK so you can later integrate the Apryse Server SDK into the project and run it.

  1. In your Linux file explorer, navigate to the Home folder, and select New Folder. Alternatively, press CTRL + Shift + n to display the New Folder modal.
  2. Enter the folder name, NewApryseProject, then click Create.
  3. Navigate to the folder /Home/NewApryseProject, right-click on the folder, then select Open in Terminal.
  4. The Linux terminal opens to the directory you specified. On the command line, enter the following, then press Enter to install the apryse-sdk library from Apryse’s S3 private repository (You need this to run your project successfully):

The PDFNet Python 3 library from the default Python Package Index (PyPI) is not used in the code below because it’s no longer being updated. You’ll use the Apryse PyPI instead in the code.

sh

1python3 -m pip install apryse-sdk --extra-index-url=https://pypi.apryse.com

or

sh

1pip3 install install apryse-sdk --extra-index-url=https://pypi.apryse.com

If no errors occur, Successfully installed apryse-sdk ... will display in terminal.

4. Integrate the Apryse Server SDK into your application

Next, you’ll open a blank PDF page within your application.

  1. Open Visual Studio Code (or your preferred application) and from the File menu, select Open Folder.
  2. Navigate to the Home folder from Linux file explorer, then, select the NewApryseProject folder and click Open.

The NewApryseProject opens in Visual Studio Code.

3. Create a file in the project named HelloWorld.py.

4. Copy the following Python code and paste it into Line 1 of the HelloWorld.py file, then Save your file.

Python

1# You can add the following line to integrate apryse-sdk
2# into your solution from anywhere on your system so long as
3# the library was installed successfully via pip
4from apryse_sdk import *
5def main():
6 # You need to initialize the PDFNet library
7 # Before calling any PDF related methods
8 PDFNet.Initialize("YOUR_LICENSE_KEY")
9 # This example creates a new document
10 # and a new page, then adds the page
11 # in the page sequence of the document
12 doc = PDFDoc()
13 page1 = doc.PageCreate()
14 doc.PagePushBack(page1)
15 # We save the document in a linearized
16 # format which is the most popular and
17 # effective way to speed up viewing PDFs
18 doc.Save(("linearized_output.pdf"), SDFDoc.e_linearized)
19 doc.Close()
20if __name__ == "__main__":
21 main()

Keep in mind the following about the code above:

  • You’re importing the Apryse SDK.
  • You’re initializing PDFNet using your Apryse license key. Scroll down to the line containing PDFNet.Initialize(“YOUR_LICENSE_KEY”); and replace the words in quotes with the copy of your trial license key. If you’re logged in to Apryse.com, your license key will automatically replace YOUR_LICENSE_KEY in the code above.
  • You’re doing various work with PDFNet. In this case, with a PDFDoc object.
  • You’re calling PDF.Terminate which releases any resources in use.

5. To test that your code works, run the script in the folder containing HelloWorld.py in the command prompt, then press Enter.

sh

1python3 -u HelloWorld.py

The code executes and you will see PDFNet is running in demo mode.

6. Exit out of the command line and navigate to the Home/NewApryseProject folder from Linux file explorer.

7. Open the linearized_output.pdf which you created by integrating the Apryse Server SDK!

8. Now that you have successfully run an OfficeToPDFTest sample and integrated the Apryse Server SDK Python PDF Library into your application, you can try out 50+ samples depending on your needs.

9. To try additional samples, go to section 2. Run a sample > Step 5 above and choose a different sample to run with a different URL path.

Additional modules are required for some samples functionality, for example, OCR, CAD and PDF to Office. You can get these from our additional module downloads page.

This guide will help you set up Apryse SDK serverless AWS Lambda functions. Your free trial includes unlimited trial usage and support from solution engineers.

Prerequisites

Initial setup

In this particular guide, we will demonstrate how to set up an AWS Lambda function to use Apryse SDK.

First, prepare a zip package with apryse-sdk embedded and your lambda_function.py.

From the command line, check your Python3 version. This information will be needed when you create your function later.

sh

1python3 --version

From the command line:

sh

1mkdir YOUR_FUNCTION_FOLDER
2cd YOUR_FUNCTION_FOLDER
3python3 -m pip install --target . apryse-sdk --extra-index-url=https://pypi.apryse.com

Copy your lambda source (i.e. lambda_function.py) to YOUR_FUNCTION_FOLDER. Then zip your package before uploading it to your AWS Lambda account.

sh

1zip -r ./YOUR_FUNCTION_FOLDER.zip .

You can now upload YOUR_FUNCTION_FOLDER.zip to your AWS Lambda.

Second, create a lambda function in your AWS account and upload the zip package:

  • Lambda > Functions > Create function > Author from scratch > Function name [YOUR_FUNCTION_NAME] > Runtime [Python3.x] (Choose the version that matches your Python3 version from the checking above.) > choose Create Function
  • Upload from .zip file > Upload [your zip package] > Save
  • Add triger > API Gateway > Create an API > REST > Security [Open]
  • Configuration > General configuration > Edit > memory [Choose 10240MB]

Integrate into your application

Once you have followed the initial setup instructions, you can begin calling Apryse SDK APIs in your lambda function source. For example:

Python

1from base64 import b64encode, b64decode
2import json
3from apryse_sdk import *
4def lambda_handler(event, context):
5 if event["httpMethod"] == "GET":
6 return {
7 'statusCode': 200,
8 'body': json.dumps('Hello from Apryse!')
9 }
10 elif event["httpMethod"] == "POST":
11 try:
12 body = json.loads(event["body"])
13 PDFNet.Initialize("YOUR_APRYSE_LICENSE_KEY") # if you use apryse-sdk 9.1.0 and above. Otherwise use PDFNet.Initialize()
14 # your AWS lambda function goes here
15 message = {
16 'statusCode': 200,
17 'headers': {'Content-Type': 'application/json'},
18 'body': json.dumps(base64_string),
19 }
20 return message
21 except Exception as e:
22 message = {
23 'statusCode': 500,
24 'body': e
25 }
26 return (message)

Run Sample Code

You can update python script of your AWS Lambda function with the following code or download the source code from our github respository. This snippet shows how to process a request sent from a client to convert an office document to PDF and send the output to client.

Python

1# This example shows how to create AWS Lambda functions using Apryse SDK.
2# A REST API request was posted with base64 encoded data by the client.
3# The request would be processed by the server and a response with base64 encoded data of OfficeToPDF output would be sent to the client.
4from base64 import b64encode, b64decode
5import json
6from apryse_sdk import *
7def lambda_handler(event, context):
8 if event["httpMethod"] == "GET":
9 return {
10 'statusCode': 200,
11 'body': json.dumps('Hello, please send base64 doc to use this Lambda!')
12 }
13 elif event["httpMethod"] == "POST":
14 try:
15 body = json.loads(event["body"])
16 base64str = body["file"]["data"]
17 filename = body["file"]["filename"]
18 base64_bytes = b64decode(base64str)
19 # save input doc
20 output_path = '/tmp/'
21 input_filename = filename.split('.')[0] + '.docx'
22 with open(output_path + input_filename, 'wb') as open_file:
23 byte_content = open_file.write(base64_bytes)
24 # Start with a PDFDoc
25 PDFNet.Initialize("YOUR_APRYSE_LICENSE_KEY") # if you use apryse-sdk 9.1.0 and above. Otherwise use PDFNet.Initialize()
26 pdfdoc = PDFDoc()
27 # perform the conversion with no optional parameters and save to /temp
28 Convert.OfficeToPDF(pdfdoc, output_path + input_filename, None)
29 # save the result
30 output_filename = filename.split('.')[0] + '.pdf'
31 pdfdoc.Save(output_path + output_filename, SDFDoc.e_linearized)
32 # sending data
33 with open(output_path + output_filename, 'rb') as open_file:
34 byte_content = open_file.read()
35 base64_bytes = b64encode(byte_content)
36 base64_string = base64_bytes.decode('utf-8')
37 print("Sending " + output_filename )
38 message = {
39 'statusCode': 200,
40 'headers': {'Content-Type': 'application/json'},
41 'body': json.dumps(base64_string),
42 }
43 return message
44 except Exception as e:
45 print(e)
46 message = {
47 'statusCode': 500,
48 'body': e
49 }
50 return (message)

Testing

After you have uploaded your zip package to your AWS Lambda function and get its API endpoint in Configuration > Triggers, you can now do a simple test using REST API.

In order to use this function to convert an office document to PDF, the client needs to post a REST API request to the server. The request must include json data structured as in the code below.

Python

1json_data = {
2 "file": {
3 "encoding": "base64",
4 "data": base64_string_of_your_office_document,
5 "filename": filename,
6 "content-type": "application/pdf"
7 }
8 }

Upon receiving a client's request, a response will be sent back to the client including the base64 encoded pdf output using the same json structure. All the client needs to do now is decode the encoded data into pdf. That's it!
Access the sample python code. After cloning the repository and installing necessary packages, please refer to /client/README.txt for detailed instructions. Navigate to the client folder, run the following command, and see the reponse to client's request in the console and check the output in the output folder:

sh

1python AWSLambdaExample.py --url <YOUR PUBLISHED FUNCTION URL>

The client will send a REST API request to convert /input/simple-word_2007.docx to pdf and the server will send back the encoded data, which will then be saved as pdf in the output folder.

You can experiment with your own office document by putting it inside input folder:

sh

1python AWSLambdaExample.py --url <YOUR PUBLISHED FUNCTION URL> --filename <YOUR OFFICE FILENAME>

We have shown how to set an AWS Lambda function using Apryse SDK. You can now experiment making your own functions, URLs, and can fully utilize Apryse SDK. If your have any questions, please don't hesitate to contact us!

Related Blogs

How to Build Optical Character Recognition (OCR) in Python - 1/16/25

Splitting a PDF Using Python - 9/11/24

PDF to Office Document Conversion Using Apryse and Python - 4/4/24

Generating Documents and Reports from DOCX Templates and JSON using Apryse and Python - 10/9/23

A Guide to PDF Data Extraction Using Apryse SDK and Python - 7/20/23

Adding a Digital Signature to a PDF With the Python SDK - 7/13/23

How to Extract Text from a PDF Using Python - 12/9/22

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales