The moose likes XML and Related Technologies and the fly likes JSP to PDF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "JSP to PDF" Watch "JSP to PDF" New topic


Boon Subra
Ranch Hand

Joined: Jul 18, 2001
Posts: 69
Hi All,

I generate a form dynamically filled form from the database and display it in the browser as a jsp file.

I need to provide a link in the jsp file like: "PDF Format" with the hyperlink. When I click on it it should generate the PDF of the same filled form.

I am aware that I should be using FOP to do it. Though I see lot of tutorials,I exactly cannot understand how the current jsp file should proceed.

Can anybody let me know ,what should be done after the jsp file is generated to integrate with FOP.

Bhuvana Bala
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
you don't use the JSP as input to FOP.
Instead create an XML document containing the data you want to put in the PDF (so the form data). FOP will use an XSL:FO file to generate your PDF using that data.

You could use the XML to populate the formfields on the JSP as well using the JSTL XML tags.
You might even be able to use the XSL:FO to generate HTML, removing the need for JSPs.

Balaji Loganathan
author and deputy

Joined: Jul 13, 2001
Posts: 3150
Originally posted by Bhuvana Bala:
Can anybody let me know ,what should be done after the jsp file is generated to integrate with FOP.

Adding to above post..
  • Once the form is submitted, create the XML file with the new form datas.
  • Feed this XML and the pre-generated XSLT to the FOP Processor.
  • Apache FOP has got an example on dynamically generating PDF with XML and XSL using servlet,Check it here and try to convert it to Javabean/JSP

  • Spritle Software Blogs
    Peter Venema

    Joined: Dec 02, 2002
    Posts: 6
    What I did was creating a custom tag that writes a "send as pdf" button. The tag stores its body as an xml document as soon as someone asks for the pdf doc.

    Next I created a struts action that converts this xml to pdf with the aid of a (partly self written) xsl document. This struts action uses the fop implementation from apache.

    The xsl document was the most time consuming to build. I paid most attention to tables in html. For my purpose it works fine.

    This approach needs some fiddling around with the format of the jsp tables.
    Boon Subra
    Ranch Hand

    Joined: Jul 18, 2001
    Posts: 69
    Hi ,
    Thanks.I am not able to figure out.

    I generated a jsp .(A table with 4 columns and 5 data)

    I provided a link n it. PDF Version.

    I also generated the xml document in the jsp page.
    I mean the xml format is in .jsp file and not .xml file.
    I try to use the xsl on the generated xml. I cant print the data rather it just prints the table header .

    Please help me out.
    I am first trying with simple xsl.Later I can change to xsl:fo.
    This is my jsp that generate xml format.

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="clientstyle.xsl"?>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

    int i=1;
    <logic:iterate id="uploadfile" name="upfiles">
    <%= i++%>
    <bean:write name="uploadfile" property="UPF_filename_mod1" />
    <bean:write name="uploadfile" property="UPF_filedate" />
    <bean:write name="uploadfile" property="UPF_filepage" />

    If I remove the highlighted line,it generates xml document.
    The xsl is as follows.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.1" xmlns:xsl="http://www/">
    <xsl:template match="/">
    <table border="1" align="center">
    <tr bgcolor="#CCCCCC">
    <th height="31" width="8%" align="center">
    <th class="texthead" height="31" width="44%" align="center">
    File Name
    <th class="texthead" height="31" width="34%" align="center">
    Submit Date
    <th class="texthead" height="31" width="14%" align="center">
    <xsl:for-each select="TeamReport/Files/download">
    <td><xsl:value-of select="TeamReport/Files/download/sno"/></td>
    <td><xsl:value-of select="TeamReport/Files/download/filename"/></td>
    <td><xsl:value-of select="TeamReport/Files/download/filedate"/></td>
    <td><xsl:value-of select="TeamReport/Files/download/filepage"/></td>

    Please help me out.
    Boon Subra
    Ranch Hand

    Joined: Jul 18, 2001
    Posts: 69

    With the code given above I was able to use XSL. Now I get the output same as the jsp.

    My doubt is:

    I have a jsp file that generates the xml file with xsl file./

    Before that,I need to parse the dynamic jsp file that fetches record from the database.

    If i use DOM ,how should I pass the value for the xml file like

    Document doc =
    traverseTree(doc, out);

    In the above example stocks.xml is a dynamic file which is not stored in any location.

    The task i want to do is to convert a jsp to PDF using FOP.

    I am able to print the jsp file in the form of xml .
    The problem is how to parse it dynamically.

    It would be helpful if anybody sends me the code.

    [ July 25, 2004: Message edited by: Boon Subra ]
    Boon Subra
    Ranch Hand

    Joined: Jul 18, 2001
    Posts: 69
    Hi can anybody help me out.

    Lasse Koskela

    Joined: Jan 23, 2002
    Posts: 11962
    You could try something like
    a) have another JSP page include the XML-generating JSP inside a custom taglib which performs the XSL-FO transformation
    b) have a servlet do RequestDispatcher#include(req, res) for the XML-generating JSP's URI and let the servlet perform the XSL-FO transformation on whatever the included JSP wrote to the ServletResponse object you passed on.

    Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
    I agree. Here's the link:
    subject: JSP to PDF
    It's not a secret anymore!