Browsing the blog archives for February, 2009.

Dynamic PDF Generation with Cocoon 2.2.0

technical

A couple of things are different from using cocoon 2.1 to cocoon 2.2. Compiling and rendering to a war file for use on tomcat is all done through maven2. The following is a summary that I have gone through and confirmed working as of the time of writing.

apt-get install maven2

mvn archetype:generate -DarchetypeCatalog=http://cocoon.apache.org

Define value for groupId: : local.cocoon
Define value for artifactId: : myBlock1
Define value for version:  1.0-SNAPSHOT: : 1.0.0
Define value for package: : local.cocoon.myBlock1

cd myBlock1

mvn jetty:run

http://192.168.0.1:8888/myBlock1

vim src/main/resources/COB-INF/sitemap.xmap

throw the following inside the pipline tags

      <map:match pattern="myFile.xml">
        <map:generate src="myXmlFile.xml" type="file"/>
        <map:serialize type="xml"/>
      </map:match>
      <map:match pattern="myFile.html">
        <map:generate src="myXmlFile.xml" type="file"/>
        <map:transform src="myHtmlFile.xslt" type="xslt"/>
        <map:serialize type="html"/>
      </map:match>
      <map:match pattern="myFile.pdf">
        <map:generate src="myXmlFile.xml" type="file"/>
        <map:transform src="myPdfFile.xslt" type="xslt"/>
        <map:serialize type="fo2pdf"/>
      </map:match>

create myXmlFile.xml in the same folder as sitemap

vim src/main/resources/COB-INF/myXmlFile.xml

<?xml version="1.0" encoding="UTF-8"?>
<content>test</content>

create myHtmlFile.xslt in the same folder as sitemap

vim src/main/resources/COB-INF/myHtmlFile.xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <head>
        <title>My second XML Pipeline</title>
      </head>
      <body>
        My second XML Pipeline:
        <xsl:value-of select="/content"/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

create myPdfFile.xslt in the same folder as sitemap

vim src/main/resources/COB-INF/myPdfFile.xslt

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="page"
          page-height="29.7cm"
          page-width="21cm"
          margin-top="1cm"
          margin-bottom="2cm"
          margin-left="2.5cm"
          margin-right="2.5cm">
          <fo:region-before extent="3cm"/>
          <fo:region-body margin-top="3cm"/>
          <fo:region-after extent="1.5cm"/>
        </fo:simple-page-master>

        <fo:page-sequence-master master-name="all">
          <fo:repeatable-page-master-alternatives>
            <fo:conditional-page-master-reference
             master-reference="page" page-position="first"/>
          </fo:repeatable-page-master-alternatives>
        </fo:page-sequence-master>
      </fo:layout-master-set>

      <fo:page-sequence master-reference="all">
        <fo:static-content flow-name="xsl-region-after">
          <fo:block text-align="center"
            font-size="10pt"
            font-family="serif"
            line-height="14pt">page <fo:page-number/></fo:block>
        </fo:static-content>

        <fo:flow flow-name="xsl-region-body">
          <fo:block font-size="36pt" space-before.optimum="24pt"
           text-align="center">
             My second XML Pipeline
          </fo:block>
          <fo:block font-size="12pt" space-before.optimum="12pt"
           text-align="center">
           <xsl:value-of select="/content"/>
          </fo:block>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
</xsl:stylesheet>

edit pom.xml to include FOP
vim pom.xml

    <dependency>
      <groupId>org.apache.cocoon</groupId>
      <artifactId>cocoon-fop-impl</artifactId>
      <version>1.0.0</version>
    </dependency>

mvn compile

mvn install

cd ..

mvn archetype:generate -DarchetypeCatalog=http://cocoon.apache.org

Define value for groupId: : local.cocoon
Define value for artifactId: : cocoon-webapp
Define value for version: 1.0-SNAPSHOT: : 1.0.0
Define value for package: : local.cocoon.cocoon-webapp

cd cocoon-webapp

vim pom.xml

add the dependancy
      <dependency>
        <groupId>local.cocoon/groupId>
        <artifactId>myBlock1</artifactId>
        <version>1.0.0</version>
      </dependency>

mvn compile

mvn war:war

cd target

upload this war file to your tomcat server.

load the following url

