一web打印常见方法:
1,一种简单的方法是在html上调用window.print(),但是这种方法极其“简陋”,除了要弹出一个对话框让我们选择打印机外,还有就是打印的内容不大好控制。
2,利用pdf,先将要打印的内容呈现在pdf上,再点击pdf上的打印按钮,但是一般要执行两步。很多情况下,这是一种不错的选择。
3,利用插件,有ActiveX控件,Applet插件,一下讲解applet打印。
二指定客户端的打印机直接打印
利用java print sevice可以很好地控制打印,此处不直接用Java Print Service,为了更好地控制打印内容的格式,我这里推荐用jasperreport来进行打印。利用applet调用其JRPrintServiceExporter对象来打印。
1,创建一个servlet,负责生产要打印的报表
-----------------------------------------
public class JasperPrintServlet extends HttpServlet
{
/**
*
*/
public void service(
HttpServletRequest request,
HttpServletResponse response
) throws IOException, ServletException
{
ServletContext context = this.getServletConfig().getServletContext();
File reportFile = new File(context.getRealPath("/reports/WebappReport.jasper"));
if (!reportFile.exists())
throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
Map parameters = new HashMap();
parameters.put("ReportTitle", "Address Report");
parameters.put("BaseDir", reportFile.getParentFile());
JasperPrint jasperPrint = null;
try
{
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
jasperPrint =
JasperFillManager.fillReport(
jasperReport,
parameters,
new WebappDataSource()
);
}
catch (JRException e)
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JasperReports - Web Application Sample</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<span class=\"bnew\">JasperReports encountered this error :</span>");
out.println("<pre>");
e.printStackTrace(out);
out.println("</pre>");
out.println("</body>");
out.println("</html>");
return;
}
if (jasperPrint != null)
{
response.setContentType("application/octet-stream");
ServletOutputStream ouputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);
oos.flush();
oos.close();
ouputStream.flush();
ouputStream.close();
}
else
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JasperReports - Web Application Sample</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<span class=\"bold\">Empty response.</span>");
out.println("</body>");
out.println("</html>");
}
}
}2,创建一个Applet,负责调用servlet,同时执行打印
------------------------------------------------------------------------------
public class HelloSwingApplet extends JApplet
{
private URL url = null;
private JasperPrint jasperPrint = null;
public HelloSwingApplet()
{
}
public void init()
{
String strUrl = getParameter("REPORT_URL");
if (strUrl != null)
{
try
{
url = new URL(getCodeBase(), strUrl);
}
catch (Exception e)
{
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
}
if (url != null)
{
try
{
if (jasperPrint == null)
{
jasperPrint = (JasperPrint)JRLoader.loadObject(url);
}
if (jasperPrint != null)
{
//ViewerFrame viewerFrame = new ViewerFrame(this.getAppletContext(), jasperPrint);
//viewerFrame.show();
}
else
{
JOptionPane.showMessageDialog(this, "Empty report.");
}
}
catch (Exception e)
{
StringWriter swriter = new StringWriter();
PrintWriter pwriter = new PrintWriter(swriter);
e.printStackTrace(pwriter);
JOptionPane.showMessageDialog(this, swriter.toString());
}
}
}
public void printReport() throws Exception{
try {
//JRSaver.saveObject(jasperPrint, "PrintServiceReport.jrprint");
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
printRequestAttributeSet.add(MediaSizeName.ISO_A4);
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
//指定打印机,不会弹出打印机选择框(PDFill PDF&Image Writer是我的打印机)
printServiceAttributeSet.add(new PrinterName("PDFill PDF&Image Writer", null));
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
exporter.exportReport();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3,创建一个html,嵌入applet
1)嵌入代码
<APPLET CODE = "HelloSwingApplet.class" archive="SSignedApplet.jar" WIDTH = 400 HEIGHT = 50 >
<PARAM NAME = "REPORT_URL" VALUE ="servlets/jasperprint">
</APPLET>2)编写一个js函数
function print(){
document.applets[0].printReport();
}3)编写一个button测试
<input type="button" value="print" name="button1" οnclick="print();">
利用Applet进行Web打印
最新推荐文章于 2021-03-15 19:30:39 发布