Tornado Render using Node.JS

This example shows how to generate a PDF from a DOC template using Node.JS.  It calls the Docmosis REST API to merge the data with the template and stream the result back.

The sample code includes the instructions to get started. You will need a Free Trial to install and run Tornado.

// This Node.js example calls the Docmosis Tornado web service to create a PDF document
// from the default template (WelcomeTemplate.doc).
//
// Copyright Docmosis 2018
//

const http = require("http");
const querystring = require("querystring");
const fs = require("fs");

// RESTful service host
const hostname = "localhost";
const port = "8080"

// Your account key
const accessKey = "";

// The template to use
// NOTE that it has to be defined in your account with the same name specified here
const template = "WelcomeTemplate.doc"; // Change with your actual template name

// The output file name
const output = "myWelcome.pdf";

// The data to use for generating the document
const data = {
    date:new Date().toDateString(),
    message:"This message was inserted into the data on " + new Date()
}

const postData = querystring.stringify({
    "accessKey":accessKey,
    "templateName":template,
    "outputName":output,
    "data":JSON.stringify(data)
});

const options = {
    hostname: hostname,
    port: port,
    path: "/api/render",
    method: "POST",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Content-Length": Buffer.byteLength(postData)
    }
};


const req = http.request(options, (res) => {
    switch (res.statusCode) {
    case 200:
        var file = fs.createWriteStream(output);

        // feed response into file
        res.pipe(file);
        file.on("finish", () => {
            file.close();
            console.log(output, "created");
        });
        break;
    default:
        // 4XX errors - client errors - something needs to be corrected in the request
        // 5XX errors - server side errors - possibly worth a retry

        // show error response (details)
        console.log("Error response:", res.statusCode, res.statusMessage);
        var response = "";
        res.on("data", (data) => {
            response += data;
        });
        res.on("end", () => {
            console.log(response);
        });
    }
});

req.on("error", (e) => {
    console.error("Request error:", JSON.stringify(e, null, 4));
});

// write data to request body
req.write(postData);
req.end();

Feedback

Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam. Please also consider contacting support@docmosis.com if you need help with this article.

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Invalid Input

Applies To

Docmosis-Java Tornado Cloud
Version(s) - ALL -