简体   繁体   English

itext生成的pdf无法在Adobe Reader中打开?

[英]Itext generated pdf not opening in Adobe reader?

I am using itext(version 5.5.4) to create a pdf file in a server. 我正在使用itext(5.5.4版)在服务器中创建pdf文件。 When I download the file on client and try to open it in adobe reader, it does not open and a message pops up saying that "There was an error processing a page. There was a problem reading this document(129)". 当我在客户端上下载文件并尝试在Adobe Reader中打开文件时,该文件无法打开,并弹出一条消息,提示“处理页面时出错。阅读此文档时出现问题(129)”。

在此处输入图片说明

This pdf file opens in other application(like evince, foxit and google chrome) just fine. 此pdf文件可以在其他应用程序(如evince,foxit和google chrome)中打开。 Below is the part of the code that I am using. 以下是我正在使用的部分代码。

 public static String genPdfAsBase64(String orientation, JSONObject data)
    throws IOException, DocumentException {
    if(orientation.equals("landscape")) {
        doc = new Document(PageSize.A4.rotate(), 10f, 10f, 50f, 5f); 
    } else {
        doc = new Document();
    }
    JSONArray header = (JSONArray)data.get("header");
    JSONArray body = (JSONArray)data.get("body");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(doc, baos);
    TableHeader evt = new TableHeader();
    evt.setOrientation(orientation);
    writer.setPageEvent(evt);
    doc.addAuthor(AUTHOR);
    doc.open();
    Image img = Image.getInstance(Base64.decode(BASE_64_IMG));
    img.setAlignment(Image.ALIGN_MIDDLE);
    img.setBorder(Rectangle.NO_BORDER);
    img.scaleToFit(20f,20f);
    doc.add(img);
    Paragraph par = new Paragraph("Report", new Font(FontFamily.HELVETICA, 10));
    par.setAlignment(Element.ALIGN_CENTER);
    doc.add(par);
    doc.add(new Paragraph(" "));
    PdfPTable table = new PdfPTable(header.size());
    table.setTotalWidth(1500);
    table.setHeaderRows(1);
    /*Header*/
    for(Object obj : header) {
        String text = (String)obj;
        PdfPCell cell = new PdfPCell(new Phrase(text));
        cell.setBackgroundColor(headerCol);
        table.addCell(cell);
    }
    /*Body*/
    for(int i=0; i<body.size(); i++) {
        JSONArray row = (JSONArray)body.get(i);
        for(Object obj : row) {
            String text = String.valueOf(obj);
            PdfPCell cell = new PdfPCell(new Phrase(text, sansFont));
            if(i%2 != 0) {
                cell.setBackgroundColor(evenCol);
            }
            table.addCell(cell);
        }   
    }       
    doc.add(table);
    doc.close();
    byte[] bytes = baos.toByteArray();
    baos.close();
    String base64 = Base64.encodeBytes(bytes);
    return base64;
}

Can anyone please help? 谁能帮忙吗? Thanks 谢谢

ps I have created a sample file . ps我已经创建了一个示例文件。

The reason stated by @mkl above is the problem. 上面@mkl指出的原因就是问题。 I am using wrong fonts. 我使用了错误的字体。 I downloaded the font from here and it is now working. 我从这里下载了字体,现在可以使用了。

Hi you can replace last 4 line with this. 嗨,您可以以此替换最后4行。 It doesn't require to close ByteArrayOutputStream before encode. 编码之前不需要关闭ByteArrayOutputStream。

byte[] bytes = baos.toByteArray();
String base64 = Base64.getEncoder().encodeToString(bytes);
return base64;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过iText读取PDF文件并像在Adobe Reader中一样显示它? - How to read PDF file through iText and display it as in Adobe Reader? 尝试打开使用iText库创建的PDF时,Adobe Reader在Mac上显示错误 - Adobe reader shows error on mac when trying to open pdf which is created using iText library 在Java中使用itext(5.3.0)进行可编辑的PDF。 无法在Adobe Reader X中保存数据 - Editable PDF using itext (5.3.0) in java. Not able to save data in Adobe Reader X iText7 pdf 使用 GlobalSign DSS AATL 证书签名显示 Adobe Reader 中的信任链损坏 - iText7 pdf signing with GlobalSign DSS AATL certificate shows broken trustchain in Adobe Reader itext API无法从Adobe生命周期工具生成的字段中获取动态pdf - itext API Not able to get the fields from Adobe lifecycle tool generated Dynamic pdf 当pdfjs.disabled为true并且安装了adobe reader时,Selenium firefox配置文件打开pdf - Selenium firefox profile opening pdf read when pdfjs.disabled is true & adobe reader is installed 权限拒绝:开放提供者Adobe Reader - Permission Denial: opening provider Adobe Reader 下载以HTML格式生成的iext PDF - Download itext generated PDF in html 无法读取iText生成的pdf - Cannot read pdf generated by iText 为iText生成的PDF编写JUnits - Writing JUnits for PDF generated by iText
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM