将html内容导出为pdf无效

时间:2014-07-01 15:25:58

标签: java html jsp servlets export-to-pdf

到目前为止,我尝试将表内容导出为ex​​cel。它工作正常。但是,我需要将相同的内容导出为pdf。所以我在内容类型中尝试了response.setHeader("Content-Type", "application/pdf");。但是,它不起作用。我得知pdf文件已损坏,无法打开它。

有人可以帮我解决这个问题吗? 这是我的代码。

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>
<!-- Jquery script -->
<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.IOException;

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


/**
 * 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");
        actionExportToExcel(request, response);

    }

  public void actionExportToExcel(HttpServletRequest request, HttpServletResponse response) throws IOException
  {

      String datatoexport = request.getParameter("datatoexport");
      response.setHeader("Content-Type", "application/pdf");
      response.setHeader("Content-Disposition", "attachment; filename=PayHistory.pdf");
      response.setHeader("Cache-Control", "cache, must-revalidate");
      response.setHeader("Pragma", "public");
      response.getWriter().write(datatoexport);     
      response.getWriter().flush();
      response.getWriter().close();
  }

}

1 个答案:

答案 0 :(得分:1)

content-type标头只是客户端/浏览器的提示。浏览器不会在格式之间进行任何转换。

它可能适用于HTML&lt; - &gt; XLS,因为Microsoft XL也设法解释HTML表。实际上,如果您只是将.html文件重命名为.xls文件并且&#34;打开...&#34; Microsoft XL,它将作为电子表格打开。

换句话说,保存带有.xls扩展名的HTML数据最终会导致数据在Microsoft XL中打开。但是,浏览器没有自动转换。 MS XL正在帮忙。

对于PDF,您必须在服务器端生成PDF并将字节发送到客户端。唯一的另一个选择是在浏览器中触发打印对话框,在这种情况下,Chrome允许用户将HTML页面另存为PDF。对于其他浏览器,如果用户有PDF虚拟打印机(免费软件),他将能够将该页面保存为PDF。