图片转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();     }  }  

相关内容

热门资讯

做出回应!微乐自建房脚本免费下... 做出回应!微乐自建房脚本免费下载苹果版,微乐小程序黑科技,要领教程(有挂下载)-哔哩哔哩1、打开软件...
第5分钟插件!微信小程序游戏辅... 第5分钟插件!微信小程序游戏辅助器,微乐小程序黑科技,积累教程(有挂app)-哔哩哔哩1、微信小程序...
透视存在!微乐小程序挂哪里,微... 透视存在!微乐小程序挂哪里,微乐小程序黑科技,窍门教程(有挂app)-哔哩哔哩运微乐小程序挂哪里辅助...
五分钟辅助!微信微乐辅助器免费... 五分钟辅助!微信微乐辅助器免费安装,微乐小程序黑科技,积累教程(有挂app)-哔哩哔哩1、游戏颠覆性...
透视安装!微乐自建房辅助多少钱... 透视安装!微乐自建房辅助多少钱一个月,微乐小程序免费黑科技,绝活教程(有挂插件)-哔哩哔哩1、许多玩...
9分钟脚本!微乐小程序辅助开挂... 9分钟脚本!微乐小程序辅助开挂,微乐小程序免费黑科技,项目教程(有挂下载)-哔哩哔哩1、操作简单,无...
透视苹果版!微信微乐游戏辅助脚... 透视苹果版!微信微乐游戏辅助脚本,微乐小程序黑科技,指引教程(有挂修改器)-哔哩哔哩微信微乐游戏辅助...
第4分钟辅助!微信微乐自建房脚... 第4分钟辅助!微信微乐自建房脚本免费下载,微乐小程序黑科技,攻略教程(有挂工具)-哔哩哔哩1、微信微...
透视插件!微乐小程序辅助工具,... 透视插件!微乐小程序辅助工具,微乐小程序免费黑科技,讲义教程(有挂安装)-哔哩哔哩;1、微乐小程序免...
第6分钟脚本!微乐小程序免费黑... 第6分钟脚本!微乐小程序免费黑科技是真的吗,微乐小程序黑科技,积累教程(有挂安装)-哔哩哔哩1、点击...