Friday, April 5, 2013

PDF Generation with XSL - FOP

Create report in PDF format using XSLFO and Apache FOP

XSLFO is XSL Formatting Objects and can be used for formatting XML data.Apache FOP (Formatting Objects Processor) is a Java application that reads a formatting objects tree and renders the resulting pages to a specified output (PDF in our case)

Requires:

- XSLT document (.xsl) with the :fo directives embedded into it. This XSLT document can be used in a java program to output the XSL-FO document 
- XML data or Java Object (convert Java Object to XML)
- Use Apache FOP driver to run the FO document against that Xml data to produce the PDF by using an XSLT Processor

Two stages of process

- XSLT to XSL-FO 
- XSL-FO to PDF

That process is similar with the following command of Apache FOP
Windows: fop -xml name.xml -xsl name2fo.xsl -pdf name.pdf

Required Jars

  1. xml-apis.jar
  2. jaxb-api-2.2.3.jar
  3. fop-1.0.jar
  4. avalon-framework-4.1.3.jar
  5. commons-io-2.0.1.jar
  6. commons-logging-1.0.3.jar
  7. xmlgraphics-commons-1.4.jar

XSLT Processor/Engine

XSLT processor aims for transforming XML documents into HTML, text, or other XML document types.

Apache FOP use JAXP as default factory.Avoid relying on the JAXP factory mechanism for selecting your transformation engine. Instead load the engine you want explicitly: it's much more reliable and much faster. 

For Saxon, replace the call on TransformerFactory.newInstance()
with new net.sf.saxon.TransformerFactoryImpl()
 

For Xalan use
new org.apache.xalan.processor.TransformerFactoryImpl()

Generate font metadata & for embedding fonts

Use Apache FOP to generate font metadate
- Setup classpath to run Apache FOP:
cmd> set CLASSPATH=fop.jar;lib\avalon-framework-4.2.0.jar;lib\commons-logging-1.0.4.jar;lib\commons-io-1.3.1.jar; lib\xmlgraphics-commons-1.4.jar

- Create the metrics file:
cmd> java org.apache.fop.fonts.apps.TTFReader FontName.TTF FONTNAME.xml


Custom configuration & embed fonts in jar

Create  conf.xml

<font metrics-url="/fonts/FontName.xml" kerning="yes" embed-url="/fonts/FontName.ttf">
       <font-triplet name="FontName" style="normal" weight="normal" />
       font-triplet name="FontName" style="italic" weight="normal" />
 </font>
             
<font metrics-url="/fonts/FontName.xml" kerning="yes" embed-url="/fonts/FontName.ttf">
     <font-triplet name="FontName" style="italic" weight="normal" />
</font> 



Reference:

http://xmlgraphics.apache.org/fop/ 
http://www.e-zest.net/blog/integrating-apache-fop-with-java-project-to-generate-pdf-files/ 


Moodle Upgrading

Install - Upgrade from 1.9 to 2.x

 

1. PHP Requirements

You must set register_globals=off.
set in php.ini
register_global = Off
require: php 5.2.24

2. Change Existing DB into UTF-8

mysqldump -uusername -ppassword -c -e --default-character-set=utf8 --single-transaction --skip-set-charset --add-drop-database -B dbname > dump.sql
cp dump.sql dump-fixed.sql
vim dump-fixed.sql
:%s/DEFAULT CHARACTER SET latin1/DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci/
:%s/DEFAULT CHARSET=latin1/DEFAULT CHARSET=utf8/
:wq
mysql -uusername -ppassword < dump-fixed.sql

3. Clone Moodle - http://docs.moodle.org/24/en/Git_for_Administrators


$ git clone git://git.moodle.org/moodle.git                     
$ cd moodle
$ git branch -a                                                  
$ git branch --track MOODLE_20_STABLE origin/MOODLE_20_STABLE    
$ git checkout MOODLE_20_STABLE                                   


You can upgrade to Moodle 2.2 from Moodle 2.1, 2.0 or 1.9. If you are upgrading from 2.0, please also read Upgrading to Moodle 2.1 . If upgrading from 1.9, please also read that and Upgrading to Moodle 2.0. After reading this you can upgrade directly to 2.2.

4. Access your Moodle (ie: http://localhost/moodle)

- It will automatically update

5. Checkout again

(ie: MOODLE_21_STABLE, MOODLE_22_STABLE, ...)
do step 3
- $ git checkout MOODLE_21_STABLE
do step 4 
- $ git checkout MOODLE_22_STABLE
do step 4 

till the stable version of moodle that you one & done.
So you both upgrade database as well as source code of moodle.

Thursday, April 4, 2013

Eclipse IDE

Display line numbers in Eclipse

In Eclipse IDE, select “Windows” > “Preference” > “General” > “Editors” > “Text Editors” , check on the “Show line numbers” option.
line numbers in Eclipse
See result.
line number in eclipse editor

Ref: http://www.mkyong.com/eclipse/how-to-display-line-numbers-in-eclipse/

Tuesday, April 2, 2013

Debugging with XStream

Problem with some data in a large object graph, how to find where the problem was residing.
When the objects didn't define a toString method, we can use XStream to get a representation of the object graph and dump it to the console

new XStream().toXML(objectGraph, System.out);
9:04 PMtech-mashup