I. Description
The Java background defines the function of timing printing, and at the same time, when you need to directly call the printing of the report, due to the different printer model and paper, you need to specify the printer to determine whether the printing is successful and set the return value. The implementation process is described in detail below.
II. Principle
1. Define report operating environment
// First, you need to define the execution environment, so that the database information can be read correctly
// Define the report running environment for the execution of the report
Module module = ActivatorToolBox.simpleLink(new BaseDBActivator(),
new ConfigurationActivator(),
new StateServerActivator(),
new ReportBaseActivator(),
new RestrictionActivator(),
new ReportActivator());
String envpath;//Project path
envpath = "//Applications//FineReport10_325//webapps//webroot//WEB-INF";
SimpleWork.checkIn(envpath);
module.start();
Note: The project path is modified according to the actual situation
2. Define execution template workbook
TemplateWorkBook workbook = TemplateWorkBookIO.readTemplateWorkBook("GettingStartedEN.cpt");
3. Get report parameters and set values
// parameters pass value
Parameter[] parameters = workbook.getParameters();
HashMap<String, String> paraMap = new HashMap<String, String>();
paraMap.put(parameters[0].getName(), "North China");
4. Call the report printing method in Java and judge
Use the formula PrintUtils.printWorkBook(cptPath) to print, and no print window will pop up at this time.
To display the print option dialog box, use PrintUtils.printWorkBook(cptPath, true), where the parameter true is to display the print option dialog box, as shown below:
// Call the report printing method in Java
boolean a = PrintUtils.printWorkBook("GettingStartedEN.cpt", paraMap, true);
if (!a) {
System.out.println("Failed! return" + a);
} else {
System.out.println("Success! return" + a);
}
The return value of printWorkBook() is boolean, and the return value true (printing success) and false (printing failure) can be used to determine whether the printing is successful.
III. Complete code
Note: The project path is modified according to the actual situation
package com.fr.io;
import com.fr.base.Parameter;
import com.fr.config.activator.BaseDBActivator;
import com.fr.config.activator.ConfigurationActivator;
import com.fr.report.RestrictionActivator;
import com.fr.main.TemplateWorkBook;
import com.fr.module.Module;
import com.fr.module.tool.ActivatorToolBox;
import com.fr.print.PrintUtils;
import com.fr.report.ReportActivator;
import com.fr.report.module.ReportBaseActivator;
import com.fr.store.StateServiceActivator;
import com.fr.workspace.simple.SimpleWork;
import java.util.HashMap;
public class JavaPrint {
public static void main(String[] args) {
// First, you need to define the execution environment, so that the database information can be read correctly
// Define the report running environment for the execution of the report
Module module = ActivatorToolBox.simpleLink(new BaseDBActivator(),
new ConfigurationActivator(),
new StateServiceActivator(),
new ReportBaseActivator(),
new RestrictionActivator(),
new ReportActivator());
String envpath;//Project path
envpath = "//Applications//FineReport10_325//webapps//webroot//WEB-INF";
SimpleWork.checkIn(envpath);
module.start();
try {
TemplateWorkBook workbook = TemplateWorkBookIO.readTemplateWorkBook("GettingStarted.cpt");
// parameters pass value
Parameter[] parameters = workbook.getParameters();
HashMap<String, String> paraMap = new HashMap<String, String>();
paraMap.put(parameters[0].getName(), "华北");
// Call the report printing method in Java
boolean a = PrintUtils.printWorkBook("GettingStarted.cpt", paraMap, true);
if (!a) {
System.out.println("Failed! return" + a);
} else {
System.out.println("Success! return" + a);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
module.stop();
}
}
}
Compile and print.
Compile the program, pop up the printer setting window, select the printer to print, and return the result in the background: Success! Returns true.
IV. Mobile
The mobile terminal does not support various printing and exporting methods