Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_cn
Articles_de
Articles_pt
Articles_zh




JSPWiki v2.2.33

[RSS]


Hide Menu

POJOToPDF


Difference between version 21 and version 2:

At line 8 added 3 lines.
Please note, the resulting PDF document does not look very pretty. This is because the XSL stylesheet is very basic and does not really apply much FO formatting tags/attributes. It would be very easy to modify the XSL to include images, bold, grey background cells etc.. but this is best left for the FOP documentation to describe.
At line 12 changed 2 lines.
* [4] Modify User Junit test
* [5] Run User Junit test
* [4] Modify UserManagerTest Junit test
* [5] Run UserManagerTest Junit test
At line 25 added 2 lines.
UserInputSource needs to extend interface FOPSource which should be in src/service/**/fop/FOPSource.java (source attached at the end of this tutorial).
At line 34 changed 1 line.
public class UserInputSource extends InputSource {
public class UserInputSource extends InputSource implements FOPSource {
At line 69 added 2 lines.
At line 79 added 2 lines.
At line 78 changed 1 line.
This class depends on two helper classes, AbstractObjectReader and EasyGenerationContentHandlerProxy that need to be added to src/service/**/util. These files are downloadable at the end of this tutorial.
This class depends on two helper classes, AbstractObjectReader and EasyGenerationContentHandlerProxy that need to be added to src/service/**/fop. These files are downloadable at the end of this tutorial.
At line 98 changed 1 line.
import org.appfuse.util.AbstractObjectReader;
import org.appfuse.fop.AbstractObjectReader;
At line 146 added 3 lines.
At line 166 changed 1 line.
processUserDetails(user);
processUserDetails(user);
At line 197 added 1 line.
At line 204 changed 1 line.
Place the user2fo.xsl stylesheet in src/web/**/fop/user2fo.xsl.
Place the user2fo.xsl stylesheet in src/service/**/fop/xsl/user2fo.xsl.
At line 330 added 1 line.
%%
At line 318 removed 1 line.
{{{body#pageName element.class { background-color: blue } }}}%%
At line 333 added 1 line.
!!Modify UserManagerTest.java Junit test [#4]
At line 321 changed 1 line.
!!Modify User Junit test [#4]
To support the execution of the User service level test, we need another helper file. This file (attached at the end) is called FOPHelper.java and lives in src/service/**/fop.
At line 337 added 68 lines.
Add the following imports and method to UserManagerTest.java:
[{Java2HtmlPlugin
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
//JAXP
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
//Avalon
import org.apache.avalon.framework.ExceptionUtil;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
//FOP
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.messaging.MessageHandler;
//Appfuse
import org.appfuse.fop.UserInputSource;
import org.appfuse.fop.FOPHelper;
public void testGeneratePDF() throws Exception {
log.info("Inside testGeneratePSF");
User testData = new User();
testData.setUsername("tomcat");
testData.getRoles().add(new Role("user"));
// set expected behavior on dao
userDAO.expects(once()).method("getUser")
.with(eq("tomcat")).will(returnValue(testData));
user = userManager.getUser("tomcat");
UserInputSource uis = new UserInputSource(user);
// Setup directories
File baseDir = new File(".");
File outDir = new File(".");
outDir.mkdirs();
//Setup input and output
File xslFile = new File(baseDir, "./src/service/org/appfuse/fop/xsl/user2fo.xsl");
File pdfFile = new File(outDir, "user.pdf");
File xmlFile = new File(outDir, "user.xml");
File foFile = new File(outDir, "user.fo");
log.debug("Input: a UserInputSource object");
log.debug("Stylesheet: " + xslFile);
log.debug("Output: PDF (" + pdfFile + ")");
log.debug("Output: FO (" + foFile + ")");
log.debug("Transforming...");
FOPHelper fopHelper = new FOPHelper();
fopHelper.convertPOJO2PDF(uis, xslFile, pdfFile, foFile);
fopHelper.convertPOJO2XML(uis, xmlFile);
}
}]
At line 407 added 13 lines.
Now execute ant test-service. The testGeneratePDF method will execute and generate the 3 files involved in the PDF generation
process. user.xml, user.fo and user.pdf (all three attached at the end of this tutorial).
You should see output:
{{{
[junit] [appfuse] INFO [main] UserManagerTest.testGeneratePDF(83) | xslFile exists
[junit] [appfuse] DEBUG [main] UserManagerTest.testGeneratePDF(88) | Input: a UserInputSource object
[junit] [appfuse] DEBUG [main] UserManagerTest.testGeneratePDF(89) | Stylesheet: ././src/service/org/appfuse/fop/xsl/user2fo.xsl
[junit] [appfuse] DEBUG [main] UserManagerTest.testGeneratePDF(90) | Output: PDF (./user.pdf)
[junit] [appfuse] DEBUG [main] UserManagerTest.testGeneratePDF(91) | Transforming...
}}}
At line 334 changed 1 line.
At line 338 changed 1 line.
import org.appfuse.fop.UserXMLReader;
import org.appfuse.fop.FOPHelper;
At line 345 changed 5 lines.
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.util.Date;
At line 355 changed 1 line.
At line 359 changed 1 line.
import org.springframework.core.io.*;
import org.xml.sax.InputSource;
At line 463 added 1 line.
import javax.xml.transform.sax.SAXSource;
At line 366 changed 1 line.
At line 371 changed 1 line.
* <p><a href="FopServlet.java.html"><i>View Source</i></a></p>
* <p><a href="FOPServlet.java.html"><i>View Source</i></a></p>
At line 377 changed 1 line.
* display-name="Fop Servlet"
* display-name="FOP Servlet"
At line 384 changed 4 lines.
public class FopServlet extends HttpServlet {
private Logger logger = null;
public class FOPServlet extends HttpServlet {
private Logger logger = null;
At line 390 changed 2 lines.
At line 393 changed 3 lines.
this.servletContext = getServletContext();
this.ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
this.servletContext = getServletContext();
this.ctx =
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
At line 397 changed 1 line.
At line 411 changed 1 line.
At line 425 changed 1 line.
At line 438 changed 4 lines.
ByteArrayOutputStream out = null;
try {
ByteArrayOutputStream out = null;
try {
At line 443 changed 1 line.
At line 446 changed 31 lines.
Driver driver = new Driver();
Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
driver.setLogger(logger);
MessageHandler.setScreenLogger(logger);
driver.setRenderer(Driver.RENDER_PDF);
// Start with a bigger buffer to avoid too many buffer reallocations
out = new ByteArrayOutputStream(16384);
driver.setOutputStream(out);
ServletContextResource resource =
new ServletContextResource(servletContext,
"/WEB-INF/classes/org/appfuse/webapp/fop/user2fo.xsl");
File xslFile = resource.getFile();
//Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslFile));
//Setup input for XSLT transformation
Source src = new UserInputSource(user).getSource();
//Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(driver.getContentHandler());
//Start XSLT transformation and FOP processing
transformer.transform(src, res);
byte[] content = out.toByteArray();
UserInputSource uis = new UserInputSource(user);
ClassPathResource resource =
new ClassPathResource("/org/appfuse/fop/xsl/user2fo.xsl");
byte[] content = FOPHelper.createPDF(uis, resource);
response.setHeader("Content-Disposition",
"attachment; filename=user-" + user.getUsername() + ".pdf");
At line 481 changed 1 line.
At line 485 changed 3 lines.
if (out!=null) {
out.close();
}
if (out!=null) {
out.close();
}
At line 491 removed 1 line.
At line 531 removed 1 line.
#
At line 624 added 1 line.
At line 647 added 2 lines.
At line 576 changed 1 line.
Also inside the package-web target add this line:
Also inside the package-web target add this block:
At line 579 changed 1 line.
<include name="**/*.xsl"/>
<fileset dir="src/service">
<include name="**/*.xsl"/>
</fileset>
At line 588 removed 1 line.
<include name="**/*.xsl"/>
At line 670 added 3 lines.
<fileset dir="src/service">
<include name="**/*.xsl"/>
</fileset>
At line 592 removed 1 line.
which ensures the xsl file is deployed.
At line 676 added 2 lines.
which ensures the xsl file is deployed into our runtime classpath.
At line 598 removed 2 lines.
*src/web/org/appfuse/webapp/fop/user2fo.xsl
*src/web/org/appfuse/webapp/action/FopServlet.java
At line 683 added 3 lines.
*src/web/org/appfuse/webapp/action/FOPServlet.java
*src/service/org/appfuse/fop/xsl/user2fo.xsl
At line 688 added 4 lines.
*src/service/org/appfuse/fop/FOPSource.java
*src/service/org/appfuse/fop/FOPHelper.java
*src/service/org/appfuse/fop/AbstractObjectReader.java
*src/service/org/appfuse/fop/EasyGenerationContentHandlerProxy.java
At line 604 removed 3 lines.
*src/service/org/appfuse/util/AbstractObjectReader.java
*src/service/org/appfuse/util/EasyGenerationContentHandlerProxy.java
At line 612 changed 2 lines.
*Ensure the xsl file is within your webapps/appfuse/WEB-INF/classes/** runtime directory somewhere
*Ensure the path in the FopServlet is correct for this XSL file (if you moved it)
*Ensure the xsl file is within your webapps/appfuse/WEB-INF/classes/** runtime directory
*Ensure the path to load the user2fo.xsl file, specified in FOPServlet, is correct (in case you moved it)

Back to POJOToPDF, or to the Page History.