Question

I want to display a pdf file without save the file on the server i have saved the generete pdf in array byte when i trie to display in the page it come like this

xœÕZIk,7¾ûWÌ9G[·$f=ä0ärË9ò.ùû©E%•Zín¿áñfì^¤Z¿úªdófO½üy2'?Myzs§ì[:}ùåå‡oNð=ø÷å·—ëûË4í<üþóéÛ§=Ywzÿõdz±Ë|6Îx–Ww6ÓóbÏ&š´¼¦³Éæb®xá¶Èý;~8—×x6ò <ÈK8“9\2›']¡õh ¸fµ|%,?½÷òxù~SÚÙ¼ù•´ÖY\5Án´B¶Á¢DvÆ}l¤ÏdÜÈx™î]–WzôJwoËT.Ùû’^ÀËÁ>èû²/S˜ß¦µLOgœµÎ9؃÷zT#f°/Z€”ObÝÝ-¼{‹k']šñ‡¥\°f™ãÙMä‚Ì·yG7;ôî†fÒ¾ôLvÉzxÄî aÓ[^ëÁr.;Pq*{=xÅ+]_ðÕ* Œ;ã#w1‡Š2z;²i´ÄÄ¡ä‹ø%À2 ŒO[fÆD µ2èêxIV¹­EKò-XbÁ÷M¦¯L–Ñ{kÎÙŽ!=ƒY)xMV¤þÆ{xo(­îb r×Û– M¸ÂVßS^RðÕÕ+%H9L­Eãƒqqï´÷®lÚ»v]qoIr¯–à[I¿;‰˜öìý¾Åb²Áua!ïÕ¾áJN¶ ¨ôAt|p:äl¤³l6ë½}’‚˜HÉO(ç¬rìÞTöb<Ú^!b\ƒ¦˜Z^u¼åÿ3ÏWOeñßMRX²tß³q)•

this is my code

 FacesContext context = FacesContext.getCurrentInstance();  
  HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();          
  response.reset();
  ServletOutputStream os = response.getOutputStream();
  response.setContentType("application/pdf"); // fill in  
  response.setContentLength(this.currentDocument.getContenu().length);  
  String pdfName="Test";
  response.setHeader("Content-Disposition", "attachment; filename=\"" + pdfName + "\""); 

  os.write(this.currentDocument.getContenu()); // fill in bytes  

  os.flush();  
  os.close();  
  context.responseComplete();  
Was it helpful?

Solution

It's happening most likely because you're using an ajax action to fire the PDF generation. A full-on HTTP request is required to properly render the PDF in the response

Make sure you're not using an ajax button like <p:commandButton/> to fire the action.

Use either

<p:commandButton ajax="false"/>

OR

<h:commandButton/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top