0

I'm trying to display a pdf in the browser but it doesn't work, It shows strange characters:

%PDF-1.4 %���� 3 0 obj <>stream x���n�������‑
C��͇[1]
�,ю��Țv=>�%���"=���ٯ�­
{�_�-�9�!�­Hu�a�Y�(`�E�c��]]������Չ��t�jsb�w�.>PV~P��
F\�z�oV?��'����S��Mb�$��`�߃������aLV�h䕴�����­�s��+���_�����������K�
�A�D����;[1]�[�I�'y���K�
��l[1]�
����,�I,2)

This is the method code in the controller:

FacesContext ctx = FacesContext.getCurrentInstance();
try {      
      ExternalContext external = ctx.getExternalContext();
      HttpServletResponse response = (HttpServletResponse) external.getResponse();
      OutputStream responseStream = response.getOutputStream();

      InputStream fileInputStream = evaluacionDataManager.getDatosEvaluacionEmpleado().getDocumento();
      byte[] pdf = IOUtils.toByteArray(fileInputStream);

      response.setContentType("application/pdf");
      response.setContentLength(pdf.length);  
      response.setHeader("Content-Disposition", "attachment; filename=\""
                 + "filename.pdf" + "\"");

      FileOutputStream fos = new FileOutputStream("e:/aaaaaMG.pdf");
      fos.write(pdf);
      fos.close();

      responseStream.write(pdf);
      response.flushBuffer();     

} catch (Exception e) {
      logger.error("MENSAJE_CERTIFICACION", e);
      addErrorMessage(e.getMessage(), "");
}
FacesContext.getCurrentInstance().responseComplete();     

The pdf aaaaaMG.pdf that is saved in the e: directory of the PC it's perfect, but the pdf displayed in the browser doesn't work.

I've tried with different content types but it appears to be that the headers aren't getting set.

I have also tried with:

response.setHeader("Content-Disposition", "inline; filename=\"" + "filename.pdf" + "\"");

instead of attachment but it didn't solve my issue.

Any clue is welcome. Thanks

Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
mgranjao
  • 104
  • 1
  • 1
  • 8

0 Answers0