This example shows how to use Docmosis to render a url-encoded form 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.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
/**
* This sample code shows how to use Docmosis to render a url-encoded form.
*
* The "renderForm" end-point is supported from Tornado version 2.5
*
* Copyright Docmosis 2017
*/
public class JavaRenderURLEncoded
{
private static final String URL = "http://localhost:8080/api/renderForm";
/*
* Run this example
*/
public static void main(String[] args) throws MalformedURLException,
IOException
{
// Set your access Key if you configure it in Tornado
final String accessKey = "";
final String outputFileName = "myWelcome.pdf";
HttpURLConnection conn = null;
DataOutputStream os = null;
try {
conn = (HttpURLConnection) new URL(URL).openConnection();
System.out.println("Connecting [directly] to " + URL);
// set connection parameters
conn.setRequestMethod("POST");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setDoInput(true);
// use a url-encoded form.
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.connect();
System.out.println("Connected");
// build request
final String templateName = "WelcomeTemplate.docx";
final String data = "{\"title\":\"Hello from Tornado using Java\",\"messages\":[{\"msg\":\"This Docmosis experience is better than I thought.\"},{\"msg\":\"The sun is shining.\"},{\"msg\":\"Right, now back to work.\"}]}";
final String params =
// docmosis parameters
"accessKey=" + URLEncoder.encode(accessKey, "UTF-8")
+ "&templateName=" + URLEncoder.encode(templateName, "UTF-8")
+ "&outputName=" + URLEncoder.encode(outputFileName, "UTF-8")
// data parameter
+ "&data=" + URLEncoder.encode(data, "UTF-8")
;
System.out.println("Sending request:" + params);
final byte[] postData = params.getBytes( Charset.forName("UTF-8") );
// send the parameters
os = new DataOutputStream(conn.getOutputStream());
os.write(postData);
os.flush();
// get the response
int status = conn.getResponseCode();
if (status == 200) {
// successful call, extract the response
// successful render,
// save our document to a file
byte[] buff = new byte[4096];
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.exit(2);
} finally {
if (os != null) {
os.close();
}
if (conn != null) {
conn.disconnect();
}
}
}
}
