Tornado Render using Java (With JSON data)

This example shows how to generate a PDF from a DOC template using Java.  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.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

/**
 * This sample code shows a render of the "WelcomeTemplate.doc" template using JSON format
 * instruction and data.
 *
 * If you would like to render via a web proxy, see the more complete example which provides proxy settings.
 * 
 * Copyright Docmosis 2015
 */
public class JavaBasicJSONExample 
{

	private static final String DWS_RENDER_URL = "http://localhost:8080/api/render";

	/*
	 * Run this example
	 */
	public static void main(String[] args) throws MalformedURLException,
			IOException 
	{
		// Set your access Key if you configure it in Tornado
		String accessKey = "";

		HttpURLConnection conn = null;
		try {
			conn = (HttpURLConnection) new URL(DWS_RENDER_URL).openConnection();
			System.out.println("Connecting [directly] to " + DWS_RENDER_URL);

			// set connection parameters
			conn.setRequestMethod("POST");
			conn.setUseCaches(false);
			conn.setDoOutput(true);
			conn.setDoInput(true);

			// this example uses JSON format
			conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");

			conn.connect();
			System.out.println("Connected");

			final String outputFileName = "myWelcome.doc";

			// build request
			String templateName = "WelcomeTemplate.doc";
			StringBuffer sb = new StringBuffer();

			// Start building the instruction
			sb.append("{\n");
			sb.append("\"accessKey\":\"").append(accessKey).append("\",\n");
			sb.append("\"templateName\":\"").append(templateName).append("\",\n");
			sb.append("\"outputName\":\"").append(outputFileName).append("\",\n");

			// now add the data specifically for this template
			sb.append("\"data\":{\n");
			sb.append("\"date\":\"").append(new Date()).append("\",\n");
			sb.append("\"message\":\"This Tornado Document Engine is working great!\",\n");
			sb.append("}\n");
			sb.append("}\n");

			System.out.println("Sending request:" + sb.toString());

			// send the instruction in UTF-8 encoding so that most character sets are available
			OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
			os.write(sb.toString());
			os.flush();
			
			int status = conn.getResponseCode();
			if (status == 200) {
				// successful render,
				// save our document to a file
				byte[] buff = new byte[1000];
				int bytesRead = 0;

				File file = new File(outputFileName);
				FileOutputStream fos = new FileOutputStream(file);
				try {
					while ((bytesRead = conn.getInputStream().read(buff, 0, buff.length)) != -1) {
						fos.write(buff, 0, bytesRead);
					}
				} finally {
					fos.close();
				}

				System.out.println("Created file:" + file.getAbsolutePath());
			} else {
				// something went wrong - tell the user
				System.err.println("Our call failed: status = " + status);
				System.err.println("message:" + conn.getResponseMessage());
				BufferedReader errorReader = new BufferedReader(
						new InputStreamReader(conn.getErrorStream()));
				String msg;
				while ((msg = errorReader.readLine()) != null) {
					System.err.println(msg);
				}
				errorReader = null;
			}
			
		} catch (ConnectException e) {
			// can't make the connection
			System.err.println("Unable to connect to Docmosis:" + e.getMessage());
			System.err.println("If you have a proxy, you will need the Proxy aware example code.");
			System.exit(2);
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
		}

	}
}

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 -