Development and application of Web report Finereport printing

Source: Internet
Author: User

Report printing is a frequently encountered problem in the use and development of reports, here is a summary of some typical application cases for Web Report development printing, with the widest range of FineReport as an example.

Case One: Java calls report printing directly

When the Java background defines the function of timed printing and the need to directly invoke the printing of the report, because the printer model and paper are different, the printer needs to be specified so that the print succeeds and the return value is set. So how can this process be achieved?

Implementation process

1. Define the report running environment

Define the report run environment in order to execute the report String Envpath = "D:\\finereport\\develop\\code\\build\\package\\webreport\\web-inf"; Frcontext.setcurrentenv (New Localenv (Envpath));

2. Define the Execution template workbook

Templateworkbook workbook = Templateworkbookio.readtemplateworkbook (Frcontext.getcurrentenv (), "GettingStarted.cpt ");

3. Get report parameters and set values

Parameter pass value parameter[] Parameters = Workbook.getparameters ();             hashmap<string, string> paramap = new hashmap<string, string> (); Paramap.put (Parameters[0].getname (), "North China");

4. Call report printing method in Java and make judgment

Print by Formula Printutils.printworkbook (Cptpath), the Print window does not pop up at this time.

If you want to print the Options dialog box, use Printutils.printworkbook (Cptpath, True), where the parameter is true to display the Print Options dialog box, as shown in the code:

// java calling report printing method                 boolean a = printutils.printworkbook ("GettingStarted.cpt",  paraMap ,  true);                if   (A == false)  {             &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Failure! Return " + a);                }  else {                &NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Success! Return " + a);                } 

where the Printworkbook () return value is Boolean, you can determine whether the print was successful by returning the value True (print succeeded) and False (print failed).

5. Complete code

The procedure shown above can be printed by full code editing, as shown below:

package com.fr.io;    import java.io.file;    import  java.util.hashmap; import com.fr.base.frcontext;      import  com.fr.base.parameter;    import com.fr.dav.localenv;import  com.fr.main.templateworkbook; import com.fr.print.printutils;        public class javaprint {         public  static void main (String[] args)  {              //  define the report run environment in order to execute the report               String envPath =  "d:\\finereport\\develop\\code\\build\\package\\ Webreport\\web-inf ";              Frcontext.setcurrentenv (New localenv (Envpath));              try {                  TemplateWorkBook workbook =  Templateworkbookio.readtemplateworkbook (frcontext.getcurrentenv (),  "GETTINGSTARTED.CPT");                  //  parameter Values                   parameter[]  parameters = workbook.getparameters ();                  HashMap<String, String> paraMap =  New hashmap<string, string> ();                paramap.put (Parameters[0].getname (),  "North");                              Call report printing method in   // java                   boolean a = printutils.printworkbook ("GettingStarted.cpt",  Paramap, true);                  if  (A == false)  {           &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Failure! Back to " + a";                  } else {              &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Success! Return " + a");                  }              } catch  (exception e)  {                  e.printstacktrace ();              }         }      }

Edit the program, pop up the printer Setup window, select the printer to print it, and return the structure in the background: Success! Returns True.

Case two: Print template differs from preview template

Question Feedback

In the process of printing, some users may encounter the following situation: The user system using an IFRAME embedded report, the client browser sees the report style 1, when printing is not seen the effect, It's a style. 2. For example, the user system displays a fixed iframe size for the report and does not fully display the A4 paper size template, so it is paginated when the report is displayed. If this display structure is printed directly on the A4 paper, only the fixed bar data is displayed on each page, and all the blanks below are blank, so that it is neither beautiful nor wasteful paper.

When you click on print, you will be called fr by JS to print using the print template, as follows:

Case three: Inbound data from the current page data

Template multiple pages, such as some of the book template, each paper printing part of the page, each time you print to trigger the event, in the database for the Location book page corresponding to the account book has been printed. Each page has the number of the book in its fixed position, and a record field that has a record in the database that has been printed.

650) this.width=650; "src=" http://img.blog.csdn.net/20160831133749456 "/>

650) this.width=650; "src=" http://img.blog.csdn.net/20160831133756777 "/>

As an example of a finereport-deployed Webreport project to the Tomcat server, the detailed procedures are as follows:

1. Add post-print events to the template

Open the designer, open its Tomcat report in the Common Group directory, click Report > Report Web Properties > Page break Preview settings in the menu, and in the right column, click Add Flash post-print event.

In its JavaScript, the input JS code, the purpose is to invoke its Wedreport project written under a JSP to perform the inbound operation.

2., get the value of the cell that contains the page

If the number ID in J3 cell, that is, the third row, the tenth column, then the JS method is as follows:

var a =$ ("#r -2-0", "Div.reportpane"). Children (). EQ (9) [0].innerhtml;

3. Uploading values to JSP pages via Ajax

The complete code is as follows:

var a =$ ("#r -2-0", "Div.reportpane"). Children (). EQ (9) [0].innerhtml;

$.ajax ({

URL: ' http://localhost:8080/WebReport/print1.jsp?ID= ' +a

})

4. Define JSP get number Modify database table

