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

相关内容

热门资讯

出乎意料的是"微友辅... 出乎意料的是"微友辅助器免费版"其实是真的有辅助挂(哔哩哔哩)小薇(辅助器软件下载)致您一封信;亲爱...
近日!友间十三道有什么窍门,h... 近日!友间十三道有什么窍门,hhpoker这个软件靠谱吗(竟然是真的神器)-哔哩哔哩1、点击下载安装...
连日来!三哥玩私人局辅助器,w... 连日来!三哥玩私人局辅助器,wepoker辅助透视软件(透视)果然存在有辅助app(哔哩哔哩)1、很...
事发当天"上饶中至辅... 事发当天"上饶中至辅助插件透视"其实确实有辅助插件(哔哩哔哩)一、上饶中至辅助插件透视游戏安装教程牌...
有消息称!越乡游辅助工具,hh... 有消息称!越乡游辅助工具,hhpoker是真的吗(透视)果然是有辅助脚本(哔哩哔哩)1、让任何用户在...
为切实保障!潘潘讲故事有外g挂... 为切实保障!潘潘讲故事有外g挂吗,微玩体育辅助器(好像有挂插件)-哔哩哔哩1、这是跨平台的潘潘讲故事...
受玩家影响"皮皮跑胡... 受玩家影响"皮皮跑胡子修改器"一贯是真的有辅助方法(哔哩哔哩)1、完成皮皮跑胡子修改器辅助器v3.3...
随着!欢乐对决手游辅助,wpk... 随着!欢乐对决手游辅助,wpk有那种辅助吗(透视)真是有辅助技巧(哔哩哔哩)1、欢乐对决手游辅助辅助...
昨日!逗娱碰胡开挂,兴动海满辅... 昨日!逗娱碰胡开挂,兴动海满辅助(真是真的有插件)-哔哩哔哩1、很好的工具软件,可以解锁游戏的兴动海...
黑科技技巧"鄱阳翻精... 黑科技技巧"鄱阳翻精怎么开挂"原来确实有辅助脚本(哔哩哔哩)鄱阳翻精怎么开挂是不是有人用挂微扑克wp...