Dynamic PDF Generation with Cocoon 2.2.0

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

Be the first to comment

Leave a Reply

Your email address will not be published.


*