在实际开发中,使用ureport2导出pdf,内容中有图片或签名是常有的事情,然后正常显示签名或图片,或需要改变图片或签名的大小时,该如何设置呢...
1、显示图片
导出的图片为常用格式时,如jpg、png等,代码无需特殊处理,实现ureport2包下的ImageProvider接口,传入的为有效的http图片地址。模版设计器中选择单元格类型为图片即可,
当导出的为svg格式的签名路径文件时,ureport2无法识别该格式的图片文件,需要代码特殊处理。将svg文件Base64解码为png。
/**
* svg转png字节流
* @param path
* @return
*/
private static byte[] convertSVGToPNG(String path) {
ByteArrayOutputStream byteArrayOutputStream;
try {
URL url = new URL(path.replace("svg//", ""));
TranscoderInput transcoderInput = new TranscoderInput(url.openStream());
byteArrayOutputStream = new ByteArrayOutputStream();
TranscoderOutput transcoderOutput = new TranscoderOutput(byteArrayOutputStream);
ImageTranscoder imageTranscoder = new PNGTranscoder();
imageTranscoder.transcode(transcoderInput, transcoderOutput);
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
log.error("convertSVGToPNG error", e.getMessage());
return null;
}
}
完整代码请看最后~
2、控制图片大小
对于jpg、png等图片格式,在模板设计器中设置高、宽即可控制图片的大小
对于svg签名文件,需要代码中设置高度与宽度
Integer width = 200;
Integer height = 100;
// Base64解码
byte[] byteArr = convertSVGToPNG(path);
InputStream inputStream = new ByteArrayInputStream(byteArr);
BufferedImage bufferedImage = ImageIO.read(inputStream);
Image tmp = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage dimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream input;
try {
ImageIO.write(dimg, "png", os);
input = new ByteArrayInputStream(os.toByteArray());
return input;
} catch (IOException e) {
log.error(e.getMessage());
}
3、ImageProvider完整实现类
import cn.hutool.http.HttpUtil;
import com.bstek.ureport.exception.ReportComputeException;
import com.bstek.ureport.provider.image.ImageProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.ImageTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
@Component
@Slf4j
public class UrImageProvider implements ImageProvider, ApplicationContextAware {
private ApplicationContext applicationContext;
private String baseWebPath;
@Override
public InputStream getImage(String path) {
try {
if (path.startsWith("http://") || path.startsWith("https://")) {
return new ByteArrayInputStream(HttpUtil.downloadBytes(path));
}
if (path.startsWith("svg//")) {
Integer width = 200;
Integer height = 100;
// Base64解码
byte[] byteArr = convertSVGToPNG(path);
InputStream inputStream = new ByteArrayInputStream(byteArr);
BufferedImage bufferedImage = ImageIO.read(inputStream);
Image tmp = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage dimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream input;
try {
ImageIO.write(dimg, "png", os);
input = new ByteArrayInputStream(os.toByteArray());
return input;
} catch (IOException e) {
log.error(e.getMessage());
}
}
path = baseWebPath + path;
return new FileInputStream(path);
} catch (Exception e) {
throw new ReportComputeException(e);
}
}
@Override
public boolean support(String path) {
if (path.startsWith("http://") || path.startsWith("https://")) {
return true;
} else if ((path.startsWith("svg//"))) {
return true;
} else if (baseWebPath != null && (path.startsWith("http://") || path.startsWith("https://"))) {
return true;
}
return false;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (applicationContext instanceof WebApplicationContext) {
WebApplicationContext context = (WebApplicationContext) applicationContext;
baseWebPath = context.getServletContext().getRealPath("/");
}
this.applicationContext = applicationContext;
}
/**
* svg转png字节流
* @param path
* @return
*/
private static byte[] convertSVGToPNG(String path) {
ByteArrayOutputStream byteArrayOutputStream;
try {
URL url = new URL(path.replace("svg//", ""));
TranscoderInput transcoderInput = new TranscoderInput(url.openStream());
byteArrayOutputStream = new ByteArrayOutputStream();
TranscoderOutput transcoderOutput = new TranscoderOutput(byteArrayOutputStream);
ImageTranscoder imageTranscoder = new PNGTranscoder();
imageTranscoder.transcode(transcoderInput, transcoderOutput);
return byteArrayOutputStream.toByteArray();
} catch (Exception e) {
log.error("convertSVGToPNG error", e.getMessage());
return null;
}
}
}