This is a demonstration of the PDFreactor Ruby API.

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

require File.expand_path('../../lib/PDFreactor.rb',  __FILE__)

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.new()

Before you can create a PDF document using the PDFreactor Ruby 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 Ruby API. Therefore you could add something similar to the code snippet shown below to your Ruby file.

# The resulting PDF
result = nil

begin
    # Render document and save result
    result = pdfReactor.convertAsBinary(config=;
rescue Exception => error
    print "Content-type: text/html\n\n"
    puts "<h1>An Error Has Occurred</h1>"
    puts "<h2>#{error}</h2>"
end

# Check if successful
if (result != nil)
    print "Content-type: application/pdf\n\n"
    $stdout.binmode
    print result
end

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.