I had written a code to retrieve the table from the database and convert it to PDF table using Itext library. Currently the PDF file automatically downloads into default location. But i want to display it in the browser and give user the choice to download the PDF.
For paragraph and text i've found it can be converted to byte array and can be sent to browser but what can be done for an entire table?
[Here is my 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;
}
[Here is my 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
}
}
Here is the 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();
}
}
});
}