iText生成的PDF在Chrome中未正确显示 [英] iText generated PDF not shown correctly in Chrome
问题描述
我使用Java中的iText库来生成PDF文件。这个想法是,用户填写一些信息,当用户点击生成按钮时,pdf将显示在浏览器的新选项卡中。现在我偶然发现了一些这样做的问题,它们是:
- URL不会改变,所以而不是/application/user.pdf我得到/application/dashboard.xhtml
- 我可以保存所有浏览器中的pdf文件,除了Chrome。
请注意,我不想将它保存在光盘上,只需在浏览器中显示PDF,以便用户可以选择如果他想保存它的话。
下面是我用来生成我的pdf的代码:
public static void createPdf(User user,byte languageNumber,HttpServletResponse response){
Document document = new Document();
try {
/ * PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(c://+ user.getUsername()+_+ languageCode +.pdf )); * /
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document,baos);
document.addTitle(您的简历);
document.addSubject(这是你的简历);
document.addKeywords(CV);
document.addAuthor(user.getUsername());
document.open();
document.add(
new Paragraph(user.getPersonalInformation()。getFirstname()++ user.getPersonalInformation()。getLastname()));
document.close();
//设置一些响应头文件
response.setHeader(Expires,0);
response.setHeader(Cache-Control,
must-revalidate,post-check = 0,pre-check = 0);
response.setHeader(Pragma,public);
//设置内容类型
response.setContentType(application / pdf);
response.setContentLength(baos.size());
// ServletOutputStream out = response.getOutputStream();
OutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
} catch(DocumentException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace(); //改变catch语句的主体使用File |设置|文件模板。
$ b * 此方法位于按钮后面我的JSF页面
public String exportPdf(){
user = userService.retrieveLoginUser();
FacesContext context = FacesContext.getCurrentInstance();
尝试{
Object response = context.getExternalContext()。getResponse();
if(响应instanceof HttpServletResponse){
HttpServletResponse hsr =(HttpServletResponse)响应;
PdfCreator.createPdf(user,selectLanguage,hsr);
//告诉JSF跳过生命周期的其余阶段
context.responseComplete();
}
返回../+ user.getUsername()+.pdf;
} catch(Exception e){
e.printStackTrace();
}
返回null;
}
使用的技术:
- JSF 2.0
- Facelets
- iText
预先感谢:D
>铬是否打开PDF,然后不正确渲染?在这种情况下,请在 http://new.crbug.com 上打开一个问题,并附上一个示例PDF文件显示了问题。在这里回复问题编号。
I am using the iText library in Java to generate a pdf file. The idea is that a user fills in some information and that when the user clicks on the generate button the pdf is shown in a new tab in the browser. Now I have stumbled upon some problems doing this, which are :
- the URL does not change, so instead of /application/user.pdf I get /application/dashboard.xhtml
- I can save the pdf file in all browsers except for Chrome.
Please note that I don't want to save it on disc but simply show the pdf in the browser so the user can choose if he wants to save it.
Here is the code that I use to generate my pdf :
public static void createPdf(User user, byte languageNumber, HttpServletResponse response) {
Document document = new Document();
try {
/* PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("c://" + user.getUsername() + "_" + languageCode + ".pdf"));*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.addTitle("Your CV");
document.addSubject("This is your CV");
document.addKeywords("CV");
document.addAuthor(user.getUsername());
document.open();
document.add(
new Paragraph(user.getPersonalInformation().getFirstname() + " " + user.getPersonalInformation().getLastname()));
document.close();
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
response.setContentLength(baos.size());
//ServletOutputStream out = response.getOutputStream();
OutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
*This method is behind a button on my JSF page *
public String exportPdf() {
user = userService.retrieveLoginUser();
FacesContext context = FacesContext.getCurrentInstance();
try {
Object response = context.getExternalContext().getResponse();
if (response instanceof HttpServletResponse) {
HttpServletResponse hsr = (HttpServletResponse) response;
PdfCreator.createPdf(user, selectLanguage, hsr);
//Tell JSF to skip the remaining phases of the lifecycle
context.responseComplete();
}
return "../" + user.getUsername() + ".pdf";
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Used technologies :
- JSF 2.0
- Facelets
- iText
Thanks in advance :D
解决方案 Does chrome open the PDF and then not render it correctly? In that case, please open an issue at http://new.crbug.com and attach an example PDF file that shows the problem. Reply with the issue number here.
这篇关于iText生成的PDF在Chrome中未正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!