RE: Redirecting to a PDF file

David R Robison <[email protected]>
Newsgroups gmane.comp.java.enhydra.barracuda.general
Message-ID <[email protected]>
Thanks for everyone's input, I now have it working. For compleatness sake, I've
included the final code incase anyone else is interested.

David Robison

	class RptOpenActionItemsHandler extends DefaultBaseEventListener {
		public void handleViewEvent(ViewEventContext context) throws EventException,
ServletException, IOException {
			HttpServletRequest req = context.getRequest();
                        HttpServletResponse resp = context.getResponse();
                        DBConnection conn = null;
                        String rptPath =
context.getConfig().getServletContext().getRealPath("reports");

                        try {
                          // See if we need to compile the report
                          java.io.File srcFile = new
java.io.File(rptPath+"/jrOpenActionItems.xml");
                          java.io.File dstFile = new
java.io.File(rptPath+"/jrOpenActionItems.jasper");
                          if (dstFile == null || (srcFile.lastModified() >
dstFile.lastModified())) {
                            // compile report
                           
JasperCompileManager.compileReportToFile(rptPath+"/jrOpenActionItems.xml",
rptPath+"/jrOpenActionItems.jasper");
                          }

                          // fill report
                          Map parameters = new HashMap();
			  parameters.put("ReportTitle", "Open Action Items");
			  try { parameters.put("MyOrg",
LoginServices.getCurrentOrganization(req).getHandle()); } catch (Exception e) {}
                          conn = Enhydra.getDatabaseManager().allocateConnection();
                          JasperPrint jp =
JasperFillManager.fillReport(rptPath+"/jrOpenActionItems.jasper", parameters,
conn.getConnection());

                          // export and return PDF file
                          byte buf[] = JasperExportManager.exportReportToPdf(jp);
                          resp.setContentType("application/pdf");
                          resp.setContentLength(buf.length);
                          resp.setHeader("content-disposition", "inline;
filename=OpenActionItems.pdf");
                          OutputStream out = resp.getOutputStream();
                          out.write(buf, 0, buf.length);
                          out.close();
                        } catch (JRException e) {
			  e.printStackTrace();
                          logger.error("RptOpenActionItemHandler: " + e);
                        } catch (DatabaseManagerException e) {
			  e.printStackTrace();
                          logger.error("RptOpenActionItemHandler: " + e);
                        } catch (java.sql.SQLException e) {
			  e.printStackTrace();
                          logger.error("RptOpenActionItemHandler: " + e);
                        } finally {
                          if (conn != null) try { conn.reset(); } catch
(Exception e) {}
                          if (conn != null) conn.release();
                        }
		}
	}


Quoting Mike Warne <[email protected]>:

> Hi,
> I have done something similar with Jasper reports, but as Chrstian
> mentioned the report needs to fit in memory..
> This example <snip> came from enhydra, but should be easy to port to a
> standard servlet..
> 
>                 JasperPrint jp = JasperFillManager.fillReport(filename,
> parameters,
>                         getDataSource(d));
>                 byte buf[] = JasperPrintManager.printReportToPdf(jp);
>                 comms.response.setContentType("application/pdf");
>                 comms.response.setContentLength(buf.length);
>                 comms.response.setHeader("content-disposition", "inline;
> filename=storageReport1.pdf");
>                 OutputStream out = comms.response.getOutputStream();
>                 out.write(buf, 0, buf.length);
>                 out.flush();
> 
> 
> Mike. 
> 
> -----Original Message-----
> From: Christian Cryder [mailto:[email protected]]
> Sent: Wednesday, February 19, 2003 3:22 AM
> To: [email protected]
> Subject: RE: [Barracuda] Redirecting to a PDF file
> 
> 
> Hi David,
> 
> Another thing that I've done which works great is to redirect the
> output
> directly to the client browser, rather than saving it on disk. For
> instance,
> 
>     class RenderCSV extends DefaultBaseEventListener {
>         public void handleViewEvent(ViewEventContext vec)
>         throws EventException, ServletException, IOException {
> 
>             //get the response object
>             HttpServletResponse resp = vec.getResponse();
> 
>             //figure out our file name
>             String filename = "nxg_batch_"+batch+".csv";
> 
>             //build the output in a StringBuffer
>             StringBuffer sb = new StringBuffer(5000);
> 
> sb.append("\"hash\",\"bt_merch_rule_set_id\",\"transit_route\",\"account\",\
> "check_num\",\"amount\",\"name\",\"address1\",\"city\",\"state\",\"country\"
> ,\"zip\",\"phone\",\"test\",\"company\",\"address2\",\"fax\",\"email\",\"shi
> pping_name\",\"shipping_company\",\"shipping_address1\",\"shipping_address2\
> ",\"shipping_city\",\"shipping_state\",\"shipping_country\",\"shipping_zip\"
> ,\"description\",\"sub_total\",\"shipping_type\",\"shipping_cost\",\"tax\"")
> ;
>             sb.append(eol);
>             sb.append(...);
>             sb.append(...);
>             sb.append(...);
> 
>             //now write the response
>             OutputStream out = resp.getOutputStream();
>             resp.setContentType("text/comma-separated-values");
>             resp.setContentLength(sb.length());
>             resp.setHeader("Content-Disposition", "inline;
> filename=\""+filename+"\"");
>             out.write(sb.toString().getBytes());
>             out.close();
>         }
>     }
> 
> Now, I'm not sure if Jasper reports lets you get the file in memory,
> rather
> than going to disk, but the advantage of this approach is that it
> doesn't
> leave anything sitting around in public space on disk (ie. if you create
> a
> report that is for an authorized user, but you save it in a publically
> accessible place, someone else could conceivably find and view it...not
> very
> secure). This way, you just stream the file directly back to the
> browser
> where its either displayed or they get prompted with the "Save as..."
> dialogue.
> 
> HIH,
> Christian
> ----------------------------------------------
> Christian Cryder [[email protected]]
> Internet Architect, ATMReports.com
> Barracuda - http://barracudamvc.org
> ----------------------------------------------
> "Coffee? I could quit anytime, just not today"
> 
> > -----Original Message-----
> > From: [email protected]
> > [mailto:[email protected]]On Behalf Of David R
> Robison
> > Sent: Tuesday, February 18, 2003 10:42 AM
> > To: [email protected]
> > Subject: [Barracuda] Redirecting to a PDF file
> >
> >
> > I am trying to integrate Jasper Reports into my barracuda
> > application. I have it
> > so that when the user issues the GetReport.event, the file Report.pdf
> is
> > generated. I then want to redirect to that file so that it will
> > be displayed in
> > the web browser. I do a
> > ClientSideRedirect("WEB-INF/report/Report.pdf");. In the
> > web browser I get a 404 error with no detail. Is there something
> > else I need to
> > do to get content back from a barracuda application without
> > having to envoke
> > another event?
> >
> > David Robison
> > _______________________________________________
> > Barracuda mailing list
> > [email protected]
> > http://barracudamvc.org/lists/listinfo/barracuda
> 
> _______________________________________________
> Barracuda mailing list
> [email protected]
> http://barracudamvc.org/lists/listinfo/barracuda
> _______________________________________________
> Barracuda mailing list
> [email protected]
> http://barracudamvc.org/lists/listinfo/barracuda
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.