Print forms directly to the printer with JasperReports

Overview

With JasperReports, output directly to the printer without outputting the file to PDF etc. A memo of the result of the investigation because there were unusual requirements as mentioned above in the business. There was some sample code on the net, but since the version was old and used a deprecated API, I created a sample to support 6.8.

Environment etc.

Form template

Assuming the following fictitious quotation template.png

Form parameter list

Parameters Key Setting value example
Customer address CLIENT_ADDRESS 1-23-45 Ginza, Chuo-ku, Tokyo
Customer company name CLIENT_NAME Co., Ltd. ○○○○
Customer representative CLIENT_PERSON ○○ ○○
Company address CORP_ADDRESS 1-2-3 Nishi-Shinjuku, Shinjuku-ku, Tokyo
Company name CORP_NAME Co., Ltd. ××××
In-house person in charge SALES_PERSON ×× ××
Estimate No. ESTIMATE_NO No.20190601
Created date CREATED_DATE 2019/06/03
expiration date EXPIRATION_DATE 2019/06/14
Delivery date DELIVERY_DATE 2019/06/28
Payment terms PAYMENT_TERMS Month-end closing Payment at the end of the following month
Remarks REMARKS 8 consumption tax%It is calculated by.

Form field list

field Key Setting value example
Product name item Product AAAA
quantity quantity 10
unit price unit 200
Amount of money price 2000

Sample code

Main.java


public class DirectPrintSample {

	/**JasperReport template file*/
	public static final String REPORT_TEMPLATE = "template/quotation.jrxml";

	/**Output destination printer name*/
	public static final String PRINTER_NAME = "(Output printer name)";

	public static void main(String[] args) {

		//Initialize the form definition
		JasperReport report = null;
		try {
			report = createReport();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JRException e) {
			e.printStackTrace();
		}

		//Create a document (set output data in form definition)
		JasperPrint print = null;
		try {
			print = createPrint(report);
		} catch (JRException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}

		//Directly print the form by specifying the printer
		try {
			exportPrint(print, PRINTER_NAME);
		} catch (JRException e) {
			e.printStackTrace();
		}
	}

	/**
	 *Initialize the form definition.
	 * @throws IOException
	 * @throws JRException
	 */
	public static JasperReport createReport() throws IOException, JRException {

		//Compile the form layout
		try (InputStream is = DirectPrintSample.class.getClassLoader().getResourceAsStream(REPORT_TEMPLATE)) {
			return JasperCompileManager.compileReport(is);
		}
	}

	/**
	 *Set the data to be output as a form.
	 * @param jasperReport
	 * @throws JRException
	 * @throws ParseException
	 */
	public static JasperPrint createPrint(JasperReport jasperReport) throws JRException, ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

		//Parameter set
		Map<String, Object> parameters = new HashMap<>();
		parameters.put("CLIENT_ADDRESS", "1-23-45 Ginza, Chuo-ku, Tokyo");
		parameters.put("CLIENT_NAME", "Co., Ltd. ○○○○");
		parameters.put("CLIENT_PERSON", "○○ ○○");
		parameters.put("CORP_ADDRESS", "1-2-3 Nishi-Shinjuku, Shinjuku-ku, Tokyo");
		parameters.put("CORP_NAME", "Co., Ltd. ××××");
		parameters.put("SALES_PERSON", "×× ××");
		parameters.put("ESTIMATE_NO", "No.20190001");
		parameters.put("CREATED_DATE", sdf.parse("2019/06/03"));
		parameters.put("EXPIRATION_DATE", sdf.parse("2019/06/14"));
		parameters.put("DELIVERY_DATE", sdf.parse("2019/06/28"));
		parameters.put("PAYMENT_TERMS", "Month-end closing Payment at the end of the following month");
		parameters.put("REMARKS", "8 consumption tax%It is calculated by.");

		//Recordset
		List<Item> list = new ArrayList<>();
		list.add(new Item("Product AAAA", 10, 200, 2000));
		list.add(new Item("Product BBBB", 20, 600, 12000));
		list.add(new Item("Product CCCC", 100, 250, 25000));
		JRDataSource dataSource = new JRBeanCollectionDataSource(list);

		return JasperFillManager.fillReport(jasperReport, parameters, dataSource);
	}

	/***
	 *Print the form directly.
	 * @param jasperPrint
	 * @param printerName
	 * @throws JRException
	 */
	private static void exportPrint(JasperPrint jasperPrint, String printerName) throws JRException {

		//Generate output class for printer
		JRPrintServiceExporter exporter = new JRPrintServiceExporter();

		//Set the document to be output
		exporter.setExporterInput(new SimpleExporterInput(jasperPrint));

		//Specify the output destination print printer
		SimplePrintServiceExporterConfiguration conf = new SimplePrintServiceExporterConfiguration();
		HashPrintServiceAttributeSet printAttribute = new HashPrintServiceAttributeSet();
		printAttribute.add(new PrinterName(PRINTER_NAME, Locale.getDefault()));
		conf.setPrintServiceAttributeSet(printAttribute);
		exporter.setConfiguration(conf);

		//Perform printer output
		exporter.exportReport();
	}

}

Item.java


public class Item {

	private String item;
	private int quantity;
	private int unit;
	private int price;

	Item(String item, int quantity, int unit, int price) {
		this.item = item;
		this.quantity = quantity;
		this.unit = unit;
		this.price = price;
	}

	public String getItem() {
		return item;
	}

	public void setItem(String item) {
		this.item = item;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public int getUnit() {
		return unit;
	}

	public void setUnit(int unit) {
		this.unit = unit;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}
}

Output result

The following is a PDF output with the output destination set to Microsoft Print to PDF. print.png

Issues / concerns

Recommended Posts

Print forms directly to the printer with JasperReports
How to access Socket directly with the TCP function of Spring Integration
Assignment to multiple variables with the ternary operator
[Swift] How to link the app with Firebase
pass two arguments to the URI with link_to
Getting Started with Doma-Introduction to the Criteria API
Try to summarize the common layout with rails
How to change the action with multiple submit buttons
`Failed to create the host-only adapter` with docker-machine start`
I want to dark mode with the SWT app
Organized how to interact with the JDK in stages
HTTPS connection with Java to the self-signed certificate server
Addicted to the webpacker that comes standard with Rails 6