Personal tools

xPages

Mar 08, 2013

Merging odf files using XDocReport and XPages

LET'S MERGE TOGHETER!

Merging odf files using XDocReport and XPages

Filed Under:

Often there is the need to create documents from templates, and the need to fill these templates with data available from other sources: let's take a look at this brief solution

In this scenario, we have a odf template to merge with data.

Working in the Lotus Domino environment, one way to achieve this goal could be a server-side component invoked by an XPagdies, to make the casting process: first, I need something in Java that could do the job for me, and after some search I came across XDocReport.

XDocReport means XML Document reporting

XDocReport provides Java API to merge XML document created with MS Office (docx, pptx) or OpenOffice (odt), LibreOffice (odt) with a Java model to generate report and convert it in another format (PDF, XHTML...).

Basicly, to make the template, main steps are:

  1. create a template document with MS Word (docx) or OpenOffice (odt, ods)
  2. compose the body of the document itself with variables to fill in
  3. use Velocity or Freemarker syntax to set variables to replace.


In this case, I opted to use Velocity inside an ODF file.

Now let's start by explaining how to set up the whole project.

Installing server libraries

External libraries (.jar) that serve the purpose have been installed in the installation directory of the Domino server (jvm/lib/ext path).

These are the files:

commons-collections-3.2.1.jar
commons-lang-2.4.jar
fr.opensagres.xdocreport.converter-1.0.0.jar
fr.opensagres.xdocreport.converter.odt.odfdom-1.0.0.jar
fr.opensagres.xdocreport.core-1.0.0.jar
fr.opensagres.xdocreport.document-1.0.0.jar
fr.opensagres.xdocreport.document.odt-1.0.0.jar
fr.opensagres.xdocreport.itext.extension-1.0.0.jar
fr.opensagres.xdocreport.template-1.0.0.jar
fr.opensagres.xdocreport.template.velocity-1.0.0.jar
itext-2.1.7.jar
odfdom-java-0.8.7.jar
org.odftoolkit.odfdom.converter-0.9.0.jar
org.odftoolkit.odfdom.converter.core-1.0.0.jar
org.odftoolkit.odfdom.converter.pdf-1.0.0.jar
org.odftoolkit.odfdom.converter.xhtml-1.0.0.jar
oro-2.0.8.jar
velocity-1.7.jar

Dont' forget an important security setting in java.policy file:

grant {
permission java.lang.RuntimePermission "getClassLoader";
permission java.lang.RuntimePermission "setContextClassLoader";
}

After that, restart HTTP server.

Server-side tools

To manage the whole process I used an HTML page with a button that calls (via Ajax) an XPages, that deals with the fusion of the data (HTML string) in the template. At the end, I create the PDF file using iText libraries.

Related to XPages, it only call the servlet in the afterPageLoad event:

com.redturtle.XDocReport.mergeAndPDF(
	facesContext.getExternalContext().getRequest(), 
	facesContext.getExternalContext().getResponse()
);

This is how it looks the package, which includes the servlet:

package

Finally, this is the servlet code:

package com.redturtle;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.odftoolkit.odfdom.converter.pdf.PdfConverter;
import org.odftoolkit.odfdom.converter.pdf.PdfOptions;
import org.odftoolkit.odfdom.doc.OdfTextDocument;

import fr.opensagres.xdocreport.core.document.SyntaxKind;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;

import lotus.domino.NotesException;
import lotus.domino.Session;

public class XDocReport {

	private static String VERSION = "XDocReport Version 1.0.0";
	static PrintWriter outClass = null;
	private static Session session = null;

