如何使用itext将特定的html表内容写入pdf中

时间:2021-05-23 22:22:02

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

我使用iText将表格内容导出为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.

使用itextpdf-5.1.0.jar这是我的JSP页面。

如何使用itext将特定的html表内容写入pdf中

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

如果单击“导出到Excel”按钮,则显示pdf之类的

如何使用itext将特定的html表内容写入pdf中

While getting the string from jsp,

从jsp获取字符串时,

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

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

我得到的内容与table td tr相同......而不是实际值。有帮助吗?

2 个解决方案

#1


3  

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

请看一下示例ParseHtmlTable1和ParseHtmlTable2。他们创建以下PDF:html_table_1.pdf和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:

我冒昧地像这样定义CSS:

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:

现在我们像这样解析CSS和HTML:

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:

您可以将此表添加到PDF文档中。这就是结果:

如何使用itext将特定的html表内容写入pdf中

#2


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

它是因为行$(“#datatoexport”)而生成的.val($(“#customers”)。html());其中Jquery方法.html从html表中检索整个html。有一种从html表中提取文本的简单方法,你需要在jquery中使用一个解析函数,如下文所示:使用jQuery从td单元格获取文本

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?

在上瘾中,您发布的示例不是请求服务器处理文本的最佳形式,请参阅此如何使用Servlet和Ajax?

#1


3  

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

请看一下示例ParseHtmlTable1和ParseHtmlTable2。他们创建以下PDF:html_table_1.pdf和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:

我冒昧地像这样定义CSS:

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:

现在我们像这样解析CSS和HTML:

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:

您可以将此表添加到PDF文档中。这就是结果:

如何使用itext将特定的html表内容写入pdf中

#2


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

它是因为行$(“#datatoexport”)而生成的.val($(“#customers”)。html());其中Jquery方法.html从html表中检索整个html。有一种从html表中提取文本的简单方法,你需要在jquery中使用一个解析函数,如下文所示:使用jQuery从td单元格获取文本

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?

在上瘾中,您发布的示例不是请求服务器处理文本的最佳形式,请参阅此如何使用Servlet和Ajax?