实现思路:1.图片base64编码转成图片文件,
2.图片文件转成pdf文件
3.pdf文件转成pdf的base64编码
注意点:在获取图片时, Image.getInstance(source)方法只能获取到文件格式为:gif,jpeg,png,bmp,wmf等格式的图片。
参考的是这几位大佬的文章
主要参考文章:(经过查找把其中的坑填上了)
JAVA将图片的base64转成PDF的base64字符串_张丽峰的博客-CSDN博客_base64转成pdf java
图片转base64位编码
image与base64相互转换_blue5299的博客-CSDN博客_image转base64
图片转pdf
图片转为PDF的bug解决之旅_WoHuaKaiHou的博客-CSDN博客_itextpdf 图片转pdf
需要引入的依赖:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
具体实现的工具类
package com.insigma.ywxt.util;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
public class ImageToPdfUtil2 {
/**
* Description: 将图片的base64转成PDF的base64编码
* @param bas64ImageStr 二维码图片base64
*/
public String imageBase64ToPdfBase64(String bas64ImageStr){
String pdfBase64 = "";//PDF的base64编码
try{
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String time=sdf.format(date);
//临时pdf文件路径 D:\app
//图片名称流水号
String custOrderNbr = time;
//1.根据微厅返回的请求参数生成二维码 base64 编码
bas64ImageStr = bas64ImageStr.replaceAll("\r|\n", "");
//2.获取要存储文件的路径,即获取src资源文件编译后的路径(即classes路径)
String url = this.getClass().getClassLoader().getResource("").getPath();
//对路劲进行拼接添加
String serviceImgPath = url + "serviceImg/";//服务器上存放二维码图片的路径
String servicePdfPath = url + "servicePdf/";//服务器上存放二维码PDF的路径
//3.判断服务器是否存在此文件夹,不存在则新建
File file1 =new File(serviceImgPath);
File file2 =new File(servicePdfPath);
if(!file1.exists() && !file1.isDirectory()) {
file1.mkdir();
}
if(!file2.exists() && !file2.isDirectory()) {
file2.mkdir();
}
//4.二维码图片的文件名字和最终保存的二维码文件路径
String fileImageName = custOrderNbr+"_phone.png";//二维码图片路径名字
String filePdfName = custOrderNbr+"_phone.pdf";//PDF图片路径名字
String lastImagePath = serviceImgPath+fileImageName;//最终二维码图片存放的路径
String lastPdfPath = servicePdfPath+filePdfName;//最终二维码PDF存放的路径
//5.首先保存二维码图片
Base64ToImage(bas64ImageStr,lastImagePath);
//6.然后把二维码图片转成PDF二维码文件进行储存
ImgChangePDF(lastImagePath,lastPdfPath);
//7.最后将PDF转成base64,PDF的base64才是最终能推送到签字版的
File file3 =new File(lastPdfPath);
pdfBase64 = PDFToBase64(file3);
pdfBase64 = pdfBase64.replaceAll("\r|\n", "");
//8.需要删除创建的临时文件
File imagefile = new File(lastImagePath);
if(imagefile.exists()){
imagefile.delete();
}
File pdffile = new File(lastPdfPath);
if(pdffile.exists()){
pdffile.delete();
}
}catch (Exception e){
e.printStackTrace();
}
return pdfBase64;
}
/**
* pdf 转64位编码
* @param file
* @return
*/
public static String PDFToBase64(File file) {
BASE64Encoder encoder = new BASE64Encoder();
FileInputStream fin =null;
BufferedInputStream bin =null;
ByteArrayOutputStream baos = null;
BufferedOutputStream bout =null;
try {
fin = new FileInputStream(file);
bin = new BufferedInputStream(fin);
baos = new ByteArrayOutputStream();
bout = new BufferedOutputStream(baos);
byte[] buffer = new byte[1024];
int len = bin.read(buffer);
while(len != -1){
bout.write(buffer, 0, len);
len = bin.read(buffer);
}
//刷新此输出流并强制写出所有缓冲的输出字节
bout.flush();
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fin.close();
bin.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* base64转image
* @param imgStr
* @param imgFilePath
* @return
*/
//base64 转成图片
public static boolean Base64ToImage(String imgStr, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片
if (imgStr == null) // 图像数据为空
return false;
// 解密
try {
// 解密
Base64.Decoder decoder = Base64.getDecoder();
// 去掉base64前缀 data:image/png;base64,
imgStr = imgStr.substring(imgStr.indexOf(",", 1) + 1, imgStr.length());
byte[] b = decoder.decode(imgStr);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
// 保存图片
OutputStream out = new FileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
// 返回图片的相对路径 = 图片分类路径+图片名+图片后缀
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* 将图片转换成PDF
* @param imageUrl 二维码图片路径 可以调用 FileUtil.getFileList() 方法
* @param target PDF的名字和位置
*/
public static void ImgChangePDF(String imageUrl, String target) {
//创建一个文档对象
Document doc = new Document();
try {
//定义输出文件的位置
PdfWriter.getInstance(doc, new FileOutputStream(target));
//开启文档
doc.open();
//路径
Image img = Image.getInstance(imageUrl);
//获得宽高
Float h = img.getHeight();
Float w = img.getWidth();
//统一压缩
Integer percent = getPercent(h, w);
//图片居中
img.setAlignment(Image.MIDDLE);
//百分比显示图
img.scalePercent(percent);
//设置高和宽的比例
doc.add(img);
// 关闭文档
if(doc != null){
doc.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (BadElementException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
/**
* 压缩
* @param
*/
public static Integer getPercent(Float h,Float w)
{
Integer g=0;
Float g2=0.0f;
g2=480/w*100;
g=Math.round(g2);
return g;
}
}
测试类
package insigma;
import com.insigma.ywxt.util.ImageToPdfUtil;
import com.insigma.ywxt.util.ImageToPdfUtil2;
import lombok.extern.slf4j.Slf4j;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringAmqpTest.class)
public class SpringAmqpTest {
private ImageToPdfUtil2 imageToPdfUtil2;
//具体的图片base64编码 太长了我就不放了
@Value("${pdf.image64}")
private String image64;
@Test
public void imageToPdf(){
imageToPdfUtil2 = new ImageToPdfUtil2();
String pdf642 = imageToPdfUtil2.imageBase64ToPdfBase64(image64);
String aa = pdf642;
}
}
版权声明:本文为hou_tao_gu原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。