This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "JSP to PDF" Watch "JSP to PDF" New topic
Author

JSP to PDF

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.

Thanks,
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.


42
Balaji Loganathan
author and deputy
Bartender

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
    Greenhorn

    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;
    %>
    <TeamReport>
    <Files>
    <logic:iterate id="uploadfile" name="upfiles">
    <download>
    <sno>
    <%= i++%>
    </sno>
    <filename>
    <bean:write name="uploadfile" property="UPF_filename_mod1" />
    </filename>
    <filedate>
    <bean:write name="uploadfile" property="UPF_filedate" />
    </filedate>
    <filepage>
    <bean:write name="uploadfile" property="UPF_filepage" />
    </filepage>
    </download>
    </logic:iterate>
    </Files>
    </TeamReport>

    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/w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
    <body>
    <table border="1" align="center">
    <tr bgcolor="#CCCCCC">
    <th height="31" width="8%" align="center">
    S.No
    </th>
    <th class="texthead" height="31" width="44%" align="center">
    File Name
    </th>
    <th class="texthead" height="31" width="34%" align="center">
    Submit Date
    </th>
    <th class="texthead" height="31" width="14%" align="center">
    Pages
    </th>
    </tr>
    <xsl:for-each select="TeamReport/Files/download">
    <tr>
    <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>
    </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>


    Please help me out.
    Thanks.
    Boon
    Boon Subra
    Ranch Hand

    Joined: Jul 18, 2001
    Posts: 69
    Hi,

    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 =
    domparser.getDocument("c:/stocks/stocks.xml");
    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.

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

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

    thanks
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    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]
     
    wood burning stoves
     
    subject: JSP to PDF
     
    Similar Threads
    reports- Urgent, please help
    Generating PDF with data from DB
    Converting Html to PDF
    FileWriter and PDF
    Doc file to PDF generation