依赖
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
base64转字节数组
import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.dci.common.core.restful.exception.ServiceException;
public class Base64Util {
public static byte[][] parseBase64(String[] base64s) {
byte[][] result = new byte[base64s.length][];
if (ArrayUtil.isEmpty(base64s)) {
throw new ServiceException("base64数组不能为空");
}
int index = 0;
for (String base64 : base64s) {
if (StrUtil.isBlank(base64)) {
continue;
}
String b64encoded = base64.substring(base64.indexOf("base64,") + "base64,".length());
byte[] decode = Base64Decoder.decode(b64encoded);
result[index++] = decode;
}
return result;
}
public static byte[] parseByte64(String base64) {
return parseBase64(new String[]{base64})[0];
}
}
字节数组转Image对象
import com.lowagie.text.BadElementException;
import com.lowagie.text.Image;
import java.io.IOException;
public class ImageUtil {
public static Image[] paresImage(byte[][] bytes) {
int index = 0;
Image[] result = new Image[bytes.length];
for (byte[] bytes1 : bytes) {
try {
result[index++] = Image.getInstance(bytes1);
} catch (BadElementException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static Image paresImage(byte[] bytes) {
return paresImage(new byte[][]{bytes})[0];
}
}
合并PDF
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.dci.common.core.restful.exception.ServiceException;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileOutputStream;
public class PdfUtil {
public static void generatePdf(Image[] images, String filePath) {
if (ArrayUtil.isEmpty(images)) {
throw new ServiceException("图片不能为空");
}
if (StrUtil.isBlank(filePath)) {
throw new ServiceException("生成路径不能为空");
}
if (!filePath.endsWith(".pdf")) {
filePath = filePath + ".pdf";
}
Document doc = new Document();
try {
PdfWriter.getInstance(doc, new FileOutputStream(filePath));
doc.open();
for (Image img : images) {
Float h = img.getHeight();
Float w = img.getWidth();
Integer percent = getPercent(h, w);
img.setAlignment(Image.MIDDLE);
img.scalePercent(percent);
doc.add(img);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
doc.close();
}
}
public static void generatePdf(Image image, String filePath) {
generatePdf(new Image[]{image}, filePath);
}
public static Integer getPercent(Float h, Float w) {
int g = 0;
float g2 = 0.0f;
g2 = 480 / w * 100;
g = Math.round(g2);
return g;
}
}
方法
@Override
public void exportImagePdf(ExportQueryParam param) {
String targetPath = param.getFilePath();
if (StrUtil.isBlank(targetPath)) {
throw new RuntimeException("文件路径不能为空");
}
String[] base64Images = param.getBase64Images();
if (ArrayUtil.isEmpty(base64Images)) {
throw new RuntimeException("base64数组不能为空");
}
byte[][] imageBytes = Base64Util.parseBase64(base64Images);
Image[] images = ImageUtil.paresImage(imageBytes);
PdfUtil.generatePdf(images, targetPath);
log.info("生成pdf文件{}", targetPath);
}
Test
@Test
public void exportPdf() {
String base64 = FileUtil.readString("H:\\base64导出pdf测试数据.txt", StandardCharsets.UTF_8);
ExportQueryParam exportQueryParam = new ExportQueryParam();
exportQueryParam.setFilePath("C:\\Users\\admin\\Desktop\\测试文件\\pdf测试")
.setBase64Images(new String[]{base64});
iExportExcelService.exportImagePdf(exportQueryParam);
}