Inforama API Examples

Generating Documents via SOAP Calls



When you have created your projects, templates and data fields and are successfully generating documents through the Inforama Studio, you have really only started to scratch the surface of what can be done. To really bring your documents to life you can hook up to the SOAP service and generate documents automatically from other systems.



The Inforama document generator WSDL can be found at...

https://www.inforama.com/services/DocumentGenerator?wsdl

In order to use the Soap Service you will need to pass your username and API key as part of each service call. You can find your API key under the account section of the studio



Sample Java Project

In this section we will go through the process of creating a Java project that will make calls to the Inforama Document Generation Service. In this example we will generate the SOAP stubs using version 2.7.0 of the Apache CXF library. You can download the latest version of the Apache CXF library from http://cxf.apache.org/download.html



Create a batch file which contains the following commands...



"C:\Downloads\Apache\CXF\apache-cxf-2.7.0\bin\wsdl2java.bat" -frontend jaxws21 -d src https://www.inforama.com/services/DocumentGenerator?wsdl


The jaxws21 instruction will produce code which is JaxWS 2.1 compatible. When executed the Java stub classes will be created is the src directory. We can then use these generated stubs to make webservice calls.



Start by creating a properties file called generate.properties that contains all of the information you will use in the call.


USERNAME=[INFORAMA_USERNAME]
PASSWORD=[INFORAMA_API_KEY]
WS_ADDRESS=https://www.inforama.com/services/DocumentGenerator?wsdl
PROJECT_NAME=[PROJECT_NAME]
DOC_NAME=[TEMPLATE_NAME]
DOC_TYPE=[TEMPLATE_TYPE]
DATASET=[DATAFIELDS_NAME]
DATA_FILE=[DATAFILE_LOCATION]
OUTPUT_FILE=[OUTPUT_LOCATION]



Property Description
USERNAME Your www.inforama.com login name
PASSWORD Your www.inforama.com API key. Can be obtained from the account section of the studio
WS_ADDRESS The URL of the Inforama document generation WSDL...
https://www.inforama.com/services/DocumentGenerator?wsdl
PROJECT_NAME The name of your Inforama project that you wish to generate documents from
DOC_NAME The name of the template that you wish to use
DOC_TYPE The type of the template (for example, PDF)
DATASET The name of the data fields from which the template is bound
DATA_FILE The location on your local machine where the file containing your data is located
OUTPUT_FILE The full path and name of the output file (ZIP format)



Next create a Java class that will use this properties file to call the Inforama service.


			package org.inforama.test;

			import java.io.FileInputStream;
			import java.io.FileOutputStream;
			import java.io.FileReader;
			import java.io.IOException;
			import java.net.MalformedURLException;
			import java.net.URL;
			import java.util.Map;
			import java.util.Properties;

			import javax.net.ssl.HttpsURLConnection;
			import javax.xml.ws.BindingProvider;

			import org.in4ama.documentservice.DocumentGenerator;
			import org.in4ama.documentservice.DocumentGeneratorImplService;
			import org.in4ama.documentservice.InlineDataRequest;
			import org.in4ama.documentservice.InlineDataResponse;

			public class SoapTest {
				
				Properties props;
				
				public static void main(String[] args)	{
					new SoapTest().callService();
				}
				
				private void callService()	{
					
					try {
					
						loadProperties();
						
						String wsdlLocation = props.getProperty("WS_ADDRESS");
						DocumentGeneratorImplService service = new 
							DocumentGeneratorImplService(new URL(wsdlLocation));
						DocumentGenerator docGen = 
							service.getDocumentGeneratorImplPort();
						
						Map req_ctx = ((BindingProvider)docGen).getRequestContext();

						String urlUsed = (String) req_ctx.
								get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
					 
						req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY
							, wsdlLocation);
						req_ctx.put(BindingProvider.USERNAME_PROPERTY
							, props.getProperty("USERNAME"));
						req_ctx.put(BindingProvider.PASSWORD_PROPERTY
							, props.getProperty("PASSWORD"));

						InlineDataRequest request = createRequest();
						InlineDataResponse response = docGen.generateBatch(request);

						extractResponse(response);

					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
					
				}
				
				private void extractResponse(InlineDataResponse response)	{
					byte[] bytes = response.getResponseDataHandler();
					
					String outputFileName = props.getProperty("OUTPUT_FILE");
					try {
						FileOutputStream os = new FileOutputStream(outputFileName);
						os.write(bytes);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				private InlineDataRequest createRequest()	{
					InlineDataRequest request = new InlineDataRequest();
					request.setProjectName(props.getProperty("PROJECT_NAME"));
					request.setDocName(props.getProperty("DOC_NAME"));
					request.setDocType(props.getProperty("DOC_TYPE"));
					request.setDatasetName(props.getProperty("DATASET"));
					
					String file = props.getProperty("DATA_FILE");
					try {
						FileInputStream fis = new FileInputStream(file);

						byte[] bytes = new byte[fis.available()];
						fis.read(bytes);
						request.setUploadedDataDocHandler(bytes);
					} catch (Exception e) {
						e.printStackTrace();
					}
					return request;
				}

				private Properties loadProperties()	{
					props = new Properties();
					try {
						FileReader reader = new FileReader("generate.properties"); 
						props.load(reader);
						return props;
					} catch (Exception e) {
						e.printStackTrace();
						return null;
					}
				}
			}
										



In this sample class the callService method is called from main. From here the loadProperies function loads the generate.properties file created earlier. The createRequest function creates a new InlineDataRequest object which will be passed to the service. The username and API Key are passed as part of the HTTP headers and the service call is made. When the service call returns the contents of the zip are extracted in the extractResponse method.




And that's it, the service has been called and has returned our documents neatly in a zip archive. This mechanism will allow you to automate your documents from the desktop or by copying the code you can integrate with your existing systems.