0

I'm trying to solve a problem with generating a pdf from an inputStream or byte array. The file being opened in Adobe Reader is corrupted. When I set Chrome as my default pdf reader it opens normally, and if I save "Save as" from the browser and then open it in Adobe then the document is correct. I've already tried everything. Originally the method was written in omnifaces. I tried to write it in several ways (i.e. pdfbox), none of them work and the effect is the same.

The original method:

    public String openDocument(StringValueTO docHandle) throws AbstracAekoException {
        try {
            IMaVorgangsdatenService service = ServiceLocator.get(IMaVorgangsdatenService.class);
            DocumentTO documentTO = service.openDocument(ElementTOUtil.nullSafeString(docHandle));

            if (documentTO == null || documentTO.getBinaryData() == null || documentTO.getBinaryData().length == 0) {
                FacesUtil.addMessage(FacesMessage.SEVERITY_ERROR, Messages.KEY_PPS_DOCUMENT_NOT_AVAILABLE);
            } else {
                byte[] data = documentTO.getBinaryData();
                InputStream inputStream = new ByteArrayInputStream(data);
                Faces.sendFile(inputStream, documentTO.getFilename().getValue().toString(), true);

                getData().setForceOpenDocumentZaem(false);
            }
         } catch (IOException e) {
             e.printStackTrace();
         }

        return null;

I changed to:

        try {
            DocumentTO documentTO = null;

            IMaVorgangsdatenService service = ServiceLocator.get(IMaVorgangsdatenService.class);
            documentTO = service.openDocument(ElementTOUtil.nullSafeString(docHandle));


        FacesContext facesContext = FacesContext.getCurrentInstance();

        HttpServletResponse response =
                (HttpServletResponse) facesContext.getExternalContext().getResponse();

        response.reset();
        response.setHeader("Content-Type", "application/pdf");
        response.setHeader("Content-Disposition", "attachment;filename=" + documentTO.getFilename().getValue().toString());

        OutputStream responseOutputStream = response.getOutputStream();

        byte[] data = documentTO.getBinaryData();
        InputStream inputStream = new ByteArrayInputStream(data);

        byte[] bytesBuffer = new byte[2048];
        int bytesRead;
        while ((bytesRead = inputStream.read(bytesBuffer)) > 0)
        {
            responseOutputStream.write(bytesBuffer, 0, bytesRead);
        }

        responseOutputStream.flush();


            inputStream.close();
            responseOutputStream.close();
            facesContext.responseComplete();


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

I tried different combinations. I completelly don't know why document is corrupted in Adobe Reader.

        <!-- PDF rendering -->
        <dependency org="org.apache.pdfbox" name="pdfbox" rev="2.0.11" conf="compile->default"/>
        <dependency org="org.apache.pdfbox" name="fontbox" rev="2.0.11" conf="compile->default"/>
        <!-- PrimeFaces -->
        <dependency org="org.primefaces" name="primefaces" rev="8.0" conf="compile->default" transitive="false" />
        <dependency org="org.omnifaces" name="omnifaces" rev="1.6.1" conf="compile->default" />

What I saw, that generated PDF file doesn't have trailer element at the end. Any help from your site will be highly appreciated.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97

0 Answers0