azure - NullPointerException 从 azure app 服务下载 pdf 文件,Spring-boot Java 8
问题描述
我有一个带有 Java 8 的 Spring Boot 应用程序,我可以在其中使用 Itext PDF 库创建和下载 PDF 文件,在本地和开发环境中这工作正常,但是当我在我的 azure 应用程序服务中部署应用程序时,我无法下载此文件,我有一个 NullPointerException。
2020-11-24T13:04:12.310887019Z: [INFO] ****Downloading PDF file: /home/site/descargas/
2020-11-24T13:04:13.814155878Z: [INFO] *** ERROR : java.lang.NullPointerException
2020-11-24T13:04:13.815304521Z: [ERROR] com.itextpdf.text.DocumentException: java.lang.NullPointerException
2020-11-24T13:04:13.816149053Z: [ERROR] at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:821)
2020-11-24T13:04:13.816580670Z: [ERROR] at com.itextpdf.text.Document.add(Document.java:277)
我的代码是:
document = new Document();
FileOutputStream coursesFile = new FileOutputStream(DIRECTORIO_TEMP+"cursos.pdf");
PdfWriter writer = PdfWriter.getInstance(document, coursesFile);
PDFEvent eventoPDF = new PDFEvent();
writer.setPageEvent(eventoPDF);
document.open();
//margenes de documento
document.setMargins(45, 45, 80, 40);
PdfPTable table = new PdfPTable(new float[] {15,35,50,10});
table.setWidthPercentage(100);
table.getDefaultCell().setPaddingBottom(20);
table.getDefaultCell().setPaddingTop(20);
Stream.of("ID", "Área", "Nombre", "Horas").forEach(columnTitle -> {
PdfPCell header = new PdfPCell();
header.setBackgroundColor(COLOR_INFRA);
header.setBorderWidth(1);
header.setHorizontalAlignment(Element.ALIGN_CENTER);
header.setPhrase(new Phrase(columnTitle, fuenteCabecera));
table.addCell(header);
});
for (Curso curso : cursos) {
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(new Phrase(curso.getCodigoCurso(), fuenteNegrita ) );
table.addCell(new Phrase(curso.getAreaBean().getNombre_area(), fuenteNormal ));
table.addCell(new Phrase(curso.getNombreCurso(), fuenteNormal ));
PdfPCell celdaHoras = new PdfPCell( new Phrase(curso.getHoras() + "", fuenteNormal ) );
celdaHoras.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(celdaHoras);
}
document.add(new Paragraph(Chunk.NEWLINE));
document.add(new Paragraph(Chunk.NEWLINE));
document.add(table);
document.close();
coursesFile.close();