T. Land
T. Land

Reputation: 1

Can PDFreactor be used to create PDF documents from browser DOM?

Can PDFreactor be used as a Web Service whereby javascript running on a browser can use the REST API to shuttle page content from the DOM to be converted to PDF? In this application, the content being rendered in the browser is generated exclusively by browser-side javascript (using the D3 library); the requirement here is to provide an "Export to PDF" type action to the browser end-user from the currently rendered page contents (via a button or menu action). The goal would then be to shuttle elements from the current DOM to PDFreactor via the REST API so that the Web Service could generate the PDF. Is this a reasonable use of PDFreactor?

Thank you in advance.

Upvotes: 0

Views: 1510

Answers (1)

realobjects
realobjects

Reputation: 301

It is of course possible to convert the HTML content which was already processed by the browser with PDFreactor. In this case you could e.g. use the JavaScript API to pass the content extracted from the "body" element to PDFreactor and display the resulting PDF in the same page. Please see an example script below:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="http://www.pdfreactor.com/product/wrappers/javascript/lib/PDFreactor.js"></script>
        <script>
            window.onload = function() {
                convertToPDF();
            }
            function convertToPDF() {
                // Create new PDFreactor instance
                var pdfReactor = new PDFreactor();
                // Get the content from the body element
                var content = "<html><body>"+document.body.innerHTML+"</body></html>";
                // Create a new PDFreactor configuration object
                var config = {
                    // Specify the input document
                    'document': content,
                    // Set a base URL for images, style sheets, links
                    baseURL: window.location.href,
                }
                // Render document and save result
                pdfReactor.convert(config, function(result) {

document.body.innerHTML += '<iframe id="result" style="width: 100%; height: 95vh"></iframe>';
                    document.getElementById("result").src = "data:application/pdf;base64," + result.document;
                }, function(error) {
                    document.body.innerHTML += "<h1>An Error Has Occurred</h1>"
                                            + "<h2>" + error + "</h2>";
                });
            }
        </script>
    </head>
    <body>
        <p>Hello World</p>
    </body>
</html>

However as PDFreactor supports a variety of popular JavaScript libraries including D3.js, jQuery, etc. it might be more efficient to directly pass your input document including the JavaScript to the PDFreactor Web Service instead of converting the HTML rendered by the browser. PDFreactor will process any JavaScript within your input document, as long as JavaScript processing (disabled by default) is enabled in your PDFreactor integration.

Upvotes: 0

Related Questions