Reputation:
So far, I tried to export the table contents to excel. It is working fine. But, I need to export the same content to pdf. So I tried response.setHeader("Content-Type", "application/pdf");
in content type. But, it is not working. I am getting the pdf file is corrupted and couldn't open it.
Can someone help me to resolve this? 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>
<!-- 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();
}
}
Upvotes: 1
Views: 926
Reputation: 4253
The content-type header is only a hint to the client/browser. The browser does not do any conversion between formats.
It might work in the case of HTML <-> XLS because Microsoft XL manages to interpret HTML tables also. In fact if you just rename a .html file as a .xls file and "Open with... " Microsoft XL, it will get opened as a spreadsheet.
In other words, it just so happens that saving HTML data with a .xls extension ends up with the data getting opened in Microsoft XL. But, there is no automatic conversion happening at the browser. MS XL is helping out.
In the case of PDF you have to generate the PDF in the server side and send the bytes to client. The only other option is triggering a print dialog box in the browser, in which case Chrome allows user to save the HTML page as a PDF. For other browsers, if the user has a PDF virtual printer (freeware), he will be able to save the page as PDF.
Upvotes: 1