业务背景:前端由用户签名,将图片放到约定好的文件夹中(D://GIF//1.jpg)再调用后端接口获取带签名的模板 base64 jpg 图片
pom
<!-- ireport包 -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>3.7.6</version>
</dependency>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports-fonts</artifactId>
<version>3.7.6</version>
</dependency>
<!--使用ireport必须导入groovy-all包-->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.7.5</version>
</dependency>
service
public Map powerOfAttorney() throws Exception{
InputStream in = null;
Map<String, Object> parameters = new HashMap<>();
String sqs_img_url = InitServlet.getConfig().get("SQS_IMG_URL");
parameters.put("qm",sqs_img_url);
List<Map<String,String>> list = new ArrayList<>();
Map<String,String> map= new HashMap<>();
list.add(map);
Map resultMap = new HashMap();
try{
in = resourceLoader.getResource("classpath:iReport/sqs.jasper").getInputStream();
byte[] bytes = ireportUtls.reportTopdf(list, parameters, in);
if(bytes != null){
String pdfBase64 = DatatypeConverter.printBase64Binary(bytes);
String base64 = PDFBase64TurnImgBase64.pdfBase64TurnImgBase64(pdfBase64);
resultMap.put("CODE","0");
resultMap.put("BASE64",base64);
resultMap.put("INFO","成功");
}else{
resultMap.put("CODE","1");
resultMap.put("BASE64","");
resultMap.put("INFO","生成base64异常");
}
}catch (Exception e){
resultMap.put("CODE","1");
resultMap.put("BASE64","");
resultMap.put("INFO","生成base64异常");
}
return resultMap;
}
工具类(根据模板生成byte数组)
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
public class ireportUtls {
public static byte[] reportTopdf(List list, Map parameters, InputStream in){
byte[] bytes = null;
JRDataSource jrDataSource = new JRBeanCollectionDataSource(list);
FileOutputStream fileOutputStream = null;
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(in, parameters,jrDataSource);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JRPdfExporter jrPdfExporter = new JRPdfExporter();
jrPdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
jrPdfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
jrPdfExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
jrPdfExporter.exportReport();
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
return null;
}finally {
try{
if (in !=null){
in.close();
}
if (fileOutputStream !=null){
fileOutputStream.close();
}
if(fileOutputStream !=null){
fileOutputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
pdf Base64转jpgBase64
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class PDFBase64TurnImgBase64 {
public static String pdfBase64TurnImgBase64(String base64){
String jpg_base64 = null;
try {
byte [] pdf_bytes = DatatypeConverter.parseBase64Binary(base64);
PDDocument doc = PDDocument.load(pdf_bytes);
int size = doc.getNumberOfPages();
int width = 0;
int[] singleImgRGB;
int shiftHeight = 0;
BufferedImage imageResult = null;
PDDocument pdDocument = doc;
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pageLength = size;
int totalCount = pdDocument.getNumberOfPages() / pageLength + 1;
for (int m = 0; m < totalCount; m++) {
for (int i = 0; i < pageLength; i++) {
int pageIndex = i + (m * pageLength);
if (pageIndex == pdDocument.getNumberOfPages()) {
break;
}
BufferedImage image = renderer.renderImageWithDPI(pageIndex, 150, ImageType.RGB);
int imageHeight = 370;
int imageWidth = 1250;
if (i == 0) {
width = imageWidth;
if ((pdDocument.getNumberOfPages() - m * pageLength) < pageLength) {
imageResult = new BufferedImage(width, imageHeight * (pdDocument.getNumberOfPages() - m * pageLength), BufferedImage.TYPE_INT_RGB);
} else {
imageResult = new BufferedImage(width, imageHeight * pageLength, BufferedImage.TYPE_INT_RGB);
}
} else {
shiftHeight += imageHeight;
}
singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
}
shiftHeight = 0;
}
pdDocument.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(imageResult, "jpg", baos);
byte[] jpg_Bytes = baos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
jpg_base64 = encoder.encodeBuffer(jpg_Bytes).trim();
jpg_base64 = jpg_base64.replaceAll("\n", "").replaceAll("\r", "");
baos.close();
doc.close();
return jpg_base64;
} catch (IOException e) {
e.printStackTrace();
}
return jpg_base64;
}
}
记录一下,写这个出现很多问题,如遇到下次差不多的业务好直接拿来用