	public static void mergeAndPDF (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, InterruptedException, NotesException {

                // NO DIIOP
		session=DominoAccess.getCurrentSession();
		try {			
			// 1) Load ODT file by filling Velocity template engine
			InputStream in = new FileInputStream("/tmp/template.odt");

			IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in,TemplateEngineKind.Velocity);

			FieldsMetadata metadata = report.createFieldsMetadata();
			metadata.addFieldAsTextStyling("comments",SyntaxKind.Html,false);

			// 2) Create context Java model
			IContext context = report.createContext();
			context.put("comments", "<b>hello world</b>");
			
			// 3) Generate report by merging Java model with the ODT
			OutputStream out = new FileOutputStream(new File("/tmp/ODTProjectWithVelocity_Out.odt"));
			report.process(context, out);

			// 1) Load ODT into ODFDOM OdfTextDocument 
			in= new FileInputStream(new File("/tmp/ODTProjectWithVelocity_Out.odt"));
			OdfTextDocument document = OdfTextDocument.loadDocument(in);

			// 2) Prepare Pdf options
			PdfOptions options = PdfOptions.create();
			
			// 3) Convert OdfTextDocument to PDF via IText
			out = new FileOutputStream(new  File("/tmp/ODTProjectWithVelocityList_Out.pdf"));
			PdfConverter.getInstance().convert(document, out, options);
 
                        // Only manage response to Ajax call
		        response.setContentType("text/plain");
		        response.setCharacterEncoding("UTF-8");
			outClass = new PrintWriter(response.getWriter());
			printAgent("OK");
			return;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected static void printAgent(String string) {
		outClass.println(string);
	}
}

Pay attention to these points:

  1. comments - variable name inside ODF template
  2. metadata.addFieldAsTextStyling("comments",SyntaxKind.Html,false); - fill variable with something in HTML syntax
  3. <b>hello world</b> - HTML string to merge

NOTE: DominoAccess and JSFUtil are two very helpful classes written by Karsten Lehmann: see the full code. Thanks to them, DIIOP process for communication is not required.

Pro & Cons.

I think that it's a very powerful approach; the only limitation I've found is the restricted set of styles supported by the merge process (see there for details). Feel free to make some test using this approach, and let me know what you think :-)

read more

Nov 28, 2012

Mixing Lotus Domino and Twitter together - a way for mobile approach

"Mobilize" your Lotus Domino

Mixing Lotus Domino and Twitter together - a way for mobile approach

Filed Under:

What if you want to create different layouts for different devices in Lotus Domino? This is a way to make this possible, for example using Twitter Bootstrap

It's always more important to develop applications that are accessible from any device so we wondered what it takes to adapt an application, perhaps already developed, to ensure this possibility. In this case, Twitter provides its layout via Twitter Bootstrap, so it was decided to integrate it.

read more

Oct 18, 2012

How to integrate D3 graphic library in Lotus Domino environment

Graphs inside XPages

How to integrate D3 graphic library in Lotus Domino environment

Filed Under:

A simple example of how to easily integrate graphics library D3 in Lotus Domino, and generate interactive charts based on the data exposed by a view of a XPages

During Better Software (http://www.bettersoftware.it/) held in September in Florence, I attended many interesting talks, including one held by Luca Mearelli (the link to the presentation is available at http://spazidigitali.com/d3js_at_bsw12/) which dealt with the graphics library Javascript D3 (http://d3js.org/)

This is just a brief example of how to integrate D3 into a framework (Lotus Domino is what I use), but there is nothing that we can do the same thing anywhere else.
I thought it was interesting to understand how you can integrate this library in a Lotus Domino XPages, in particular for manipulating data exposed by views, forms, and more.

 

D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietary framework, combining powerful visualization components and a data-driven approach to DOM manipulation.

read more

Apr 06, 2011

DIIOP?? No more...

Filed Under:

I'm porting servlet code running on 6.5 Domino Server in XPages.

Servlet comunicate with Domino through DIIOP, and need DIIOP to istantiate Session object:

lotus.Domino.session = NotesFactory.createSession(server,login, password);

Thanks to Karsten Lehmann for its great library:

http://www.mindoo.com/web/blog.nsf/dx/18.07.2009191738KLENAL.htm?opendocument&comments

Now i can initialize a session using:

lotus.Domino.session = DominoAccess.getCurrentSession();

No more DIIOP needed!!

Oct 27, 2010

Forms with the same alias is *still* a bad practice (Xpages Bug)

Filed Under:

Probably a bug found in XPages 8.5.2 engine

There is not to boast, but perhaps the development team RedTurtle found a bug in the engine of XPages 8.5.2.

It's happened during the development of an application where we had to connect a datasource to a Xpage. It appears that, in case you have two forms with the same alias, the list of available fields always refer to the first of two forms that is found in the list (we know, it's a very bad practice assigning same aliases to different forms, but we inherit this scenario from someone else...)

Only renaming the alias you obtain the right binding of the fields, even if the engine allows you to refer to the two blur.

By submitting the matter to two of the leading experts on the subject, Bob Balfe and John Mackey, we had confirmation that the problem exists.

UI Architect for IBM Workplace Designer has been alerted and will investigate the problem.

You will be posted ...