Tornado - Create a PDF using NodeJs via API

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({

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
        file.on("finish", () => {
            console.log(output, "created");
        // 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", () => {

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

// write data to request body


Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam. Please also consider contacting 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

API Overview

Docmosis offers a high-performance, template-based PDF generation API. Suitable for use with custom software applications built using Java, C#/.Net, Python, Ruby and more. See a side-by-side comparison of the API for our SaaS and self-hosted products.