Cloud 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 and then plug your Docmosis Cloud access key into the code below, then run.

// This Node.js example calls the Docmosis web service to create a PDF document
// from the default template (samples/WelcomeTemplate.docx).
// To run:
//  1) plug your Docmosis access key into the accessKey variable
//
// Copyright Docmosis 2017
//

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

// RESTful service host
const hostname = "dws2.docmosis.com";

// Your account key
const accessKey = "XXXXX";

// make sure the access key above has been set
if (accessKey == "XXXXX") {
    console.log("Please set your access key in this example");
    process.exit();
}

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

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

// The data to use for generating the document
const data = {
    title:"Welcome to Docmosis in the Cloud",
    messages:[
        {"msg":"This cloud experience is better than I thought."},
        {"msg":"The sun is shining."},
        {"msg":"Right, now back to work."}
    ]
}

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

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

const req = https.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.

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) - - DWS2