在JSP中使用PDF打印,demo项目结构如下:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.lowagie.text.*,com.lowagie.text.pdf.*,com.company.pdf.ExportPDF421"%>
<%
String[] header ={"标题A","标题B","标题C","标题D","标题E","标题F","标题G"};
String[] bogusData = { "内容A", "内容B", "内容C", "内容D","内容E", "内容F"};
int [] headerwidths = {8,4,8,12,8,11}; //列宽度
//样式
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontHeader = new Font(bfChinese, 12, Font.BOLD); //Header Style
Font fontContent = new Font(bfChinese, 12, Font.NORMAL); //Header Style
//表
PdfPTable table = new PdfPTable(6); //列数
table.setWidthPercentage(100); //表宽百分比
table.setHorizontalAlignment(Element.ALIGN_CENTER); //表居中
table.setWidths(headerwidths);
//表格标题行
for(int j= 0;j<header.length;j++){
int col=1,row=2;
if(j==2){
col=2;
row=1;
}else if(j>4){
row=1;
}
table.addCell(ExportPDF421.setHeaderCell(header[j],fontHeader,col,row,Element.ALIGN_MIDDLE,Element.ALIGN_CENTER,0.85f));
}
//表格内容行
for (int i = 1; i < 100; i++) {
for (int x = 0; x < bogusData.length; x++) {
table.addCell(ExportPDF421.setContentCell(bogusData[x]+i,fontContent,Element.ALIGN_MIDDLE,Element.ALIGN_CENTER));
}
}
String url = ExportPDF421.print(response,request,table,"文档标题421","abc.pdf");
%>
<html>
<head>
<script type="text/javascript">
function doPrint(){
var params = "?uri=<%=url%>";
var url = encodeURI("pdf.jsp"+params);
document.getElementById("print").src= url;
}
function doView(){
var url = encodeURI("html.jsp");
document.getElementById("print").src= url;
}
</script>
</head>
<body>
<input type="button" value="查看" onclick="doView()"/>
<input type="button" value="打印" onclick="doPrint();"/>
<br>
URL:<%=url %>
<br>
<table>
</table>
<iframe id="print" name="print" src='html.jsp' style="border:1px solid red;" width="800" height="380"></iframe>
</body>
</html>
pdf.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String uri= java.net.URLDecoder.decode(request.getParameter("uri"),"gbk");
uri = null==uri?"":uri;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>分页显示示例</title>
</head>
<body>
<object id="printer" align="center" data="<%=uri%>" type="application/pdf" width="780" height="370"/>
</body>
</html>
html.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>TEST</title>
</head>
<body>
<table border=1 width="780" height="340" cellpadding="1" cellspacing="0" align="center">
<thead align="center">
<tr style="font-weight:bold;">
<td>标题A</td>
<td>标题B</td>
<td>标题C</td>
<td>标题D</td>
<td>标题E</td>
<td>标题F</td>
<td>标题G</td>
</tr>
</thead>
<tbody align="center">
<tr>
<td>内容A</td>
<td>内容B</td>
<td>内容D</td>
<td>内容C</td>
<td>内容E</td>
<td>内容F</td>
</tr>
</tbody>
</table>
</body>
</html>
ExportPDF421.java
package com.company.pdf;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class ExportPDF421 {
private static String DOC_TITLE = "title";
private static String DOC_HEADER = "header";
private static String DOC_CONTENT = "content";
private static String DOC_FOOTER = "footer";
private static String FILE_DIR = "/download/abc/";
private static Font fontZH = null;
public static String print(HttpServletResponse response,HttpServletRequest request,PdfPTable table,String title,String fileName) throws Exception {
// 取URL
String root = request.getContextPath();
String url = request.getRequestURL().toString();
url = url.substring(0, url.indexOf(root)) + root;
url += FILE_DIR+fileName;
// 取真实路径
String rPath = request.getRealPath(FILE_DIR);
//创建文件夹
File f = new File(rPath);
if (!f.isDirectory()) {
f.mkdirs();
}
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
fontZH = new Font(bfChinese, 20, Font.BOLD);
// 插入一个Table
FileOutputStream out = new FileOutputStream(rPath+File.separator + fileName);
Document document = new Document();
PdfWriter.getInstance(document, out);
document.open();
//文件标题
if(null!=title && !"".equals(title)){
PdfPTable tab = new PdfPTable(1); //列数
PdfPCell cell = new PdfPCell(new Phrase(title,fontZH)); //标题
cell.setColspan(1); //跨列
cell.setBorder(Rectangle.NO_BORDER); //去边框
cell.setFixedHeight(50f); //行高
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //上下居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER); //左右居中
tab.addCell(cell);
document.add(tab);
//document.add(new Paragraph("\n\n")); //换行
}
//数据内容
if(null!=title){
document.add(table);
}
document.close();
return url;
}
/**
* 设置表格内容和样式
* @param text
* @param font
* @param colspan
* @param rowspan
* @param align
* @param valign
* @param fill
* @return
*/
public static PdfPCell setCell(String flag,String text,Font font,int colspan,int rowspan,int align,int valign,float fill){
PdfPCell cell = new PdfPCell(new Phrase(text,font));
cell.setVerticalAlignment(align); //上下居中
cell.setHorizontalAlignment(valign); //左右居中
if(DOC_TITLE.equals(flag)){
}else if(DOC_HEADER.equals(flag)){
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setGrayFill(fill);
}else if(DOC_CONTENT.equals(flag)){
}else if(DOC_FOOTER.equals(flag)){
}
return cell;
}
/**
* 设置表格列头
* @param text
* @param font
* @param colspan
* @param rowspan
* @param align
* @param valign
* @param grayfill
* @return
*/
public static PdfPCell setHeaderCell(String text,Font font,int colspan,int rowspan,int align,int valign,float grayfill){
PdfPCell cell = setCell(DOC_HEADER,text,font,colspan,rowspan,align,valign,grayfill);
return cell;
}
/**
* 设置表格内容
* @param text
* @param font
* @param align
* @param valign
* @return
*/
public static PdfPCell setContentCell(String text,Font font,int align,int valign){
PdfPCell cell = setCell(DOC_CONTENT,text,font,1,1,align,valign,1f);
return cell;
}
}
访问:http://127.0.0.1:8080/print/index.jsp 点击"打印",会显示:
点击上图中的打印机图标,即可完成打印