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.
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)
Next, set Image in the form.
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 ...
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.
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.
Now, try accessing the address specified by the controller.
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