0

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();
            }
        }
});
}

SCREENSHOT OF AJAX RESPONSE

Binay
  • 1
  • 3
  • 1
    I have read your question three times and still I don't understand your problem. You may want to clarify if you want an answer. For instance, you say *For paragraph and text i've found it can be converted to byte array and can be sent to browser.* To me this doesn't make sense: the byte array is probably a PDF, not a paragraph and text. If that is the case: why would a table be different from a paragraph or text? If you can solve your problem for `Paragraph` objects, then what different about `PdfPTable` objects? Please explain. – Bruno Lowagie Nov 18 '16 at 10:11
  • @BrunoLowagie i'm sorry that you had to go through all the trouble. what i wanted to do was instead of downloading the pdf file in my local drive, open it in the browser tab. what must i change in my servlet to do so. PdfWriter.getInstance(pdfReport, baos); i did this too but browser tab isn't opening... Sorry for the trouble.. im a noob so u know :D – Binay Nov 18 '16 at 15:13
  • OK, that question has been asked before. You need to send the `baos` to the `response.getOutputStream()`. I'll look for a previous answer on Stack Overflow and point you to it in a moment. – Bruno Lowagie Nov 18 '16 at 15:43
  • Does this answer your question: http://stackoverflow.com/questions/26385446 ? – Bruno Lowagie Nov 18 '16 at 15:44
  • This is a more elaborate answer: http://stackoverflow.com/questions/36745151 – Bruno Lowagie Nov 18 '16 at 15:48
  • @BrunoLowagie i've update my servlet but still pdf is not displayed. Is there anything wrong with my servlet. – Binay Nov 20 '16 at 01:50
  • This is wrong: `dfReport.add(new PdfPTable(pdfTable));` It should be `dfReport.add(pdfTable);`. – Bruno Lowagie Nov 20 '16 at 07:44
  • @BrunoLowagie i have corrected the error that u pointed but still pdf is not displayed in browser:: are there any other mistakes. – Binay Nov 20 '16 at 08:40
  • Have you tried it on different OSs en different browsers? I don't see any other apparent problems. Now that you've made that final change, there's a 100% chance that an exception is thrown by the `reportGenerator()` method. Replace `// TODO: handle exception` with code that logs the error and tell us which exception is thrown (including the full stack trace). – Bruno Lowagie Nov 20 '16 at 11:09
  • @BrunoLowagie I've tried printing stackTrace but it isn't throwing any errors. But since i'm using ajax to call the servlet, looks like i got pdf in response of ajax call. If i'm getting pdf in ajax, then that means servlet is fine, right? So, why i'm not getting pdf in browser. Is using ajax to call servlet is wrong? I've added ajax and response Screenshot to the question. – Binay Nov 21 '16 at 03:10
  • @BrunoLowagie apparently the problem was the ajax. I removed the ajax and used
    and it worked. Thank you very much for your help.
    – Binay Nov 21 '16 at 06:48

0 Answers0