Define the page print1.jsp, first get the value of the number ID, the connection database through the UPDATE statement to the table toprint field to modify, the code is as follows:

package com.fr.io;import java.io.file;  import java.io.fileinputstream;   import java.io.inputstream;  import java.sql.connection;  import  java.sql.drivermanager;  import java.sql.preparedstatement;  import  com.fr.base.frcontext;  import com.fr.base.env;  public class  Savereporttodatabase {      public static void main (String[]  args)  {          savereport ();       }      private static void savereport ()  {           try {               //  Connecting Databases                string driver =  "Oracle.jdbc.driver.OracleDriver";               String url =  "jdbc:oracle:thin:@192.168.100.169:1521:orcl10g";               String user =  " Temp ";               string pass  =  "Temp123";               Class.forName (Driver);               Connection conn = drivermanager.getconnection (Url, user, pass);               PreparedStatement presmt =  conn                       .preparesTatement ("Insert into report values (?,?)");               //  read into template files that need to be saved in the library               env oldenv =  frcontext.getcurrentenv ();               string envpath = oldenv.getpath ();               file cptfile = new file (envpath                       +   "\\REPORTLETS\\GETTINGSTARTED.CPT");               int lens =  (int)  cptfile.length ();               inputstream ins = nEw fileinputstream (Cptfile);               //  save a template to storage                presmt.setstring (1,  "GETTINGSTARTED.CPT"); //  first field holds template relative path                presmt.setbinarystream (2, ins, lens);  //   The second field holds the binary stream of the template file                Presmt.execute ();               Conn.commit ();               presmt.close ();               conn.close ();           } catch  (exception e)  {         &nbSp;     e.printstacktrace ();           }      }  }

Case FOUR: Submit the printed information to the library

When you print, you sometimes want to know exactly which reports are printed at what time, and record them in a table of records in the database to make it easier to see the information. In general, when you click Print, the Print event will be triggered directly, that is, click to print is the status of print execution. For the entire project, when previewing the report in the browser, click Flash Print to record its print information to the library table as an example.

1. New Information table

Create a new record information table, such as: Create a new table in the Access database, named Table 1. Where two fields are entered: Date and TableName, and the types are dates and text types, respectively.

2. Add post-Print events

Click Server > Server configuration, select the page Break preview settings option, and go to the page Break Preview Settings window. In the right column, click Add Flash post-print event as shown:

650) this.width=650; "class=" Magplus "title=" click to view original size picture "src=" http://img.blog.csdn.net/20160831134110703 "width=" "height=" 568 "/>

Select Submit to storage and select its Information record table, and after clicking the Smart Add field, enter the formula in date as above: =today () gets the current date, and enters the formula in TableName: =reportname gets the report that is currently printed.

Case FIVE: Dynamic printing

When you are viewing a report, you sometimes find one or two important or erroneous messages that need to be printed out to back up, and you need to dynamically implement the print, tick the information you want to print, and print it.

1. Parent Template

    • Data preparation

New template, new data query: SQL is select * FROM employee.

    • Report Body Design

Use the check box control in cell A3 to tick the information you want to print, such as:

650) this.width=650; "src=" http://img.blog.csdn.net/20160831134132568 "/>

    • Gets the value of the check box

To add an initialization event to the check box, place the information for the tick option in the array and save it as follows:

if (! fr.checkboxes) {fr.checkboxes=new Array ();} var len=fr.checkboxes.length; Fr.checkboxes[len]=this;
    • toolbar to add a custom Print button

Click Templates > Templates Web properties > Fill out page settings, choose separate settings for the template, add a custom Print button to the toolbar, and empty the custom Print button

650) this.width=650; "src=" http://img.blog.csdn.net/20160831134231002 "/>

Custom JavaScript with the following code:

var joindata=function () {var datas=[]; for (var i=0;i<fr.checkboxes.length;i++) {var checkbox=fr.checkboxes[i];if ( Checkbox.getvalue () ===true) {var colrow=fr.cellstr2columnrow (checkBox.options.location); Colrow.col++;var location =fr.columnrow2cellstr (Colrow); var value=_g (). Getcellvalue (location,null);d Atas[datas.length]=value;}} Return Datas.join (",");} var data=joindata (); var url= "http://localhost:8075/WebReport/ReportServer?reportlet=doc/Advanced/PrintReport/ Selectprint_son.cpt "+" &ids= "+data;window.onbeforeunload=null; Fr.dourlpdfprint (URL);

and save

1. Sub-template

    • Data preparation

New template, new data query Ds1,sql for SELECT * FROM employee where employee ID in (${ids}), this parameter is to receive the employee ID passed by the parent template.

    • Report Body Design

650) this.width=650; "src=" http://img.blog.csdn.net/20160831134339909 "/> Save

3. Previewing and printing

Open the parent template, click Fill Preview, select the data you want to print, click the Print button on the toolbar to print out the selected information, such as:

650) this.width=650; "class=" Magplus "title=" click to view original size picture "src=" http://img.blog.csdn.net/20160831134412523 "width=" "height=" 193 "/>

Development and application of Web report Finereport printing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.