简体   繁体   中英

Itext generated pdf not opening in Adobe reader?

I am using itext(version 5.5.4) to create a pdf file in a server. 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)".

在此处输入图片说明

This pdf file opens in other application(like evince, foxit and google chrome) just fine. 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 .

The reason stated by @mkl above is the problem. 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. It doesn't require to close ByteArrayOutputStream before encode.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM