图片转PDF工具
创始人
2024-11-14 17:06:33
0

一、maven依赖

 	org.apache.pdfbox 	pdfbox 	2.0.29 	 		 			commons-logging 			commons-logging 		 	  

二、工具类

import lombok.extern.slf4j.Slf4j; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;  import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Objects;  /**  * @date 2023/11/27 15:02  */  @Slf4j public class ImageToPdfUtils {      /**      * 转换image文件为PDF      *      * @param imgFilePath img文件路径      * @param outFile     文件路径      */     public static void convertImages2Pdf(String imgFilePath, String outFile) {         try {             // 1. 下载你的图片转成字节             List list = getImgByteList(imgFilePath);              // 2. 生成一页 PDF document             PDDocument document = new PDDocument();             for (byte[] imageBytes : list) {                 PDImageXObject image = PDImageXObject.createFromByteArray(document, imageBytes, "image");                 // 这里是你生成PDF自适应图片大小,不设置会默认为A4                 PDRectangle pageSize = new PDRectangle(image.getWidth(), image.getHeight());                 PDPage page = new PDPage(pageSize);                 document.addPage(page);                 // 3.将 图片 添加进PDF document                 PDPageContentStream contentStream = new PDPageContentStream(document, page);                 float pageWidth = pageSize.getWidth();                 float pageHeight = pageSize.getHeight();                 float imageWidth = image.getWidth();                 float imageHeight = image.getHeight();                 float scale = Math.min(pageWidth / imageWidth, pageHeight / imageHeight);                 float scaledWidth = imageWidth * scale;                 float scaledHeight = imageHeight * scale;                 float x = (pageWidth - scaledWidth) / 2;                 float y = (pageHeight - scaledHeight) / 2;                 // 这里是将你的图片填充入pdf页                 contentStream.drawImage(image, x, y, scaledWidth, scaledHeight);                 contentStream.close();             }              // 4. 保存PDF             File outputFile = new File(outFile);             File parentFolder = outputFile.getParentFile();             if (parentFolder != null && !parentFolder.exists()) {                 boolean success = parentFolder.mkdirs();                 log.info("生成PDF文件结果:{}", success);             }             document.save(outputFile);             document.close();         } catch (Exception e) {             log.error("生成PDF文件结果失败,imgFilePath:{},filePath:{}", imgFilePath, outFile, e);         }      }      /**      * 得到img字节列表      *      * @param filePath 文件路径      * @return {@link List}<{@link byte[]}>      */     private static List getImgByteList(String filePath) throws IOException {         // 转换         List imageBytesList = new ArrayList<>();         File folder = new File(filePath);         File[] files = folder.listFiles();         if (Objects.isNull(files) || files.length == 0) {             return imageBytesList;         }          for (File file : files) {             if (file.isFile() && isImage(file)) {                 byte[] imageBytes = convertImageToBytes(file);                 imageBytesList.add(imageBytes);             }         }         return imageBytesList;     }      /**      * 判断文件是否是图片类型      *      * @param file 文件      * @return 返回值      */     private static boolean isImage(File file) {         String fileName = file.getName().toLowerCase();         return fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") ||                 fileName.endsWith(".png") || fileName.endsWith(".gif") ||                 fileName.endsWith(".bmp");     }      /**      * 转换图片文件为Byte数组      *      * @param file 文件      * @return 返回值      */     private static byte[] convertImageToBytes(File file) throws IOException {         BufferedImage image = ImageIO.read(file);         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();         ImageIO.write(image, "jpg", outputStream);         return outputStream.toByteArray();     }  }  

相关内容

热门资讯

五分钟了解!(亲朋千分)真实是... 五分钟了解!(亲朋千分)真实是有挂辅助挂吗,乐乐围棋入门原来真实是有挂,详细教程(有挂教程)-哔哩哔...
两分钟了解!(丁丁麻将)真实是... 两分钟了解!(丁丁麻将)真实是有挂辅助挂吗,开心消外挂透视辅助挂,详细教程(有挂实锤)-哔哩哔哩;超...
盘点十款!Wepoke插件原来... 盘点十款!Wepoke插件原来一直总是有挂,aapoker是正规的,必赢教程(有挂技巧)-哔哩哔哩;...
八分钟了解!Uupoker软件... 八分钟了解!Uupoker软件透明挂,麻辣竞技可以开挂的,详细教程(有挂攻略)-哔哩哔哩;一、麻辣竞...
7分钟神器!hm3德州辅助,w... 7分钟神器!hm3德州辅助,wepoke来好牌,wEPOKE原来一直都是有挂(2024已更新)-哔哩...
2分钟了解!(Wepoke稳赢... 2分钟了解!(Wepoke稳赢)微扑克ai辅助会封号(软件透明挂)确切有挂原来一直都是有挂(2024...
十分钟了解!(浙江大联盟)确实... 十分钟了解!(浙江大联盟)确实是有挂,微乐苏州麻将2021已更新详细教程(有挂方略)-哔哩哔哩;AI...
5分钟了解!(练手麻将)是有挂... 5分钟了解!(练手麻将)是有挂辅助挂吗,皮皮四川麻将其实一直总是有挂,详细教程(真实有挂)-哔哩哔哩...
九分钟代打!约局吧是正规的,a... 九分钟代打!约局吧是正规的,aapoker有手游版,微扑克网页版原来一直都是有挂(2022已更新)-...
教程辅助!wpk后台原来一直都... 大家肯定在之前wpk后台或者wpk后台中玩过教程辅助!wpk后台原来一直都是有挂,菠萝德州辅助神器,...