pdf - 为什么pdf下载的bij Trinidad fileDownloadActionListener不能用pdf阅读器打开?
问题描述
我确实创建PDF
了String
它所String
在的位置,并将其作为带有 的按钮xml format
呈现给用户。trinidad
fileDownloadActionListener
我可以下载 PDF 但无法使用 adobe reader 打开它PDF reader
我认为它可能已损坏但事实并非如此,因为我可以使用Visual Studio Code
任何其他文本编辑器(如notepad
.
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>
后端:
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();
}
pdf创作:
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();
}
推荐阅读
- javafx - Gdx.files.local NullPointerException
- spring - Spring数据elasticsearch + arangodb + spring boot
- java - 匹配以'开头的字符串的正则表达式
- angular - 如何更新angular2中按钮单击的数据?
- python - PyTorch:预测单个示例
- ms-access - 新手到 MS ACCESS - 超链接到 PDFS 文件夹?
- typescript - Angular4返回带有嵌套Observables的Observable
- macos - 如何在二进制可执行文件中找到隐藏或未记录的选项/标志?
- c# - 在 JSON 中获取相同值的多条记录
- c# - Attachment 类中 System.Net.Mime.MediaTypeNames 的意义是什么?