[JAVA] I tried printing a form with Spring MVC and JasperReports Extra edition (image edition)

Attach an image to the form.

Previous article

That's right. If it is a business document, it will be a stamp. If you use it for play, I think it's a good idea to add a logo mark to make it cool.

Prepare parameters and images

First, prepare the parameters to insert the image. This time we will read from a static file, so set the parameter type to InputStream. (When reading Blob type from DB, it seems better to use ByteArrayInputStream type, not executed)

image.png

Next, set Image in the form.

image.png

Prepare the parameters prepared earlier for Expression. You will be asked what to do with the image source, so select "Enter from Java later" (remember)

If you can do this, you may try it with a preview. Nothing is displayed ...

Read the image file on the Java side.

This time, let's make it in a format that will be displayed on the browser, not in a download format. (My taste) The iron rule of MVC. Write a controller.

PainterPrintController.java


@Controller
public class PainterPrintController {

	@Autowired
	ResourceLoader resource;
	
	@RequestMapping(value = "/paint", method = RequestMethod.GET)
	public ModelAndView paintPrint() {
		ModelAndView rslt = new ModelAndView("reportPrint");
		HashMap<String, Object> data = new HashMap<String, Object>();
		InputStream img =null;
		
		//Get image
		try {
			img = new FileInputStream(resource.getResource("picture/aomin.icon.jpg ").getFile());
			
		} catch (IOException e) {
			//TODO auto-generated catch block
			e.printStackTrace();
		}
		
		//Set to View
		data.put("icon_image", img);
		rslt.addObject("jspData", data);
		
		//Returns a view
		return rslt;
	}

Two elements are required.

  1. Load the image.
  2. Pass the hashmap as a view.

Only this. The process of compiling and outputting the form is left to the JSP side.

First, write the following code in Jsp.

reportPrint.jsp


<%@ page contentType="application/pdf" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.data.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.springframework.core.io.*" %>
<%@ page import="org.springframework.beans.factory.annotation.*" %>
<%@ page import="java.util.*" %> 
<%
	try{
		//GetData And jrxml
		HashMap<String, Object> param = (HashMap<String, Object>)request.getAttribute("jspData");
		String jrxmlFile = session.getServletContext().getRealPath("/report/paint_print.jrxml");
		InputStream input = new FileInputStream(jrxmlFile);
		
		//compile krxml
		JasperReport jasperReport = JasperCompileManager.compileReport(input);
		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, new JREmptyDataSource());
		JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
		
		//print pdf on page
		response.getOutputStream().flush();
		response.getOutputStream().close();
				
	} catch (Exception e){
		e.printStackTrace();
	}

%>

What has changed

reportPrint.jsp


<%@ page contentType="application/pdf" %>

The place called. Here, set the PDF output format.

In addition, the basic method of outputting forms does not change regardless of the download format or display format. Prepare a list for parameter and field data Compile and Output to the output stream.

Since there is no field this time, I am using new JREmptyDataSource () when embedding data.

Run

Now, try accessing the address specified by the controller.

image.png

You have printed the form with the character logo. I think it's a better choice to use it for logos and electronic stamps in business places.

Let's automatically create documents.

Recommended Posts

I tried printing a form with Spring MVC and JasperReports Extra edition (image edition)
I tried printing a form with Spring MVC and JasperReports Extra edition (Variables edition)
I tried printing a form with Spring MVC and JasperReports 1/3 (JasperReports settings)
I tried printing a form with Spring MVC and JasperReports 3/3 (Spring MVC control)
I tried to implement file upload with Spring MVC
I tried to create a shopping site administrator function / screen with Java and Spring
I tried GraphQL with Spring Boot
I tried Flyway with Spring Boot
I tried connecting to MySQL using JDBC Template with Spring MVC
I tried to create a Spring MVC development environment on Mac
I tried playing with BottomNavigationView a little ①
I tried Lazy Initialization with Spring Boot 2.2.0
Implement image input / output with Spring MVC
How to create an Excel form using a template file with Spring MVC
I tried to clone a web application full of bugs with Spring Boot
Just input and output images with Spring MVC
I also tried WebAssembly with Nim and C
I tried to link JavaFX and Spring Framework.
I tried to break a block with java (1)
I tried Spring.
I tried using Wercker to create and publish a Docker image that launches GlassFish 5.
I made a function to register images with API in Spring Framework. Part 1 (API edition)
Spring Security usage memo: Cooperation with Spring MVC and Boot
I started MySQL 5.7 with docker-compose and tried to connect
I tried to get started with Spring Data JPA
I made a Restful server and client in Spring.
I tried OCR processing a PDF file with Java
I wrote a test with Spring Boot + JUnit 5 now
Easy to make LINE BOT with Java Servlet Part 2: I tried image messages and templates
I made a function to register images with API in Spring Framework. Part 2 (Client Edition)
[I tried] Spring tutorial
I tried Spring Batch
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
[Rails] I tried to create a mini app with FullCalendar
Spring Boot Introductory Guide I tried [Accessing Data with JPA]
I tried to verify this and that of Spring @ Transactional
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I tried JAX-RS and made a note of the procedure
I tried to create a padrino development environment with Docker
I tried OCR processing a PDF file with Java part2
I tried to get started with Swagger using Spring Boot
I made a simple MVC sample system using Spring Boot
I tried to express the result of before and after of Date class with a number line