-1

I have used iText to export the table contents to pdf.

Here is my code:

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
<script src="scripts.js"></script>
<script language="javascript"> 
function exportToExcel()
{

    $("#datatoexport").val($("#customers").html()); 
    $('#myForm').submit();      
}
</script>
</head>
<body>
  <form id="myForm" action="Sample" method="post">
  <div id="customers">
    <table id="exportTableSelector" align="left" border="2">
        <thead>
            <tr bgcolor="lightgreen">
                <th>Sr. No.</th>
                <th>Text Data</th>
                <th>Number Data</th>
            </tr>
        </thead>
        <tbody>
            <%
                for (int i = 0; i < 10; i++) {
            %>
            <tr bgcolor="lightblue">
                <td align="center"><%=i + 1%></td>
                <td align="center">This is text data <%=i%></td>
                <td align="center"><%=i * i%></td>
            </tr>
            <%
                }
            %>
        </tbody>
    </table>
    </div>
    <br><br>
    <p>
    some text
    </p>

    <textarea name="datatoexport" id="datatoexport"></textarea>


    <a href="" onclick="exportToExcel();" target="_blank">Export to Excel</a>
   </form>
</body>
</html>

Servlet:

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Servlet implementation class Sample
 */
public class Sample extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Sample() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        System.out.println("Inside doGet");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        System.out.println("Inside doPost");

        try {
            // Get the text that will be added to the PDF
            String text = request.getParameter("datatoexport");


            if (text == null || text.trim().length() == 0) {
                 text = "You didn't enter any text.";
            }
            // step 1
            Document document = new Document();
            // step 2
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);
            // step 3
            document.open();
            // step 4
            document.add(new Paragraph(text));
            // step 5
            document.close();

            // setting some response headers
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control",
                "must-revalidate, post-check=0, pre-check=0");
            response.setHeader("Pragma", "public");
            // setting the content type
            response.setContentType("application/pdf");
            // the contentlength
            response.setContentLength(baos.size());
            // write ByteArrayOutputStream to the ServletOutputStream
            OutputStream os = response.getOutputStream();
            baos.writeTo(os);
            os.flush();
            os.close();
        }
        catch(DocumentException e) {
            throw new IOException(e.getMessage());
        }
    }



}

Used itextpdf-5.1.0.jar This is my JSP page.

enter image description here

If I click Export to Excel Button, it is showing the pdf like

enter image description here

While getting the string from jsp,

String text = request.getParameter("datatoexport");

I am getting the same content like table td tr... instead of actual values. Any help?

  • You'll need to use the object model for rendering data in the PDF document. `PDFPTable` I think is the name of the object to add a table in. – christopher Jul 02 '14 at 12:29

2 Answers2

4

Please take a look at the examples ParseHtmlTable1 and ParseHtmlTable2. They create the following PDFs: html_table_1.pdf and html_table_2.pdf.

The table is created like this:

StringBuilder sb = new StringBuilder();
sb.append("<table border=\"2\">");
sb.append("<tr>");
sb.append("<th>Sr. No.</th>");
sb.append("<th>Text Data</th>");
sb.append("<th>Number Data</th>");
sb.append("</tr>");
for (int i = 0; i < 10; ) {
    i++;
    sb.append("<tr>");
    sb.append("<td>");
    sb.append(i);
    sb.append("</td>");
    sb.append("<td>This is text data ");
    sb.append(i);
    sb.append("</td>");
    sb.append("<td>");
    sb.append(i);
    sb.append("</td>");
    sb.append("</tr>");
}
sb.append("</table>");

I've taken the liberty to define the CSS like this:

tr { text-align: center; }
th { background-color: lightgreen; padding: 3px; }
td {background-color: lightblue;  padding: 3px; }

In another answer, it was already mentioned that your design is flawed. You should learn how to create a decent architecture. Separating data (e.g. the table) and style (e.g. the colors) is one example where you can improve.

Now we parse the CSS and the HTML like this:

CSSResolver cssResolver = new StyleAttrCSSResolver();
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes()));
cssResolver.addCss(cssFile);
// HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
// Pipelines
ElementList elements = new ElementList();
ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.parse(new ByteArrayInputStream(sb.toString().getBytes()));

Now the elements list contains a single element: your table:

return (PdfPTable)elements.get(0);

You can add this table to your PDF document. This is what the result looks like:

enter image description here

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Instead of just down-voting the answer, one should at least have the decency to explain what is wrong with it. – Bruno Lowagie Jul 02 '14 at 14:12
  • You spoke wrongly. Thats why downvoted. But your work is appreciated.. Answering it as right one –  Jul 02 '14 at 14:33
  • I don't use harsh words to hurt people. I use those words to teach people who otherwise wouldn't listen. It's how my teachers have taught me and although I did not like my teachers when I was still learning, I do appreciate them now. They've taught me well and I'm very thankful for the way they've taught me. – Bruno Lowagie Jul 02 '14 at 14:41
1

It's ocorring because of line $("#datatoexport").val($("#customers").html()); where the Jquery method .html retrieve the entire html from the html table. There is not easy way to extract text from a html table, you will need a parse function in jquery like in this post: Getting text from td cells with jQuery

In adiction, the sample that you post isn't the best form to request a server processing text, look in this How to use Servlets and Ajax?

Community
  • 1
  • 1
Auston
  • 307
  • 2
  • 6
  • The design of the application written by user3152748 is indeed flawed. He should reconsider and go back to the drawing board. Nevertheless, I'll answer the question how to convert an HTML table to PDF. – Bruno Lowagie Jul 02 '14 at 13:01