I. Common web printing methods:
1. A simple method is to call window on HTML. print (), but this method is extremely "simple". In addition to the pop-up dialog box for us to select a printer, the printed content is not well controlled.
2. Use PDF to display the content to be printed on PDF first, and then click the print button on PDF, but generally two steps are required. In many cases, this is a good choice.
3. Use plug-ins, ActiveX controls, and Applet plug-ins to explain applet printing.
2. Print the printer on the specified client directly
The use of JAVA print sevice can control Printing Well. JAVA print service is not used here. to better control the format of printed content, we recommend that you use jasperreport for printing. Use the applet to call its jrprintserviceexporter object for printing.
1. Create a servlet to produce the report to be printed
-----------------------------------------
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 = NULL;
Try
{
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 ("Out. println ("<title> jasperreports-web application sample </title> ");
Out. println ("<LINK rel = \" stylesheet \ "type = \" text/CSS \ "href = \".. /stylesheet.css \ "Title = \" style \ "> ");
Out. println ("
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 ("
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 ("Out. println ("<title> jasperreports-web application sample </title> ");
Out. println ("<LINK rel = \" stylesheet \ "type = \" text/CSS \ "href = \".. /stylesheet.css \ "Title = \" style \ "> ");
Out. println ("
Out. println ("<body bgcolor = \" white \ "> ");
Out. println ("<SPAN class = \" bold \ "> empty response. </span> ");
Out. println ("</body> ");
Out. println ("}
}
} 2. Create an applet to call the servlet and execute printing at the same time.
------------------------------------------------------------------------------
Public class helloswingapplet extends japplet
{
Private URL url = NULL;
Private jasperprint = NULL;
Public helloswingapplet ()
{
}
Public void Init ()
{
string strurl = getparameter ("report_url");
If (strurl! = NULL)
{< br> try
{< br> url = new URL (getcodebase (), strurl );
}< br> catch (exception e)
{< br> stringwriter swriter = new stringwriter ();
printwriter pwriter = new printwriter (swriter );
E. printstacktrace (pwriter);
joptionpane. showmessagedialog (this, swriter. tostring ();
}< BR >}< br> If (URL! = NULL)
{< br> try
{< br> If (jasperprint = NULL)
{< br> jasperprint = (jasperprint) jrloader. loadobject (URL);
}< br> If (jasperprint! = NULL)
{< br> // viewerframe = new viewerframe (this. getappletcontext (), jasperprint);
// viewerframe. show ();
}< br> else
{< br> joptionpane. showmessagedialog (this, "EMPTY report. ");
}< BR >}< br> catch (exception e)
{< br> stringwriter swriter = new stringwriter ();
printwriter pwriter = new printwriter (swriter);
E. printstacktrace (pwriter);
joptionpane. showmessagedialog (this, swriter. tostring ();
}< BR >}
}
Public void printreport () throws exception {
Try {
// Jrsaver. saveobject (jasperprint, "printservicereport. jrprint ");
Printrequestattributeset = new hashprintrequestattributeset ();
Printrequestattributeset. Add (mediasizename. iso_a4 );
Printserviceattributeset = new hashprintserviceattributeset ();
// Specify the printer. The printer selection box is not displayed (pdfill PDF & image writer is my printer)
Printserviceattributeset. Add (New printername ("pdfill PDF & image writer", null ));
Jrprintserviceexporter = 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. Create an HTML file and embed it into the applet.
1) embedCode
<APPLET code = "helloswingapplet. Class" ARCHIVE = "ssignedapplet. Jar" width = 400 Height = 50>
<Param name = "report_url" value = "servlets/jasperprint">
</APPLET>2) Compile a JS Function
Function print (){
Document. Applets [0]. printreport ();
}3) Compile a button for testing.
<Input type = "button" value = "print" name = "button1" onclick = "Print ();">