http://192.168.0.1:8180/cocoon-webapp-1.0.0/myBlock1/myFile.pdf

http://192.168.0.1:8180/cocoon-webapp-1.0.0/myBlock1/myFile.html

http://192.168.0.1:8180/cocoon-webapp-1.0.0/myBlock1/myFile.xml

if all three loads, you have a dynamically generated pdf! (and html and xml)

next step is to force cocoon to retrieve the files remotely, by doing so, the content can be retrieved from a database of choice using your own desirable programming language of choice! In my case php+postgres/mysql and .net+sql/access

No Comments

True Dynamic PDF Generation

technical

I did this about 2 years ago. Not much has changed but there has been more steps involved due to security issues and bug fixes. Here’s the current instructions as they currently work today.


apt-get install sun-java6-jdk
apt-get install tomcat5.5
apt-get install tomcat5.5-admin
apt-get install tomcat5.5-webapps
vim /etc/tomcat5.5/tomcat-users.xml

add the following (not sure why admin and manager isn’t defined)

<role rolename=”manager”/>
<role rolename=”admin”/>
<user username=”####” password=”####” roles=”tomcat,role1,manager,admin”/>

restart
/etc/init.d/tomcat5.5 restart
open browser (no trailing forward slash at the end)
https://ubuntu.local:8180
should anything during the installation process go wrong, you can repeat the installation by purging the old files through apt-get –purge autoremove tomcat5.5, correct the mistake/problem, and retry the installation. for me, I had multiple java instances problems and having defined JAVA_HOME when it isn’t needed (in this setup scenario it is automatically found)

PART 2
setting up cocoon download cocoon from http://cocoon.apache.org/mirror.cgi
<code>
wget http://east.unified.net/apache/cocoon/cocoon-2.1.11-src.tar.gz
tar -xvf cocoon-2.1.11-src.tar.gz
cd cocoon-2.1.11
./build.sh war
</code>
i got warnings of jpegencoder funnctions. i don’t need them.
cd build/cocoon
your cocoon.war file is here. use the manager interface to upload the cocoon file.
https://ubuntu.local:8180
I have encountered a permission issue while browsing the catalina log file. to resolve this I had to add the following
vim /etc/tomcat5.5/policy.d/04webapps.policy
<pre>grant codeBase “file:${catalina.home}/bin/tomcat-juli.jar” {
permission java.io.FilePermission “/var/lib/tomcat5.5/webapps/cocoon/WEB-INF/classes/logging.properties”, “read”;
permission java.io.FilePermission “/usr/share/tomcat5.5-webapps/jsp-examples/WEB-INF/classes/logging.properties”, “read”;
permission java.io.FilePermission “/usr/share/tomcat5.5-webapps/servlets-examples/WEB-INF/classes/logging.properties”, “read”;
};</pre>
security errors were also encountered on page load of: http://192.168.0.1:8180/cocoon/ easiest fix was to turn off security checks.
enable this option:
vim /etc/default/tomcat5.5
add this:
TOMCAT5_SECURITY=no
restart tomcat:
/etc/init.d/tomcat5.5 restart
view the sample pdf as included by cocoon:
http://192.168.0.1:8180/cocoon/samples/blocks/itext/hello.pdf
if you browse to the directory you’ll notice that hello.pdf does not exist. Instead a sitemap.xmap helps define what hello.pdf should be generated from.
context://samples/hello-world/content/hello.xml
defines the data location

context://samples/hello-world/style/xsl/page2itext.xsl

defines how the xml should be represented

&lt;<map:serialize type=”itext2pdf”/&gt;
defines the transformation mode

For our purposes we will be using XSL-FO so the transformer is:
&lt;map:serialize type=”fo2pdf”/&gt;

That’s it! you’ve got dynamic pdf generator. now you just have to learn the XSL-FO tags to create your pdf! A possible followup to this write up will cover query string and retriving source files remotely in a dynamic fashion. By doing so, you will then have the ability to create free dynamic PDF from any platform like asp.net websites!

No Comments

CES 2009 footage in 3D

technical

A month has past since the CES 2009. As promised I filmed various booths in 3D. Unfortunately I was unable to film all booths nor in depth for those that I do have usable footage of. I am in progress of transferring the footage. Will update progress here.

1 Comment