Skip to Content
0
Former Member
Dec 04, 2008 at 11:13 AM

Stream a PDF to response

100 Views

Hello...

I'm trying to stream a generated pdf file (jasperreports) to the response in an AbstractPortalComponent. The Code looks like follows...

public class HelloWorld extends AbstractPortalComponent
{
    
    JasperPrint jprint;
    JasperReport report;
    Map parameters;
    
    public void doContent(IPortalComponentRequest request, IPortalComponentResponse response)
    {    			    	    	
  
try 
  {
  //input parameters
  parameters = new HashMap();    													
  //path to report
  IResource res = request.getResource(IResource.SCRIPT, "reports/SAPPortalPDF.jasper");
  String path = res.getResourceInformation().getSource();
			
  //jasper print object
  jprint = JasperFillManager.fillReport(path, parameters, new JREmptyDataSource());
			
 //output data in byte[]-object							
 byte[] bytes = JasperExportManager.exportReportToPdf(jprint);
 Writer w = response.getWriter();
									
 //Content-Type setzen
PortalComponentContentType content = PortalComponentContentType.parse("application/pdf");			
response.setContentType(content);												
response.write(response.getContentType().toString());					
		
} 
catch(Exception e)
		{			
			response.write(e.toString());
			response.write(e.getLocalizedMessage());
			response.write(e.getCause().toString());
		}
	
 }

It seems to work fine, but the data is not shown as a pdf file. I get all the characters displayed, which are in the byte array. So there are two possibilities... either the way I stream the data to the response is not correct or there are problemes concerning the mime-type. If I put out the mime-type of the response, it says "application/pdf". So this seems to be correct...

any ideas ????

Greets, Jochen