使用带有java的iText 5.1.3 PDF库时,我遇到了一个奇怪的问题。当我使用main方法运行代码并生成PDF时,我的代码工作正常,但是当我在Web应用程序中使用相同的功能时,我需要下载该PDF,它在前端显示异常
HTTP Status 500 - Dispatch[/Invoice] to method funcName returned an exception
虽然它不会在控制台上打印任何异常。 这是我的代码,任何人都可以查看我的代码并解释此代码的问题。
import com.ilex.invoice.reporting.common.IlexReportPDFConstants;
import com.ilex.report.pdf.helper.PDFUtils;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.html.WebColors;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
public class SimpleTable {
public static final String DEST = "results/tables/simple_table.pdf";
public static void main(String[] args) throws IOException, DocumentException, Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SimpleTable().createPdf(DEST);
new SimpleTable().generateInvPDF();
}
public void createPdf(String dest) throws IOException, DocumentException, Exception {
Document document = new Document(PageSize.A4.rotate(),
IlexReportPDFConstants.PAGE_LEFT_MARGIN, IlexReportPDFConstants.PAGE_TOP_MARGIN, IlexReportPDFConstants.PAGE_RIGHT_MARGIN,
IlexReportPDFConstants.PAGE_BOTTOM_MARGIN);
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(76);
table = populateHeader(table);
for (int aw = 0; aw < 150 * 76; aw++) {
table.addCell(getNormalCell("Some Text Here", 1.5f));
}
table.setTotalWidth(800f);
table.setLockedWidth(true);
document.add(table);
document.close();
}
public static PdfPCell getNormalCell(String string, float size)
throws DocumentException, IOException {
if (string != null && "".equals(string)) {
return new PdfPCell();
}
Font f = new Font();
f.setColor(BaseColor.BLACK);
f.setSize(size);
f.setStyle("normal");
PdfPCell cell = new PdfPCell(new Phrase(string, f));
cell.setNoWrap(false);
cell.setBorderWidth(0.1f);
return cell;
}
public static PdfPCell getHeaderCell(String string, float size)
throws DocumentException, IOException {
Font f = new Font();
f.setSize(size);
PdfPCell cell = new PdfPCell(new Phrase(string, f));
cell.setNoWrap(false);
BaseColor myColor = WebColors.getRGBColor("#D3D3D3");
cell.setBackgroundColor(myColor);
cell.setBorderWidth(0.1f);
return cell;
}
PdfPTable populateHeader(PdfPTable table) throws Exception {
table.addCell(getHeaderCell("Invoice #", 1.8f));
table.addCell(getHeaderCell("Approved Date", 1.8f));
table.addCell(getHeaderCell("PO#", 1.8f));
table.addCell(getHeaderCell("Partner", 1.8f));
table.addCell(getHeaderCell("Category", 1.8f));
table.addCell(getHeaderCell("Comcast Vendor ID", 1.8f));
table.addCell(getHeaderCell("Comcast Site Code", 1.8f));
table.addCell(getHeaderCell("Authorized", 1.8f));
table.addCell(getHeaderCell("Invoiced", 1.8f));
table.addCell(getHeaderCell("Approved", 1.8f));
table.addCell(getHeaderCell("Customer", 1.8f));
table.addCell(getHeaderCell("Project", 1.8f));
table.addCell(getHeaderCell("Job", 1.8f));
table.addCell(getHeaderCell("Site", 1.8f));
table.addCell(getHeaderCell("Job Complete", 1.8f));
table.addCell(getHeaderCell("First onsite", 1.8f));
table.addCell(getHeaderCell("Last offsite", 1.8f));
table.addCell(getHeaderCell("PO Complete", 1.8f));
table.addCell(getHeaderCell("PM", 1.8f));
table.addCell(getHeaderCell("Owner", 1.8f));
table.addCell(getHeaderCell("Partner / Invoice", 1.8f));
table.addCell(getHeaderCell("Dup Inv check", 1.8f));
table.addCell(getHeaderCell("Dup PO Number", 1.8f));
table.addCell(getHeaderCell("Layout code", 1.8f));
table.addCell(getHeaderCell("Source Name", 1.8f));
table.addCell(getHeaderCell("Invoice Type", 1.8f));
table.addCell(getHeaderCell("operating Unit", 1.8f));
table.addCell(getHeaderCell("Vendor Number", 1.8f));
table.addCell(getHeaderCell("Vendor Site Code", 1.8f));
table.addCell(getHeaderCell("Invoice Num", 1.8f));
table.addCell(getHeaderCell("Invoice Date", 1.8f));
table.addCell(getHeaderCell("Currency Code", 1.8f));
table.addCell(getHeaderCell("Invoice Amount", 1.8f));
table.addCell(getHeaderCell("Invoice H Desc", 1.8f));
table.addCell(getHeaderCell("Pay Group", 1.8f));
table.addCell(getHeaderCell("Invoice Line Type", 1.8f));
table.addCell(getHeaderCell("Invoice Line Number", 1.8f));
table.addCell(getHeaderCell("Inv L Description", 1.8f));
table.addCell(getHeaderCell("Inv Line Amount", 1.8f));
table.addCell(getHeaderCell("Tax Code", 1.8f));
table.addCell(getHeaderCell("Concatenated Distr Acct", 1.8f));
table.addCell(getHeaderCell("Source Sys Ref", 1.8f));
table.addCell(getHeaderCell("H Attribute3", 1.8f));
table.addCell(getHeaderCell("H Attribute4", 1.8f));
table.addCell(getHeaderCell("H Attribute5", 1.8f));
table.addCell(getHeaderCell("H Attribute6", 1.8f));
table.addCell(getHeaderCell("H Attribut7", 1.8f));
table.addCell(getHeaderCell("H Attribute8", 1.8f));
table.addCell(getHeaderCell("H Attribute9", 1.8f));
table.addCell(getHeaderCell("H Attribut10", 1.8f));
table.addCell(getHeaderCell("H Attribute11", 1.8f));
table.addCell(getHeaderCell("H Attribute12", 1.8f));
table.addCell(getHeaderCell("H Attribute13", 1.8f));
table.addCell(getHeaderCell("H Attribute14", 1.8f));
table.addCell(getHeaderCell("H Attribute15", 1.8f));
table.addCell(getHeaderCell("L Attribute1", 1.8f));
table.addCell(getHeaderCell("L Attribute2", 1.8f));
table.addCell(getHeaderCell("L Attribute3", 1.8f));
table.addCell(getHeaderCell("L Attribute4", 1.8f));
table.addCell(getHeaderCell("L Attribute5", 1.8f));
table.addCell(getHeaderCell("L Attribute6", 1.8f));
table.addCell(getHeaderCell("L Attribute7", 1.8f));
table.addCell(getHeaderCell("L Attribute8", 1.8f));
table.addCell(getHeaderCell("L Attribute9", 1.8f));
table.addCell(getHeaderCell("L Attribute10", 1.8f));
table.addCell(getHeaderCell("L Attribute11", 1.8f));
table.addCell(getHeaderCell("L Attribute12", 1.8f));
table.addCell(getHeaderCell("L Attribute13", 1.8f));
table.addCell(getHeaderCell("L Attribute14", 1.8f));
table.addCell(getHeaderCell("L Attribute15", 1.8f));
table.addCell(getHeaderCell("H Attr Category", 1.8f));
table.addCell(getHeaderCell("L Attr Category", 1.8f));
table.addCell(getHeaderCell("H Terms Date", 1.8f));
table.addCell(getHeaderCell("H Terms Name", 1.8f));
table.addCell(getHeaderCell("H Attribute1", 1.8f));
table.addCell(getHeaderCell("H Legal Entity Name", 1.8f));
return table;
}
/*
* public static void main(String[] args) { try { float indentationLeft = 20f; float indentationLeftTemp = 20f; List prtLst = new List(List.UNORDERED);
* prtLst.setListSymbol("\u2022"); prtLst.add("P1"); prtLst.setListSymbol("+"); prtLst.add("P2"); prtLst.setListSymbol("\u2022"); List prevList = prtLst; for (int i =
* 0; i < 500; i++) { List chldLst2 = new List(List.UNORDERED); chldLst2.setIndentationLeft(indentationLeft); indentationLeftTemp += 20f;
* chldLst2.setListSymbol("\u2022"); chldLst2.add("YY" + i); prevList.add(chldLst2); prevList = chldLst2; } prtLst.add("P3");
*
* Document d = null; if (indentationLeftTemp > 400f) { Rectangle pageSize = new Rectangle(indentationLeftTemp + 200f, indentationLeftTemp + 200f); d = new
* Document(pageSize); } else { d = new Document(); } PdfWriter.getInstance(d, new FileOutputStream("D:/test.pdf")); d.open(); d.add(prtLst); d.close(); } catch
* (Exception e) { e.printStackTrace(); } }
*
*/
private byte[] generateInvPDF() {
File tempFile = PDFUtils.getTempFile();
Document document = null;
OutputStream pdfOut = null;
InputStream pdfIn = null;
try {
//IlexReportPDFConstants.PAGE_SIZE
document = new Document(PageSize.A4.rotate(),
IlexReportPDFConstants.PAGE_LEFT_MARGIN, IlexReportPDFConstants.PAGE_TOP_MARGIN, IlexReportPDFConstants.PAGE_RIGHT_MARGIN,
IlexReportPDFConstants.PAGE_BOTTOM_MARGIN);
pdfOut = new FileOutputStream(tempFile);
PdfWriter writer = PdfWriter.getInstance(document, pdfOut);
document.open();
document.add(new Chunk(""));
PdfPTable table = new PdfPTable(76);
table = populateHeader(table);
for (int aw = 0; aw < 150 * 76; aw++) {
table.addCell(getNormalCell("Some Text Here", 1.5f));
}
table.setTotalWidth(800f);
table.setLockedWidth(true);
// PdfAction action = PdfAction.gotoLocalPage(1, new PdfDestination(PdfDestination.FIT, 0, 10000, 1), writer);
// writer.setOpenAction(action);
writer.setViewerPreferences(PdfWriter.FitWindow | PdfWriter.CenterWindow);
document.add(table);
document.close();
pdfIn = new FileInputStream(tempFile);
return IOUtils.toByteArray(pdfIn);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (document != null) {
try {
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
IOUtils.closeQuietly(pdfOut);
IOUtils.closeQuietly(pdfIn);
try {
if (tempFile.exists()) {
tempFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
尝试将单元格添加到表格i-e table.addCell
时出现问题
当它到达addCell行时它没有说什么,没有抛出异常并且控制转移到generateInvPDF()
的finally块
提前致谢。