jsp 实现打印
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp 实现打印相关的知识,希望对你有一定的参考价值。
我的jsp是由三个frame组成的frameset,其中一个frame中有内容,内容下面有“打印”按钮,我想按一下“打印”按钮把这个frame中的内容打印出来,而“打印”按钮不打印出来,要怎么实现?
直接调用windows的打印是没戏了你可以使用程序来先输出一个样式,然后按这个样式去打印
我们之前做了一个用itext组件,在线生成PDF,可以精确两个边与内容的间距
你可以试试看
这是同事写的,你参考下吧,
/**
* 个人总结打印 正
* @param path
* @return
*/
public boolean createPDF1(SumVo sum,HttpServletResponse response)
Document document = new Document(PageSize.A4,33,31,72,30);//建立一个Document对象
ByteArrayOutputStream ba = new ByteArrayOutputStream();
try
PdfWriter writer = PdfWriter.getInstance(document, ba);
//PdfWriter.getInstance(document,new FileOutputStream(path));//建立一个PdfWriter对象
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体
Font headFont = new Font(bfChinese, 10, Font.BOLD);//设置字体大小
Font headFont1 = new Font(bfChinese, 10, Font.NORMAL);//设置字体大小
Font headFont2 = new Font(bfChinese, 14, Font.NORMAL);//设置字体大小
float[] widths = 72f ,72f ,72f ,80f ,72f ,72f ;//设置表格的列宽
PdfPTable table = new PdfPTable(widths);//建立一个pdf表格
table.setTotalWidth(440);//设置表格的宽度
table.setLockedWidth(true);
PdfPCell cell = new PdfPCell(new Paragraph(sum.getExam().getName(),headFont2));//建立一个单元格
cell.setBorder(0);
cell.setFixedHeight(40);
cell.setColspan(6);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("考核对象",headFont));//建立一个单元格
cell.setBorderWidth(1.5f);
cell.setFixedHeight(20);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(sum.getUser().getCname(),headFont));
cell.setBorderWidth(1.5f);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("岗位",headFont));
cell.setBorderWidth(1.5f);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(sum.getUser().getDuty().getName(),headFont));
cell.setBorderWidth(1.5f);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("填表日期",headFont));
cell.setBorderWidth(1.5f);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(sum.getCtime().substring(0,10),headFont));
cell.setBorderWidth(1.5f);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("个人总结 t(限1500字)",headFont1));
cell.setBorderWidth(1.5f);
cell.setFixedHeight(657);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置内容水平居中显示
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(sum.getScontent(),headFont1));
cell.setBorderWidth(1.5f);
cell.setFixedHeight(657);
cell.setColspan(5);
table.addCell(cell);
document.add(table);
document.close();
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
catch (DocumentException de)
System.err.println(de.getMessage());
return false;
catch (IOException ioe)
System.err.println(ioe.getMessage());
return false;
return true;
/**
* 打印 个人总结 反面
* @param path
* @param sum
* @return
*/
public boolean createPDF2(SumVo sum,HttpServletResponse response)
Document document = new Document(PageSize.A4,32,31,72,30);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
try
PdfWriter writer = PdfWriter.getInstance(document, ba);
//PdfWriter.getInstance(document,new FileOutputStream("c:/ss.pdf"));//建立一个PdfWriter对象
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体
BaseFont bfComic = BaseFont.createFont("c:\windows\fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font headFont = new Font(bfChinese, 10, Font.BOLD);
Font headFont1 = new Font(bfChinese, 10, Font.UNDERLINE);
Font font = new Font(bfComic, 12);
Font font1 = new Font(bfComic, 12,Font.UNDERLINE);
float[] widths = 70f, 340f ;
PdfPTable table = new PdfPTable(widths);
table.setTotalWidth(440);
table.setLockedWidth(true);
PdfPCell cell = new PdfPCell(new Paragraph("考勤情况",headFont));
cell.setBorderWidth(1.5f);
cell.setFixedHeight(100);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(sum.getAttendance(),headFont));
cell.setBorderWidth(1.5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("综合评分",headFont));
cell.setBorderWidth(1.5f);
cell.setFixedHeight(100);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(sum.getComp(),headFont));
cell.setBorderWidth(1.5f);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("nnn部门考核意见",headFont));
cell.setBorderWidth(1.5f);
cell.setBorder(13);
cell.setFixedHeight(192);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(sum.getDepview(),headFont));
cell.setBorderWidth(1.5f);
cell.setBorder(13);
table.addCell(cell);
cell = new PdfPCell();
cell.setBorderWidth(1.5f);
cell.setBorder(14);
cell.setFixedHeight(45);
table.addCell(cell);
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy年tMMt月tddt日");
String date ="年 t月 t日 ";
try
if(sum.getDeptime()!=null&&sum.getDeptime()!="")
date =sdf1.format(sdf.parse(sum.getDeptime()));
catch (ParseException e)
e.printStackTrace();
cell = new PdfPCell(new Paragraph(date+" ",headFont));
cell.setBorderWidth(1.5f);
cell.setBorder(14);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(" nn个人意见",headFont));
cell.setBorderWidth(1.5f);
cell.setBorder(13);
cell.setFixedHeight(75);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(sum.getMyview(),headFont));
cell.setBorderWidth(1.5f);
cell.setBorder(13);
table.addCell(cell);
cell = new PdfPCell();
cell.setBorderWidth(1.5f);
cell.setBorder(14);
cell.setFixedHeight(25);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("签名: n 年 t月 t日 ",headFont));
cell.setBorderWidth(1.5f);
cell.setBorder(14);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("nnnn院考核意见",headFont));
cell.setBorderWidth(1.5f);
cell.setBorder(13);
cell.setFixedHeight(60);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
String key ="合格";
if("1".equals(sum.getStatus2())) key ="优秀";
else if("2".equals(sum.getStatus2())) key ="合格";
else if("3".equals(sum.getStatus2())) key ="基本合格";
else if("4".equals(sum.getStatus2())) key ="不合格";
// cell = new PdfPCell(new Paragraph(""+sum.getUser().getCname()+" 同志,在"+sum.getExam().getName()+"中,被评为 "+key,headFont));
Phrase myPhrase = new Phrase(12);
myPhrase.add(new Phrase(sum.getUser().getCname(), font1));
myPhrase.add(new Phrase(" 同志,在 ", font));
myPhrase.add(new Phrase(sum.getExam().getName().substring(0, 4)+"年度考核", font1));
myPhrase.add(new Phrase(" 中,被评为 ", font));
myPhrase.add(new Phrase(key, font1));
myPhrase.add(new Phrase(" 。", font));
cell = new PdfPCell(myPhrase);
cell.setBorderWidth(1.5f);
cell.setBorder(13);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell();
cell.setBorderWidth(1.5f);
cell.setBorder(14);
cell.setFixedHeight(40);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("年 t月 t日 ",headFont));
cell.setBorderWidth(1.5f);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setBorder(14);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("备注",headFont));
cell.setBorderWidth(1.5f);
cell.setFixedHeight(80);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(sum.getDescr(),headFont));
cell.setBorderWidth(1.5f);
table.addCell(cell);
document.add(table);
document.close();
response.setContentType("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
catch (DocumentException de)
System.err.println(de.getMessage());
return false;
catch (IOException ioe)
System.err.println(ioe.getMessage());
return false;
return true;
参考技术A jsp打印功能是通过js来实现的。
打印的页面部分,再通过javascript函数print()调用浏览器自带的打印功能,也可以直接实现打印。
以下通过实例进行说明。
< !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>
< meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
< title>JSP中实现打印功能</title>
< style type="text/css">
< !--
.STYLE5 color: #993399; font-size: xx-large;
.STYLE6 font-size: xx-large
-->
< /style>
< !-- 将不用打印出来的地方隐藏了-->
< style type="text/css" media="print">
< !--
.disdisplay:none;
-->
< /style>
< /head>
< body>
< table width="616" height="350" border="1" align="center">
<tr>
<td colspan="2"> <span class="STYLE6">bbbbb </span></td>
</tr>
<tr>
<td><span class="STYLE5">aaa</span></td>
<td bgcolor="#99CCCC"><span class="STYLE6">bbbb</span></td>
</tr>
<tr>
<td><span class="STYLE5">aaa</span></td>
<td bgcolor="#99CCCC"><span class="STYLE6">bbbb</span></td>
</tr>
<tr>
<td><span class="STYLE5">aaa</span></td>
<td bgcolor="#99CCCC"><span class="STYLE6">bbbbb</span></td>
</tr>
< /table>
<p class="dis" align="center">
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id="WebBrowser" width=0> </OBJECT>
< input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开>
< input name=Button onClick=document.all.WebBrowser.ExecWB(2,1) type=button value=关闭所有>
< input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存为>
< input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印>
< input name=Button onClick=document.all.WebBrowser.ExecWB(6,6) type=button value=直接打印>
< input name=Button onClick=document.all.WebBrowser.ExecWB(7,1) type=button value=打印预览>
< input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=页面设置>
< input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=属性>
< input name=Button onClick=document.all.WebBrowser.ExecWB(17,1) type=button value=全选>
< input name=Button onClick=document.all.WebBrowser.ExecWB(22,1) type=button value=刷新>
< input name=Button onClick=document.all.WebBrowser.ExecWB(45,1) type=button value=关闭>
</p>
< /body>
< /html>
将arraylist导入并打印到JSP
【中文标题】将arraylist导入并打印到JSP【英文标题】:Import and print arraylist to JSP 【发布时间】:2012-11-14 10:43:59 【问题描述】:我有一个简单的 jsp 页面 (page.jsp) 和一个简单的 java 类 (classWithArray),其中包含一个 arraylist (list)。
如何从 jsp 中访问数组列表,例如在表格中显示它?
【问题讨论】:
【参考方案1】:使用 JSTL 的 forEach
标记在 JSP 中迭代集合(在这种情况下特别是 ArrayList
),请查看 post 了解更多详细信息。
【讨论】:
【参考方案2】:您可以使用 JSTL 中的 c:forEach
标记遍历 ArrayList
下面是迭代 peopleList 列表的示例代码。
<c:forEach var="person" items="$people.peopleList">
<tr>
<td>$person.name</td>
</tr>
</c:forEach>
在你的JSP页面中使用页面标签导入一个java.util.List
<%@ page import="java.util.List" %>
【讨论】:
以上是关于jsp 实现打印的主要内容,如果未能解决你的问题,请参考以下文章