• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP to PDF

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

  •  
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 69
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 69
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 69
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi can anybody help me out.

    thanks
     
    author
    Posts: 11962
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
    reply
      Bookmark Topic Watch Topic
    • New Topic