我使用iText jar导出pdf中的内容。我使用FileOutputStream将文件存储在本地(如D:/filename.pdf)。我不需要这样做,而是要显示弹出对话框,并在pdf中询问保存选项。如何实现呢?
Here is my code.
JSP:
<%@ page language="java"
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 ExportToPDF()
{
$('#myForm').submit();
}
</script>
</head>
<body>
<form id="myForm" action="Sample" method="post">
<br><br>
<p>
some text
</p>
<a href="" onclick="ExportToPDF();" target="_blank">Export to Excel</a>
</form>
</body>
</html>
的Servlet
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
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 {
actionExportToPDF(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void actionExportToPDF(HttpServletRequest request, HttpServletResponse response) throws Exception
{
/* Create Connection objects */
Class.forName ("oracle.jdbc.OracleDriver");
Connection conn=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XID","username","password");
Statement stmt = conn.createStatement();
System.out.println(conn);
/* Define the SQL query */
ResultSet query_set = stmt.executeQuery("SELECT first_name,last_name,email,phone,dob,squestion FROM signup");
/* Step-2: Initialize PDF documents - logical objects */
Document my_pdf_report = new Document();
PdfWriter.getInstance(my_pdf_report, new FileOutputStream("D:/pdf_report_from_sql_using_java.pdf"));
my_pdf_report.open();
//we have four columns in our table
PdfPTable my_report_table = new PdfPTable(4);
//create a cell object
PdfPCell table_cell;
while (query_set.next()) {
String first_id = query_set.getString("first_name");
table_cell=new PdfPCell(new Phrase(first_id));
my_report_table.addCell(table_cell);
String last_name=query_set.getString("last_name");
table_cell=new PdfPCell(new Phrase(last_name));
my_report_table.addCell(table_cell);
String email_id=query_set.getString("email");
table_cell=new PdfPCell(new Phrase(email_id));
my_report_table.addCell(table_cell);
String phone_id=query_set.getString("phone");
table_cell=new PdfPCell(new Phrase(phone_id));
my_report_table.addCell(table_cell);
String dob_id=query_set.getString("dob");
table_cell=new PdfPCell(new Phrase(dob_id));
my_report_table.addCell(table_cell);
String squestion_id=query_set.getString("squestion");
table_cell=new PdfPCell(new Phrase(squestion_id));
my_report_table.addCell(table_cell);
}
/* Attach report table to PDF */
my_pdf_report.add(my_report_table);
my_pdf_report.close();
/* Close all DB related objects */
query_set.close();
stmt.close();
conn.close();
}
}
二手罐子:
itextpdf-5.2.0.jar
itextpdf-5.2.0-javadoc.jar
itextpdf-5.2.0-sources.jar
iText的-XTRA-5.2.0.jar
iText的-XTRA-5.2.0-javadoc.jar
iText的-XTRA-5.2.0-sources.jar
ojdbc14-10g.jar
答案 0 :(得分:1)
PdfWriter
可以将PDF写入任何OutputStream
。如果要创建Servlet,则不应将PDF写入FileOutputStream
,而应写入ServletOutputStream
。请查看官方文档中的PdfServlet
示例。在这个例子中,我们将PDF写入内存(使用ByteArrayOutputStream
),一旦我们关闭文档,我们将其写入ServletOutputStream
:OutputStream os = response.getOutputStream(); baos.writeTo(os);
有非常具体的原因您希望首先在内存中创建文档,并在文档完成后将其写入ServletOutputStream
,但这会导致我们解释所有这些原因(它们列在{9的第9章中) {3}}并且解释需要几页。response.setHeader("Content-Disposition", "attachment; filename=\"subscribe.fdf\"");
在这种情况下,我们还提供了文件名的建议。该文件的默认名称为subscribe.fdf
。警告:浏览器的行为可能有所不同,例如由于特定的浏览器功能导致浏览器忽略响应标头。