I think I have found a strange behaviour in the Struts-Tiles framework that I 
am attempting to pin down and understand.  First, I'm using Struts 1.2.9 with 
the bundled Tiles framework, so its a bit older, but I'm hoping it will work 
without having to go through the trouble of upgrading to Struts2 and Tiles2 
because that would be a lot of work.

Here is my scenario, I'm developing a struts/tiles application, part of
which contains a reporting mechanism for users to query against. I have
the ability to print a report, but I also want the ability to PDF that
exact same report and save it for the user. To accomplish that task I
am using the iText API and a HttpURLConnection to retrieve the report
content.

I've been experimenting with multiple variations on how to accomplish
this so my below code is a little messy, but it does create and return
a PDF, but it is missing the content it should have. As far as I can
tell from the order of events the following occurs. I open my browser
to the makePDF.do action which sets up an HttpURLConnection to the
getReport action. The getReport action then forwards to the tiles name
which retrieves the layout jsp and is where it should insert the tiles
attributes and get the report content. This is where it breaks down
however and the only HTML that is returned is from the layout page.

My goal is to simply convert the resulting HTML code into a PDF
document, so if there is another approach I could take I am open to it.
This is the best method I could figure that would allow me to maintain
just one copy of the report rather than having to build both a
printable and PDF version of the report and ensuring they look the same.

I've also noticed something interesting, and
that is in the PDF there is text rendered for the tiles tag
<tiles:getAsString name="title" /> which tells me that the
HttpURLConnection is getting the tiles attributes, but where it is
failing is on the <tiles:insert attribute="body" /> tag. That at
least narrows the problem down to the tiles:insert line specifically
and not tiles overall. 

How can I make this work???

THANKS!
-Ryan Henson

PDFAction.java

/*
* @author: Ryan Henson Created on July 7, 2008
* 
* Purpose: This file takes an input source, converts it into a PDF, and returns 
it
*/
package org.airybsa.struts.control;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.lowagie.text.*;
import com.lowagie.text.html.HtmlParser;
import com.lowagie.text.pdf.PdfWriter;

public class PDFAction extends DispatchAction {
public ActionForward makePDF(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
// prepare a string of key value pairs.
            String paramStr = 
"func=getReport&print=true&reportID=1&selectedIDs=315,158";

// POST requests are required to have Content-Length
            String lengthString = String.valueOf(paramStr.length());

/*
             * Establish a connection to the application.
             */
            URL url = new 
URL("http://localhost:8080/AirySchedule/getReport.do";);
            URLConnection con = url.openConnection();

/*
             * configure the connection to allow for the operations necessary.
             * 1. Turn off all caching, so that each new request/response is
             * made from a fresh connection with the servlet. 2. Indicate that
             * this client will attempt to SEND and READ data to/from the
             * servlet.
             */
            con.setUseCaches(false);
            con.setDefaultUseCaches(false);
            con.setDoInput(true);
            con.setDoOutput(true);
// Set the content type we are POSTing and length of the data.
            con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
            con.setRequestProperty("Content-Length", lengthString);

            OutputStream os = con.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
// send data to servlet
            osw.write(paramStr);
            osw.flush();
            osw.close();

            Document document = new Document();
            document.setPageSize(PageSize.A4.rotate());
            OutputStream out = response.getOutputStream();

            response.addHeader("Content-Type", "application/pdf");
//response.addHeader("Content-Disposition", "attachment; 
filename=\"unnamed.pdf\"");
//response.addHeader("Content-Length", "" + cachedURL.length());

try {
                PdfWriter.getInstance(document, out);
                HtmlParser.parse(document, con.getInputStream());
} catch (Exception e) {
                log.error(e);
}

} catch (Throwable t) {
            log.error(t);
}
return null;
}
}


      

Reply via email to