8 Replies Latest reply on Oct 27, 2008 1:21 PM by sessa

    How to stream a pdf at portlet?

    littleprince

      i want to stream a pdf report to a page, but some how i got funny character when streaming. below is the code i wrote. i got this works at normal java application, but not in portlet.

      FacesContext ctx=FacesContext.getCurrentInstance();
      String fullPath = getReportPath(reportName);
      HttpServletResponse response=(HttpServletResponse)ctx.getExternalContext().getResponse();
      response.setContentType("application/pdf");
      response.addHeader("Content-Disposition", "attachment; filename="+reportName+".pdf");
      JasperPrint jasperPrint=JasperFillManager.fillReport(fullPath, parameters, new JRBeanCollectionDataSource(list));
      JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

      ctx.getApplication().getStateManager().saveSerializedView(ctx);
      ctx.responseComplete();

      the result i got is
      %PDF-1.4 %âãÃÂ�Ó 4 0 obj <>stream xœ¥˜ÉnÛ0†ïz ÓCenZœ[‚.hÂÂ�öÂÂ�¸§¢Ã’]qØúú¥š†ÑŒ$$±Âd>ÃÂ�ð*óǒ}MîW‰`Ü|4¯¥–luHŸæ»mrónõ'ùè‡p¶9øÂÂ�2”iރ’ ހ¼ýõi—ÜܽTû {¬×§ÃÂ�oöP?O—榜ÃÂ"7þùË\«ð§<-™ýzø –&ƒ`ÿ,§+Ê%“Š½;ÕÉ=(U±¢ÈJý~Lß.ÂÂ�§™KÔ1¦ÅÓ"k’¦2c‡$ÓÃÂ�µ_>%ÂÂ�4ª_¢¹ TvÃÂ�…Q ¤5Sò¬½7ËtTò¬@$ÿ°¾Ôc57[ðÕ|I=Àú%A>_t› Ã`Q1n¦îÂ(Ù _D…æùV~µ?ŒV¾ÙÂÂ�>/HÒ¿âú5EE ~Ÿå@]1n®úy9¨~ÃÂ�õœëûRÂÂ�n€Ùˆ/¤”ÂÂg×!ýØ|ëŠq3 eÞ6`™G e‰4ànsÙÿŽÕ¿ÙøË-%IÿÂÂ�³kŠŽ¾þ6:*@]1nî¤53¶Ñ_ˆ7fm›m¨ßŽÕËÓè¿@ÃÂ�F|!µÊi7àìš"¤ß

      However, if i use
      JasperExportManager.exportReportToPdfFile(jasperPrint,"c:\\test.pdf");
      i got the pdf nicely, but it save into my local, which does not serve my need.

      So what is the correct way to stream a pdf? i want to do it like a popup message to let me download the pdf.

      Thanks in advance.