Some test text!

menu
search

Get started with Python3

Welcome to PDFTron. Python3 for the PDFTron SDK is supported on both Linux and Windows and requires that custom wrappers are built. To get started, choose your preferred platform from the tabs below.

Get started with Python Custom Wrapper

This guide will help you run PDFTron samples and integrate a free trial of the PDFTron SDK into Python applications on macOS. Your free trial includes unlimited trial usage and support from solution engineers.

There are two ways to use PDFTron with Python:

  • Use the precompiled library for Python 2.7.
  • Use PDFNet bindings to build your own wrapper.

This guide will help you get started by building your own wrapper. You can find more information about using the precompiled library .

linkPrerequisites

  • Python3 or Python2 with developer extensions
  • CMake version ≥ 2.8
  • PDFTron SDK for macOS:
    Download for macOS
  • SWIG 2.0.4 - 2.0.12 or SWIG 3.x

linkInitial setup

  1. Make a directory to store the wrappers and navigate into that directory.
  2. Clone the uncompiled PDFNet wrappers project by executing

    git clone https://github.com/PDFTron/PDFNetWrappers
  3. Navigate to PDFNetWrappers/PDFNetC and move the downloaded [PDFNet C/C++ SDK] for macOS(#prerequisites) into that directory and unzip it. Ensure you obtain the right architecture for your Python interpreter. This can be done with these two commands:

    curl -L -O http://www.pdftron.com/downloads/PDFNetCMac.zip
    unzip PDFNetCMac.zip
  4. Now to move the headers in place, make sure you are in the PDFNetWrappers/PDFNetC directory and execute

    mv PDFNetCMac/Headers/ .

    and

    mv PDFNetCMac/Lib/ .

    to move the PDFNet libraries in place.

    You can delete PDFNetCMac.zip to free up space.

    Your /PDFNetC folder should be laid out like this like this:

    PDFNetC
    ├── Headers
    ├── Lib
    ├── PDFNetCMac
    └── README.txt
  5. Make a build directory inside /PDFNetWrappers and navigate to it. This guide will assume the build directory is called Build.
  6. Execute

    cmake -D BUILD_PDFNetPython=ON ..

    If all goes well, you should get a message which reads:

    Build files have been written to ~/PDFNetWrappers/Build
  7. Execute make followed by sudo make install.
  8. Next we need to fix the rpaths issue on Mac. While still in the same Build directory, execute these lines:

    cp ../fix_rpaths.sh ../PDFNetC/Lib/
    cd ../PDFNetC/Lib/
    sudo sh ./fix_rpaths.sh

You are now ready to run the samples or integrate PDFTron SDK into your own application.

Also check out the source for the PDFNetC wrappers here.

linkRun the samples

Running a specific sample

  1. Navigate to the PYTHON folder in the sample, for example /Samples/AddImageTest/PYTHON and execute ./RunTest.sh.

Run all samples

  1. To run all tests, navigate to /Samples and execute

    ./runall_python.sh

    The tests will run one by one.

Output files will be in /Samples/TestFiles/Output

linkIntegrate into your application

This section will show you how to use our SDK to create a simple PDFTron "Hello World!" application. It will create a document with one blank page and save it as a linearized PDF in its running directory.

  1. Navigate to the /Samples directory and create a new directory called myApp (if it does not exist already). This guide will assume your application is named myApp. For organization, create a new directory inside myApp called PYTHON.
  2. Navigate inside that PYTHON directory and create a new Python file called myApp.py. Open it with your favorite text editor and paste this into it:

    import site
    # below is the relative path to PDFTron PDFNetC libraries
    site.addsitedir("../../../PDFNetC/Lib")
    import sys
    from PDFNetPython import *
    
    def main():
        PDFNet.Initialize()
        doc = PDFDoc()
        page = doc.PageCreate()         # Start a new page
        doc.PagePushBack(page)          # Add the page to the document
        doc.Save("output.pdf", SDFDoc.e_linearized);  # Save the document as a linearized PDF
        print("Hello World!")
    
    if __name__ == '__main__':
        main()
  3. Run your application via python myApp.py. If all goes well your output should read:

    PDFNet is running in demo mode.
    Permission: write
    Hello World!

Check the output.pdf that the program output in the same directory. It should be a PDF with one blank page.

linkNext step

GuidesSamples

Also check out the source for the PDFNetC wrappers.

linkTroubleshooting

Check the troubleshooting page if you run into any issues going through this document.

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close