iT邦幫忙

0

iTextpdf改為lowagie openpdf出現The document has no pages報錯問題

  • 分享至 

  • xImage

由於公司最近有品保議題需要將itext元件移除
而我的專案本身有用到itext
參考其他專案做法就是將itext改為openpdf
然後把import套件由itextpdf改為lowagie

但是改完之後總是會發生
ExceptionConverter: java.io.IOException: The document has no pages.
報錯
但我除了改pom之外就沒有改程式了
後來用debug模式捉到出錯的程式

public byte[] generateDocByte() {
		ByteArrayOutputStream bos = null;
		try {
			bos = new ByteArrayOutputStream();
			Document doc = new Document(PageSize.A4);
			PdfWriter writer = PdfWriter.getInstance(doc, bos);

			HeaderFooter event = new HeaderFooter();
			writer.setPageEvent(event);

			doc.open();
			doc.add(new Chunk(""));
			int row = 0;
			int total_row = asnDetailList.size();
			PdfPTable tbl = new PdfPTable(TABLE_COLUMN_WIDTH.length);
			tbl.getDefaultCell().setBorder(Rectangle.NO_BORDER);
			// tbl.getDefaultCell().setFixedHeight(30);
			tbl.setTotalWidth(520);
			tbl.setWidths(TABLE_COLUMN_WIDTH);
			tbl.setLockedWidth(true);
			PdfPCell cell = null;
			for (AsnDetail asnDetail : asnDetailList) {
				cell = new PdfPCell(new Phrase(ITextUtil.getString(asnDetail.getAsnItemNo()), chineseFont));//這邊的iTextUtil只處理文字方面,套件也改為lowagie了
				cell.setHorizontalAlignment(PdfPTable.ALIGN_LEFT); 
				cell.setBorder(Rectangle.NO_BORDER);
				tbl.addCell(cell);
				tbl.addCell(new Phrase(asnDetail.getPoNo(), chineseFont));

				cell = new PdfPCell(new Phrase(ITextUtil.getString(asnDetail.getPoItemNo()), chineseFont));
				cell.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
				cell.setBorder(Rectangle.NO_BORDER); 
				tbl.addCell(cell);
				tbl.addCell(new Phrase(ITextUtil.getString(asnDetail.getMtno()), chineseFont));

				cell = new PdfPCell(new Phrase(ITextUtil.getString(asnDetail.getShipQty(), "#,###"), chineseFont));
				cell.setHorizontalAlignment(PdfPTable.ALIGN_LEFT); 
				cell.setBorder(Rectangle.NO_BORDER);
				tbl.addCell(cell);
				cell = new PdfPCell(new Phrase(ITextUtil.getString(asnDetail.getInvoiceNo()), chineseFont)); 
				cell.setBorder(Rectangle.NO_BORDER);
				tbl.addCell(cell);
				cell = new PdfPCell(new Phrase(ITextUtil.getString(asnDetail.getRemark()), chineseFont)); 
				cell.setBorder(Rectangle.NO_BORDER);
				tbl.addCell(cell);

				tbl.addCell(new Phrase("", chineseFont));
				tbl.addCell(new Phrase("", chineseFont));
				tbl.addCell(new Phrase(" ", chineseFont));
				cell = new PdfPCell(new Phrase(ITextUtil.getString(asnDetail.getDescription()), chineseFont));
				cell.setBorder(Rectangle.NO_BORDER); 
				cell.setColspan(4);
				tbl.addCell(cell);

				row++;
				if (row % ROW_PER_PAGE == 0) {

					doc.add(tbl);

					if (total_row > row) { // 後面 還有 記錄
						doc.newPage();

						tbl = new PdfPTable(TABLE_COLUMN_WIDTH.length);
						tbl.getDefaultCell().setBorder(Rectangle.NO_BORDER);
						// tbl.getDefaultCell().setFixedHeight(30);
						tbl.setTotalWidth(520);
						tbl.setWidths(TABLE_COLUMN_WIDTH);
						tbl.setLockedWidth(true);
					} else {
						tbl = null;
					}
				}
			}
			if (tbl != null)
				doc.add(tbl);

			writer.flush();

			float height = writer.getVerticalPosition(false) - 80;
			if (height < 60) {
				height = 480;
			}

			// 簽名
			tbl = new PdfPTable(5);
			tbl.setTotalWidth(520);
			tbl.setLockedWidth(true);
			tbl.getDefaultCell().setBorder(Rectangle.NO_BORDER);
			tbl.getDefaultCell().setFixedHeight(height);
			tbl.getDefaultCell().setVerticalAlignment(PdfPCell.ALIGN_BOTTOM);
			// tbl.addCell(new Phrase(String.format("客戶簽名: %f | %f", writer.getPageSize().getHeight() , writer.getVerticalPosition(false)), chineseFont));
			tbl.addCell(new Phrase("Deliver:", chineseFont));
			cell = new PdfPCell();
			// cell.setFixedHeight(height);
			cell.setBorder(Rectangle.BOTTOM);
			tbl.addCell(cell);
			tbl.addCell("                 ");
			tbl.addCell(new Phrase("Receiver:", chineseFont));
			tbl.addCell(cell);

			doc.add(tbl);

			// PdfContentByte cb = writer.getDirectContent();
			// ColumnText text = new ColumnText(cb);
			// text.setSimpleColumn(new Phrase("客戶簽名:"), 100, 100, 100, 100000,
			// 15,
			// Element.ALIGN_CENTER);
			// text.go();

			// 結束

			if(writer!=null) {				
				writer.close();//這邊會報錯!
			}
			if(doc!=null) {				
				doc.close();
			}
			return bos.toByteArray();
		} catch (DocumentException e) {
			throw new BaseException(e);
		} finally {
			try {
				if (bos != null)
					bos.close();
			} catch (Exception e) {
				throw new BaseException(e);
			}
		}
	}

最後都會在writer.close();這行發生錯誤ExceptionConverter: java.io.IOException: The document has no pages.

也有參考一些stackoverflow加上doc.add(new Chunk(''));
之類的,但還是沒辦法
請問有人有解嗎?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
WUcheap
iT邦研究生 4 級 ‧ 2024-07-03 11:59:15

請將
bos = new ByteArrayOutputStream();
補上輸出初始大小
bos = new ByteArrayOutputStream(4096);

一樣呢,補上還是一樣的報錯~

WUcheap iT邦研究生 4 級 ‧ 2024-07-03 14:28:05 檢舉

先close doc再close writer試試

if(doc!=null) {
doc.close();
}
if(writer!=null) {
writer.close();
}

我要發表回答

立即登入回答