[英]PDF Generator using iText and java
我需要使用iText生成一個PDF,也要使用ajax。 我給了一個按鈕,單擊時,我必須彈出一個保存pdf文件的彈出窗口,就像通常在許多可下載文件中一樣。 我在代碼中找不到任何錯誤,請仔細閱讀代碼並為我提供幫助。 我必須打印一個簡單的表,其中包含行和列,而我卻不能。
Ajax編碼:
function getFocusedPDF(){
alert("Inside create PDF ajax");
$.ajax({
url : '/PreTestWeb/getFocusedPDF',
type : 'get',
dataType : 'json',
contentType : 'application/json',
success : function(map) {
console.log(map);
},
error : function(map) {
alert(map);
alert("error occured!!!");
},
});
}
家庭控制器:
@RequestMapping(value = "/getFocusedPDF", method = RequestMethod.GET)
public void getRecentFocusGrpData(HttpServletRequest req, HttpServletResponse res) throws IOException, DocumentException {
String contType="application/pdf";
res.setContentType(contType);
Gson json = new Gson();
System.out.println("Inside home ctrlr");
PdfGenerator pdf=new PdfGenerator();
System.out.println("==== Before ===");
byte[] b=pdf.createFirstTable();
System.out.println("==== After ===");
res.setHeader("Content-Disposition",
"attachment; filename=pdf.pdf");
res.setContentLength(b.length);
res.getOutputStream().write(b);
res.getOutputStream().flush();
res.getOutputStream().close();
System.out.println("Last line in home ctrlr pdf generation");
}
createFirstTable :(方法)
public static byte[] createFirstTable() throws DocumentException, FileNotFoundException {
System.out.println("=========Inside pdf generator ==========");
// a table with three columns
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
Document document=new Document();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
writer.setCloseStream(false);
document.open();
PdfPTable table = new PdfPTable(2);// new PdfTable(periodList.size() + 1);
// the cell object
PdfPCell cell = new PdfPCell(new Paragraph ("Merry Moore"));
cell.setColspan(2);
table.addCell(cell);
System.out.println("Added new paragraph");
// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setColspan(2);
table.addCell(cell);
System.out.println("Added new cell");
// we add the four remaining cells with addCell()
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");
document.add(table);
System.out.println("Added table to document");
document.close();
return outputStream.toByteArray();
}
我認為您僅通過Ajax調用就可以做到這一點。 Ajax只能接收文本形式的響應。
如果要提示下載,我的方法是填寫一個可能隱藏的表格,然后通過ajax提交:
<form id="form" action="/downloadPDF.do" method="POST">
<fieldset>
<label for="something">Something :</label>
<input type="text" name="something" id="something"/>
</fieldset>
$( "#button" ).click(function(e) {
e.preventDefault();
$('#form').submit();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.