This is a demonstration of the PDFreactor JavaScript API.

The first step on your way to successfully create a PDF document using the PDFreactor JavaScript API should be the import of the PDFreactor JavaScript lib. To ensure that the example works out-of-the-box you could use something similar to the example shown below.

<script src="../lib/PDFreactor.js"></script>

Next you have to determine an appropriate starting time for the PDF conversion. To achieve that you could wrap the entire JavaScript code into a "window.onload" function which will start the PDF conversion once the document is loaded into the browsers window.

window.onload = function() {
    //Further JavaScript code here
}

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.

var pdfReactor = new PDFreactor();

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

var 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 JavaScript API. Therefore you could add something similar to the code snippets shown below to your scripting area.

pdfReactor.convertAsync(config, function(documentId) {
    progressStatus(documentId);
}, function(error) {
    document.body.innerHTML = "<h1>An Error Has Occurred</h1>"
                            + "<h2>" + error + "</h2>";
});

This example renders the document and returns the document ID which will be used in the next step to retrieve the document. Following it 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 PDFreactor configuration object will trigger such an error message.

Now you have to check whether the conversion has finished or not. The function below will display a link to the resulting PDF once the conversion has finished.

function progressStatus(documentId) {
    pdfReactor.getProgress(documentId, function(status) {

        if (status.progress != 100) {
            progressStatus(documentId);
        }

        if (status.progress == 100) {
            var url = "http://localhost:9423/service/rest/document/"
                    + documentId + ".pdf";
            var link = document.createElement('a');
            var linkText = document.createTextNode("Show converted PDF");
            link.appendChild(linkText);
            link.href = url;
            link.target = "_blank";
            document.body.appendChild(link);
        }
    });
}