Skip to content

Foxit PDF SDK Python Library ​

This section describes how to run samples and create a basic Python project using the Foxit PDF SDK Python library. The project demonstrates rendering the first page of a PDF document to a bitmap and saving it as a JPG image.

Prerequisites ​

Development environment ​

  • Python version: Supports Python 2.7, Python 3.6–3.10, and Python 3.11+ builds using the ABI3 interface.
  • Foxit PDF SDK Python library: Request a trial via the Foxit Developer Hub. Download and install the library that matches your OS and architecture.

System support ​

We provide detailed system support information for Windows, Linux, and Mac, including operating system versions and compiler requirements. Select your platform to view the details.

Windows Linux Mac

Configuration ​

Install the Foxit SDK Python module ​

python
pip install FoxitPDFSDKPython3

Install third-party modules ​

To run the Security Demo and Signature Demo successfully, install the third-party modules as follows.

python
pip install cryptography==2.3
pip install pyopenssl==19.0.0
pip install uuid

Configure the Python library ​

To run the Foxit PDF SDK Python 2.7 library correctly on Windows or Linux, copy the appropriate library file based on your Python version (32-bit or 64-bit).

Windows Python 2.7 users:

  • x64 systems:
    • Manually copy FoxitPDFSDKPython2/x64_vc15/_fsdk.pyd to the FoxitPDFSDKPython2/ directory.
  • x86 systems:
    • Manually copy FoxitPDFSDKPython2/x86_vc15/_fsdk.pyd to the FoxitPDFSDKPython2/ directory.

Linux Python 2.7 users:

  • x64 systems:
    • Manually copy FoxitPDFSDKPython2/x64/_fsdk.so to the FoxitPDFSDKPython2/ directory.
  • x86 systems:
    • Manually copy FoxitPDFSDKPython2/x86/_fsdk.so to the FoxitPDFSDKPython2/ directory.

Alternatively, run the examples/simple_demo/rundemo_python.py script to copy the library files automatically.

Run samples ​

Foxit PDF SDK provides Python scripts to run Python samples. Follow the steps below to start and run the relevant samples quickly.

Run samples with the Python script ​

The rundemo_python.py script wraps sample configuration and dependency handling to simplify running samples.

Run all samples

python
cd examples/simple_demo/
python rundemo_python.py

Run a specific sample

For example, to run the annotation sample:

python
cd examples/simple_demo/
python rundemo_python.py annotation

Run sample scripts directly ​

If your Python environment and dependencies are configured correctly, you can run sample scripts directly. The following command runs the annotation sample:

python
cd examples/simple_demo/annotation/
python -u annotation.py

NOTE

  • For Python 2.7, see Configure the Python library and ensure the library file matches your Python version.
  • For Python 3, if the FoxitPDFSDKPython3 module is installed, you can run sample scripts directly.

Quick start ​

  1. Create a project directory and prepare sample files

    • Create a project folder: Create a folder named test.
    • Copy sample data: Copy SamplePDF.pdf from /examples/simple_demo/input_files into test as the project input file.
  2. Configure the Python library

  3. Create and run the project script

    • Create a sample script: In the test folder, create a Python script named test.py and add the sample code below:
    [test.py]
    python
    import sys
    import site
    import platform
    from shutil import copyfile
    
    if sys.version_info.major == 2:
        _PYTHON2_ = True
    else:
        _PYTHON2_ = False
    
    # For Python2, copy the corresponding version of the dynamic library to the folder FoxitPDFSDKPython2.
    if _PYTHON2_:
        arch = platform.architecture()
    
        if arch[0] == "32bit":
            src_lib_path = "./FoxitPDFSDKPython2/x86_vc15/_fsdk.pyd"
        elif arch[0] == "64bit":
            src_lib_path = "./FoxitPDFSDKPython2/x64_vc15/_fsdk.pyd"
    
        dest_lib_path = "./FoxitPDFSDKPython2/_fsdk.pyd"
    
        if src_lib_path is not None:
            copyfile(src_lib_path, dest_lib_path) 
    
    if _PYTHON2_:
        site.addsitedir('./')
        from FoxitPDFSDKPython2 import *
    else:
        from FoxitPDFSDKPython3 import *
    # Assuming PDFDoc doc has been loaded.
    
    # The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN=").
    # The value of "key" can be got from "gsdk_key.txt" (the string after "Sign=").
    sn = " "
    key = " "
    
    def main():
        # Load a PDF document, and parse the first page of the document.
        doc = PDFDoc("SamplePDF.pdf")
        error_code = doc.Load("")
        if error_code!= e_ErrSuccess: 
            return 0
        page = doc.GetPage(0)
        page.StartParse(PDFPage.e_ParsePageNormal, None, False)
    
        width = int(page.GetWidth())
        height = int(page.GetHeight())
        matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation())
    
        # Prepare a bitmap for rendering.
        bitmap = Bitmap(width, height, Bitmap.e_DIBArgb)
        bitmap.FillRect(0xFFFFFFFF, None)
        # Render page.
        render = Renderer(bitmap, False)
        render.StartRender(page, matrix, None)
    
        # Add the bitmap to image and save the image.
        img = Image()
        img.AddFrame(bitmap)
        img.SaveAs("testpage.jpg")
        return 0
    
    if __name__ == '__main__':
        code = Library.Initialize(sn, key)
        if code == e_ErrSuccess: 
            main()
        Library.Release()
    • Run the sample script: Open a terminal, navigate to the test folder, and run python test.py.
    • Verify output: If the script runs successfully, testpage.jpg is generated in the current folder.