pdfjsfitexttrinidad

Why pdf downloaded bij Trinidad fileDownloadActionListener can't be open with pdf reader?


I do create PDF from String which this String is in xml format and present to user as a trinidad button with fileDownloadActionListener.

I can download the PDF but can't open it with PDF reader such as adobe reader first I thought it maybe corrupted but it is not the case because I can open it with Visual Studio Code or any other text editor such as notepad.

1- why I can't open it with adobe reader?

2- Is there any better why to do this?

Frontend:

<h:commandButton id="mehrpdf" value="Download PDF" styleClass="popupButton">
        <tr:fileDownloadActionListener filename="mehr.pdf"  contentType="application/pdf; charset=utf-8" method="#{bean.downloadMehr}" />
</h:commandButton>

Backend:

public void downloadMehr(FacesContext context, OutputStream out) throws IOException
    {
        String fetchedXmlMessage = getFetchedXmlMessage();
        XmlPrettifier prettifier = XmlPrettifierFactory.getInstance();
        prettyXmlMessage = prettifier.makePretty(fetchedXmlMessage);

        // alternativ way
        File pdfFile = createPdfFromTxt(prettyXmlMessage);

        OutputStreamWriter w = new OutputStreamWriter(out, "UTF-8");
        w.write(prettyXmlMessage);
        w.flush();
    }

pdfcreation:

public File createPdfFromTxt(String content) throws IOException, DocumentException {

        //define the size of the PDF file, version and output file
        File outfile = File.createTempFile("mehr", ".pdf");
        Document pdfDoc = new Document(PageSize.A4);
        PdfWriter.getInstance(pdfDoc, new FileOutputStream(outfile)).setPdfVersion(PdfWriter.PDF_VERSION_1_7);
        pdfDoc.open();

        //define the font and also the command that is used to generate new paragraph
        Font myfont = new Font();
        myfont.setStyle(Font.NORMAL);
        myfont.setSize(11);
        pdfDoc.add(new Paragraph("\n"));

        // add paragraphs into newly created PDF file
        File contentFile = File.createTempFile("content", ".txt");
        FileUtils.writeStringToFile(contentFile, content);

        BufferedReader br = new BufferedReader(new FileReader(contentFile));
        String strLine;
        while ((strLine = br.readLine()) != null) {
            Paragraph para = new Paragraph(strLine + "\n", myfont);
            para.setAlignment(Element.ALIGN_JUSTIFIED);
            pdfDoc.add(para);
        }
        pdfDoc.close();
        br.close();

        return outfile;
    }

Solution

  • I did change the backend as below according to what people mentioned in the command above, now I can read it with PDF reader. Thanks to all of you.

    public void downloadMehr(FacesContext context, OutputStream out) throws IOException
        {
            String fetchedXmlMessage = getFetchedXmlMessage();
            XmlPrettifier prettifier = XmlPrettifierFactory.getInstance();
            prettyXmlMessage = prettifier.makePretty(fetchedXmlMessage);
    
            // create pdf from string
            PdfCreator pdfCreator = new PdfCreator( prettyXmlMessage);
            File pdfFile = pdfCreator.create();
    
            FileInputStream inStream = new FileInputStream(pdfFile);
            byte[] buffer = new byte[4096];
            int bytesRead = -1;
    
            while ((bytesRead = inStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
    
            inStream.close();
            outputStream.close();
        }