Java实现将图片转换成PDF

1.引入依赖

xml 复制代码
<dependency>
   <groupId>org.apache.pdfbox</groupId>
   <artifactId>pdfbox</artifactId>
   <version>2.0.24</version>
</dependency>

2.工具方法

java 复制代码
package com.prescription.transfer.system.utils;

import org.apache.pdfbox.io.IOUtils;
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 java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ImgToPdf {

    /**
     * 图片转Pdf
     *
     * @param imgFile
     * @return
     * @throws IOException
     */
    public static File getPdfFile(File imgFile, String filePath) throws IOException {
        File outputFile = new File(filePath);
        genPdf(readBytesFromFile(imgFile), outputFile);
        return outputFile;
    }

    private static void genPdf(byte[] imageBytes, File outputFile) throws IOException {
        PDDocument document = new PDDocument();
        //2. 生成一页 PDF document
        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();
        document.save(outputFile);
        document.close();
    }

    /**
     * 从文件读取字节
     *
     * @param file 文件
     * @return {@link byte[]}
     * @throws IOException ioexception
     */
    private static byte[] readBytesFromFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = IOUtils.toByteArray(inputStream);
        inputStream.close();
        return bytes;
    }

}
相关推荐
User_芊芊君子3 分钟前
【Java】设计模式——单例、工厂、代理模式
java·设计模式·代理模式
2301_8035545212 分钟前
正向代理,反向代理,负载均衡还有nginx
java·nginx·负载均衡
要开心吖ZSH14 分钟前
软件设计师备考-(十六)数据结构及算法应用(重要)
java·数据结构·算法·软考·软件设计师
向上的车轮22 分钟前
基于Java Spring Boot的云原生TodoList Demo 项目,验证云原生核心特性
java·spring boot·云原生
程序员清风24 分钟前
快手一面:为什么要求用Static来修饰ThreadLocal变量?
java·后端·面试
逍遥德25 分钟前
Java8 Comparator接口 和 List Steam 排序使用案例
java·spring boot·list·排序算法
前行的小黑炭43 分钟前
Android :如何快速让布局适配手机和平板?
android·java·kotlin
_BugMan2 小时前
【IDEA】干活?一个IDEA即可,集成开发平台打造攻略
java·ide·intellij-idea
YA3333 小时前
java设计模式二、工厂
java·开发语言·设计模式
金色天际线-3 小时前
Nginx 优化与防盗链配置指南
java·后端·spring