【问题标题】:java.io.IOException: The document has no pages. Working fine locally but not working after deploying in awsjava.io.IOException:文档没有页面。在本地工作正常,但在 aws 中部署后无法工作
【发布时间】:2020-04-08 02:18:59
【问题描述】:

您好,我正在尝试使用 itextpdf 创建 PDF。 在本地测试时它工作正常。
但是在 aws 部署之后我得到 406 Not Acceptable with java.io.IOException: The document has no pages excpetion。

有很多答案,但与部署问题无关。 我是否需要检查任何网络配置或问题在于 pdf 生成代码?

以下是我的代码实现:
请提出一些解决方案。

public byte[] createPdf(List<Participant> participantList) throws IOException,
        DocumentException, com.google.zxing.WriterException {
    Document document = new Document();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, byteArrayOutputStream);
    document.open();
    document.add(createMainTable(participantList));
    document.close();
    return byteArrayOutputStream.toByteArray();
}

public static PdfPTable createMainTable(List<Participant> optionalParticipant) throws BadElementException,
        IOException, com.google.zxing.WriterException {
    PdfPTable table = new PdfPTable(2);
    logger.info("Main Table was created");
    for (int i = 0; i < optionalParticipant.size(); i++) {
        PdfPCell cell1 = new PdfPCell();
        cell1.setBorderWidth(0);
        cell1.setPadding(10f);
        cell1.addElement(createSubTable(optionalParticipant.get(i)));
        table.addCell(cell1);
    }
    return table;
}

public static PdfPTable createSubTable(Participant participant) throws BadElementException,
        IOException, com.google.zxing.WriterException {

    BaseColor baseColor = new BaseColor(150, 150, 150);
    PdfPTable table = new PdfPTable(2);
    table.setWidthPercentage(100);
    PdfPCell cell, cell1, cell2;
    Font font = new Font();
    font.setSize(10f);
    font.setColor(BaseColor.WHITE);
    String participantName = participant.getFirstName() + " " + participant.getLastName();
    Chunk chunk = new Chunk(participantName, font);
    Paragraph head = new Paragraph("             "+chunk);
    head.setFont(font);
    cell = new PdfPCell(head);
    cell.setColspan(2);
    cell.setBackgroundColor(baseColor);
    cell.setPadding(2f);
    table.addCell(cell);
    String qrData = participant.getQrCodeData();
    Image img = getQRCodeImage(qrData);

    font = new Font();
    font.setSize(5f);

    chunk = new Chunk("\n" + "Event ID: " + participant.getEvent().getEventId() +
            "\n\n" + "Unique ID: " + participant.getUniqueId() +
            "\n\n" + "Email ID: " + participant.getEmail(), font);

    Paragraph body = new Paragraph(chunk);
    cell1 = new PdfPCell(body);
    cell1.setBorderWidthRight(0);
    cell1.setPadding(10f);

    cell2 = new PdfPCell();
    cell2.addElement(img);
    cell2.setBorderWidthLeft(1);
    cell2.setPadding(10f);

    table.addCell(cell1);
    table.addCell(cell2);
    logger.info("Sub Table was created");
    return table;
}

【问题讨论】:

  • 考虑在createPdf 中捕获所有可投掷物并记录它们,包括堆栈跟踪。

标签: java amazon-web-services amazon-ec2 itext


【解决方案1】:

请检查参与者列表不为空且包含元素。您可以在开始使用之前使用记录器打印列表的大小。

logger.info("No of participants: "+participantList.size());

另外,在打开文档后,总是在文档中添加一个空块,这样你就可以避免这个异常。

document.open();
document.add(new Chunk(""));

【讨论】:

    猜你喜欢
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多