Home JBoss Server JBoss JMX Collection: how to create a PDF from text
11 | 03 | 2010
JBoss 5 AS Book
"JBoss AS 5 development" reviews
Please share your feedback/review with other readers!
Banner
Dashboard
Advertise with Us
Banner
RSS Feed
Login
Sign here for the NewsLetter.



Poll
What book could be in your wish list next XMas ?
 
JBoss admin resources
Banner
JBoss howto

How can you solve deployment errors caused by large war/jar/ear files ?

jboss recipe of the day ...
Read More

How do you configure your .war to be deployed after your EJB ?

jboss recipe of the day ...
Read More

How do I configure a Queue/Topic to work in a cluster?

JBoss recipe of the day ...
Read More
JBoss JMX Collection: how to create a PDF from text PDF Print E-mail
Written by F.Marchioni   
Thursday, 10 December 2009 14:59
Starting from now we will collect all interesting MBeans and POJO Services and make available to readers. In this tutorial we will show a POJO Service that transforms a text file into a PDF table using the iText library

The prerequisite of this sample is the iText library which needs to be downloaded from:

http://itextpdf.com/

iText is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.
 

  • Download the iText1.2.X.jar library and add to the application server library path.

To create a service POJO all you need is adding a few annotations to your POJO Class, which will act as a singleton service.

Here's the service POJO Class:
 
package com.sample;

import org.jboss.ejb3.annotation.Management;
import org.jboss.ejb3.annotation.Service;

@Service(objectName = "textToPdf:service=PDFConverter")
@Management(PDFConverter.class)
public class PDFConverterService implements PDFConverter{
 
		// Lifecycle methods
		public void create() throws Exception {} 

		@Override
		public void createReportPDF(String inputfileName,String outputfileName) {
			 try {
				PDFCreator.createReportPDF(inputfileName,outputfileName);
			} catch (Exception e) {
				 
				e.printStackTrace();
			} 
			
		}
		 	 	 
}
What is really interesting about the POJO service are the two annotations:
@Service(objectName = "textToPdf:service=PDFConverter")
@Management(PDFConverter.class)


With the first one (@Service) you are binding the POJO in the textToPdf domain using the PDFConverter service name

The Second annotation (@Management) contains the interface name which will be exposed to the client. The interface is nothing more than a plain Java interface:
package com.sample;

public interface PDFConverter {
	public void createReportPDF(String inputfileName,String outputfileName);	
}

The real class which does the job of converting a text file into a PDF formatted table is PDFCreator which basically reads the input file name stores the content into an Array using the "|" as separator between fields. 

package com.sample;

import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class PDFCreator {

 
	public static void createReportPDF(String inputfileName, String outputfileName) throws DocumentException, IOException {
		  ArrayList mainList = new ArrayList();
          
		  // READ TEXT FILE
		  BufferedReader in = new BufferedReader(new FileReader(inputfileName));
		  String line;
		  
		  int rows=0;
		  int columns=0;
		  
		  while((line = in.readLine()) != null) {  
		      ArrayList subList = new ArrayList();
			  StringTokenizer st = new StringTokenizer(line,"|");   
			  while (st.hasMoreElements()) {
				 String cell = st.nextToken();
				 subList.add(cell);
				 columns=subList.size();
		      }
			  mainList.add(subList);
		  }	  
			  
		  in.close();     
		  rows=mainList.size();
		  
			
		// CREATE PDF DOCUMENT 
		Document document = new Document();
		PdfWriter.getInstance(document, 
			new FileOutputStream(outputfileName));
		document.open();
		
		PdfPTable table = new PdfPTable(columns); 
        for (int i=0;i<mainList.size();i++) {
        	ArrayList subList = (ArrayList)mainList.get(i);
        	for (int y=0;y<subList.size();y++) {
        		String cell = (String)subList.get(y);
        		table.addCell(cell);
        	}
        }
		
		 
		document.add(table);		
		document.close();

		
	}
     
}
Deploying the Service POJO is just a matter of packaging the files into a JAR archive and copying the archive into the deploy folder of JBoss AS. The following output will be produced on the JBoss AS Console:


17:04:36,364 INFO  [JBossASKernel] installing bean: jboss.j2ee:jar=JBossServiceP
DF.jar,name=PDFConverterService,service=EJB3
17:04:36,364 INFO  [JBossASKernel]   with dependencies:
17:04:36,380 INFO  [JBossASKernel]   and supplies:
17:04:36,380 INFO  [JBossASKernel]      Class:com.sample.PDFConverter
17:04:36,380 INFO  [JBossASKernel]      jndi:PDFConverterService/remote
17:04:36,380 INFO  [JBossASKernel] Added bean(jboss.j2ee:jar=JBossServicePDF.jar
,name=PDFConverterService,service=EJB3) to KernelDeployment of: JBossServicePDF.jar
17:04:36,427 INFO  [EJBContainer] STARTED EJB: com.sample.PDFConverterService ejbName: PDFConverterService

For testing the POJO Service you can navigate to the JMX Console and add the input and output file name:

This is a sample of input file:

column1|column2|column3
aaaaaaa|bbbbbbb|ccccccc
ddddddd|eeeeeee|fffffff
ggggggg|hhhhhhh|iiiiiii


This is the PDF output:
jboss pdf java
Resources:
Service POJO Tutorial

JBoss.org Search
Custom Search
Comments
Search
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Thursday, 10 December 2009 16:27 )