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

相关内容

热门资讯

2027版开挂!锦州麻将开挂多... 2027版开挂!锦州麻将开挂多功能AI教程,锦州麻将其实有开挂教程该软件可以轻松地帮助玩家将锦州麻将...
2027版辅助!欢喜广西麻将辅... 2027版辅助!欢喜广西麻将辅助多功能AIapp,欢喜广西麻将一直有辅助app1、任何欢喜广西麻将透...
2027版外挂!中至九江麻将外... 2027版外挂!中至九江麻将外挂多功能AI攻略,中至九江麻将真是有外挂攻略运中至九江麻将辅助工具,进...
2027版作弊器!开心门一番作... 2027版作弊器!开心门一番作弊器多功能AI工具,开心门一番竟然真的有作弊器工具1、开心门一番免费脚...
2027版开挂!羊城牌乐汇开挂... 2027版开挂!羊城牌乐汇开挂多功能AI软件,羊城牌乐汇好像是有开挂软件小薇(辅助器软件下载)致您一...
2027版透视器!朋友安徽麻将... 2027版透视器!朋友安徽麻将透视器多功能AI攻略,朋友安徽麻将其实真的是有透视器攻略1、起透看视 ...
2027版透视器!紫竹休闲透视... 2027版透视器!紫竹休闲透视器多功能AI方法,紫竹休闲其实是有透视器方法1、点击下载安装,紫竹休闲...
2027版开挂!广安欢欢麻将开... 2027版开挂!广安欢欢麻将开挂多功能AI脚本,广安欢欢麻将果然有开挂脚本1、打开软件启动之后找到中...
2027版开挂!微乐三丁拐开挂... 2027版开挂!微乐三丁拐开挂多功能AI方法,微乐三丁拐切实存在有开挂方法1、下载好微乐三丁拐正确养...
2027版透视挂!喜临门透视挂... 2027版透视挂!喜临门透视挂多功能AI攻略,喜临门一直真的有透视挂攻略1、完成喜临门有辅助插件,帮...