我编写了一个代码来从数据库中检索表,并使用Itext库将其转换为PDF表。目前,PDF文件会自动下载到默认位置。但我想在浏览器中显示它并让用户选择下载PDF。
对于段落和文本,我发现它可以转换为字节数组,可以发送到浏览器但是可以为整个表做什么?
[这是我的DaoImpl]
@Override
public PdfPTable reportGenerator() throws SQLException, FileNotFoundException, DocumentException {
String sql = "select * from projects";
Connection conn = Database.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
PdfPTable reportTable = new PdfPTable(4);
PdfPCell tableCell;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
tableCell = new PdfPCell(new Phrase("Id"));
tableCell.setBorderWidth(1);
tableCell.setBorderColor(Color.BLUE);
reportTable.addCell(tableCell);
tableCell = new PdfPCell(new Phrase("Title"));
tableCell.setBorderColor(Color.BLUE);
tableCell.setBorderWidth(1);
reportTable.addCell(tableCell);
tableCell = new PdfPCell(new Phrase("StartDate"));
tableCell.setBorderColor(Color.BLUE);
tableCell.setBorderWidth(1);
reportTable.addCell(tableCell);
tableCell = new PdfPCell(new Phrase("Deadline"));
tableCell.setBorderColor(Color.BLUE);
tableCell.setBorderWidth(1);
reportTable.addCell(tableCell);
int counter = 1;
while (rs.next()) {
String str = Integer.toString(counter);
tableCell = new PdfPCell(new Phrase(str));
tableCell.setBorderWidth(1);
tableCell.setBorderColorTop(Color.blue);
reportTable.addCell(tableCell);
String title = rs.getString("title");
tableCell = new PdfPCell(new Phrase(title));
tableCell.setBorderWidth(1);
tableCell.setBorderColorTop(Color.blue);
reportTable.addCell(tableCell);
Date startDate = rs.getDate("startdate");
String date = df.format(startDate);
tableCell = new PdfPCell(new Phrase(date));
tableCell.setBorderWidth(1);
tableCell.setBorderColorTop(Color.blue);
reportTable.addCell(tableCell);
Date deadlineDate = rs.getDate("deadlinedate");
String date1 = df.format(deadlineDate);
tableCell = new PdfPCell(new Phrase(date1));
tableCell.setBorderWidth(1);
tableCell.setBorderColorTop(Color.blue);
reportTable.addCell(tableCell);
counter++;
}
rs.close();
stmt.close();
conn.close();
return reportTable;
}
[这是我的Servlet]
private void report(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
try {
Document pdfReport = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(pdfReport, baos);
pdfReport.open();
PdfPTable pdfTable = projectDaoImpl.reportGenerator();
pdfReport.add(new PdfPTable(pdfTable));
pdfReport.close();
response.setHeader("Content-Disposition", "inline; filename=\"Report.pdf\"");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/pdf");
response.setContentLength(baos.size());
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
} catch (Exception e) {
// TODO: handle exception
}
}
这是ajax
$('#btnReport').click(function() {
create();
});
function create(){
$.ajax({
type : "GET",
url : "projects/report",
contentType : "application/pdf",
complete : function(response) {
if (response.status === 201) {
alert("PDF Generated");
window.location.reload();
}
}
});
}