At the beginning you have to import the PDFreactor Perl module. To ensure that the example works out-of-the-box you could use something similar to the example shown below.
use Cwd qw(abs_path); use File::Basename; my $directory = dirname(dirname(abs_path($0)))."/lib/"; require $directory."PDFreactor.pm";
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.
my $pdfReactor = PDFreactor -> new();
Before you can create a PDF document using the PDFreactor Perl 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 Perl API. Therefore you could add something similar to the code snippet shown below to your Perl file.
eval { # Render document and save result to result $result = $pdfReactor -> convertAsBinary($config); print "Content-type: application/pdf\n\n"; binmode(STDOUT); print $result; } || do { my $e = $@; # Check if successful if (!defined $result) { # Not successful, print error and log print "Content-type: text/html\n\n"; print "<h1>Error During Rendering</h1>"; print "<h2>" . $e . "</h2>"; } };
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.