The specific of the Struts UI framework is that in some cases it takes control and opens output stream before PD4ML-enabled JSP page is loaded. On the other hand PD4ML needs an exclusive control over the output stream: it outputs binary data and any other output writers can corrupt it.
In order to solve the problem PD4ML offers the following solution.
(Since PD4ML 1.2.8 / Pro 2.1.4 there is an alternative to the approach below. The just generated PDF can be temporally stored to the hard drive and the current HTTP request can be forwarded to the new static PDF. See <pd4ml:savefile/> description for more details)
PD4ML transformer JSP pages need to be deployed to a separate web application outside of Struts context. It can be the same web application, but the PD4ML-enabled JSP pages should be out of control of the Struts dispatching servlet. (Can be achieved by web.xml settings).
If you want to separate PD4ML transforming and your application business logic – it is possible as well. Run a servlet runtime (like Tomcat) on a separate hardware unit and deploy PD4ML to it.
For our example the separated web application is associated with name separate_web_app.
Create a PD4ML transformer JSP page transformer.jsp in separate_web_app like the following.
<%@ taglib uri="/WEB-INF/tlds/pd4ml.tld" prefix="pd4ml" %><%@page contentType="text/html; charset=ISO8859_1"%><pd4ml:transform screenWidth="400" pageFormat="A5" pageOrientation="landscape" pageInsets="15,15,15,15,points" enableImageSplit="false" inline="true" fileName="myreport.pdf" interpolateImages="false" url="http://mainserver/struts/report.jsp"> <pd4ml:footer titleTemplate="$[title]" pageNumberTemplate="page $[page] of $[total]" titleAlignment="left" pageNumberAlignment="right" fontSize="12" areaHeight="14"/> </pd4ml:transform>
Replace url attribute value of <pd4ml:transform> with the actual URL of your Struts (JSP) page, that should be converted to PDF.
The HTML content of transformer.jsp is ignored completely, but as usually: it should be no any white space character before <pd4ml:transform> tag.
At this point you can access the transformer.jsp and force PDF conversion of the specified source. But in most of the cases a session ID (JSESSIONID) propagation is important for proper Struts (or other JSP-based frameworks) functionality.
If the url attribute of <pd4ml:transform> is set, than PD4ML takes its value and appends to it JSESSIONID parameter. The value for the parameter it takes from HTTP request variable pd4session.
So a request for PDF generation from Struts context can look like that:
<% response.sendRedirect( "/separate_web_app/transformer.jsp?pd4session=" + session.getId() ); %>
New information: using of <pd4ml:savefile> tag allows us to save the just-generated PDF to the server’s local drive and to redirect the initial PDF generation HTTP request to the new static PDF file.