为什么pdf下载的bij Trinidad fileDownloadActionListener不能用pdf阅读器打开?

为什么pdf下载的bij Trinidad fileDownloadActionListener不能用pdf阅读器打开?,pdf,jsf,itext,trinidad,Pdf,Jsf,Itext,Trinidad,我确实使用xml格式的字符串创建PDF,并使用fileDownloadActionListener将其作为特立尼达按钮呈现给用户 我可以下载PDF,但不能先用adobe reader之类的PDF阅读器打开它 我认为它可能已损坏,但事实并非如此,因为我可以使用Visual Studio代码或任何其他文本编辑器(如记事本)打开它 1-为什么我不能用adobe reader打开它 2-为什么要这样做,还有更好的理由吗 前端: <h:commandButton id="mehrpdf" value

我确实使用xml格式的字符串创建PDF,并使用fileDownloadActionListener将其作为特立尼达按钮呈现给用户

我可以下载PDF,但不能先用adobe reader之类的PDF阅读器打开它 我认为它可能已损坏,但事实并非如此,因为我可以使用Visual Studio代码或任何其他文本编辑器(如记事本)打开它

1-为什么我不能用adobe reader打开它

2-为什么要这样做,还有更好的理由吗

前端:

<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>
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;
    }

我确实根据上面命令中提到的内容更改了后端,现在我可以用PDF阅读器阅读了。谢谢大家

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();
    }

我可以用Visual Studio代码打开它,这是什么意思。?您是否在VS代码中使用PDF预览插件?我的建议是使用文本编辑器打开PDF,查看在PDF标题%PDF-…?之前是否有一些文本,可能是一些日志或错误消息。因为您使用的是编写器而不是输出流。Writer用于字符数据。PDF是二进制的。为什么要将PDF视为XML是另一个谜。好吧,那就是你没有创建PDF!请发布你的createPdfFromTxt方法,这一点都没有意义。据称,您正在接收XML,将其转换为PDF,扔掉生成的文件,用编写器保存XML,然后尝试使用i-a PDF阅读器、ii-VIsual Studio和iii-a文本编辑器打开它。您需要从文件复制到输出,只使用输入和输出流,然后可能会删除文件。这不是一个替代方案。这是这里唯一能生成PDF的东西,你要么把它注释掉,要么把它扔掉。
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();
    }