Python API

# This is a demonstration of the PDFreactor Python API.

At the beginning you have to import the PDFreactor Python module. To ensure that the example works out-of-the-box you could use something similar to the example shown below.

    import os
    import sys
    sys.path.append(os.path
        .abspath(os.path.join(os.path.dirname( __file__ ), '../lib/')))
    from PDFreactor import *

You can create a new PDFreactor instance now. Therefore you should store this instance in a new variable named similar to "pdfReactor". This variable will be used later on to convert your input document to the PDF document. Please refer to the example below.

    pdfReactor = PDFreactor()

Before you can create a PDF document using the PDFreactor Python API you need to create the PDFreactor configuration object. All the information necessary about the resulting PDF is stored in this object.

    config = {
        'document': "<html><body><p>Hello World</p></body></html>",
        'title': "Hello World sample",
        'viewerPreferences': [
            PDFreactor.ViewerPreferences.FIT_WINDOW,
            PDFreactor.ViewerPreferences.PAGE_MODE_USE_THUMBS
        ],
        'userStyleSheets': [
            {
                'content': "body {"\
                               "margin: 1cm;"\
                           "}"
            }
        ]
    }

The example above describes the configuration object of a very simple "Hello World" document with the title "Hello World sample", a margin of 1cm on each side and some ViewerPreferences. It is of course possible to personalize the config object as needed. For further information about possible configuration properties please refer to the PDFreactor Manual.

Only the PDF conversion is left to successfully generate a PDF document using the PDFreactor Python API. Therefore you could add something similar to the code snippet shown below to your Python file.

    # The resulting PDF
    result = None

    try:
        # Render document and save result
        result = pdfReactor.convertAsBinary(config)
    except Exception as e:
        # Not successful, print error and log
        print("Content-type: text/html\n\n")
        print("<h1>An Error Has Occurred</h1>")
        print("<h2>" + str(e) + "</h2>")

    # Check if successful
    if result != None:
        if sys.platform == "win32":
            import msvcrt
            msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

        # Set the correct header for PDF output and echo PDF content
        print("Content-Type: application/pdf\n")

        # Check Python version
        if sys.version_info.major == 2:
            print(result)
        else:
            sys.stdout.flush()
            sys.stdout.buffer.write(result)

This code snippet performs a check whether the conversion was successful or not. If the conversion was unsuccessful an appropriate error warning will be displayed. For example a missing document property in the configuration object will trigger such an error message. If the conversion was successful the resulting PDF document will be displayed via the browsers PDF viewer plug-in.