Java 环境下用 PDFBox 实现 PDF 文档转换 JPG 图片的功能

PDFBox 是一个用于处理 PDF 文档的开源 Java 工具库。该项目允许创建新的 PDF 文档、操作现有文档以及从文档中提取内容。
我们下面要通过 PDFBox 来演示如何将一个 PDF 文件转换成一个 JPG 文件。代码如下:
package com.sunbloger.pdf2jpg.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/japi")
public class render {

    @RequestMapping(value = "/convert", method = RequestMethod.GET)
    public @ResponseBody Map<String, Object> convert () throws Exception
    {
        Map<String, Object> responseMap = new HashMap<String, Object>();
        
        PDDocument doc = null;
        InputStream stream = null;
        FileOutputStream fos = null;
        ByteArrayOutputStream baos = null;
        
        try {

            // 读入 PDF 文件
            stream = new FileInputStream("D:\\workspace-springboot\\1.pdf");
            doc = PDDocument.load(stream);

            // 渲染
            PDFRenderer pdfRenderer = new PDFRenderer(doc);
            BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 300);
            pdfRenderer = null;

            // 写入 JPG 文件
            baos = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", baos);
            byte[] dataList = baos.toByteArray();
            baos = null;
            File file = new File("D:\\workspace-springboot\\1.jpg");
            fos = new FileOutputStream(file);
            fos.write(dataList);
            fos.close();
            fos = null;
            file = null;
            responseMap.put("suc", 1);
            responseMap.put("info", "convert sucess.");
        } catch (Exception e) {
            throw e;
        }
        
        return responseMap;
    }
}
上述代码用 SpringBoot 构建了一个 PDF 转 JPG 的接口,为了方便演示,PDF 和 JPG 文件地址均采用了本地路径,开发者可以自行调整代码,例如改为通过 POST 请求传入 PDF 文件。
pom.xml文件中添加 PDFBox 信息(推荐使用目前 2.0 最新的 2.0.27 版本):
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.27</version>
</dependency>

 

PHP重置JPG图片尺寸的函数

代码如下:

<?php
/**
 * 重置Jpg图片尺寸
 * 
 * @param string $path
 * @param string $filename 源文件名
 * @param int $maxwidth
 * @param int $maxheight
 * @param string $newname 新文件名
 */
function reSizeJpg($path, $filename, $maxwidth, $maxheight, $newname)
{
    $jpg = imagecreatefromjpeg($path.'/'.$filename);
    if ($jpg) {
        $width = imagesx($jpg);
        $height = imagesy($jpg);
    } else {
        return false;
    }
    
    if (($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)) {
        if ($maxwidth && $width > $maxwidth) {
            $widthratio = $maxwidth / $width;
            $resize_width = true;
        }
        if ($maxheight && $height > $maxheight) {
            $heightratio = $maxheight / $height;
            $resize_height = true;
        }
        if ($resize_width && $resize_height) {
            if ($widthratio < $heightratio) {
                $ratio = $widthratio;
            } else {
                $ratio = $heightratio;
            }
        } elseif ($resize_width) {
            $ratio = $widthratio;
        } elseif ($resize_height) {
            $ratio = $heightratio;
        }
        $newwidth = $width * $ratio;
        $newheight = $height * $ratio;
        if (function_exists("imagecopyresampled")) {
            $newim = imagecreatetruecolor($newwidth, $newheight);
            imagecopyresampled($newim, $jpg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        } else {
            $newim = imagecreate($newwidth, $newheight);
            imagecopyresized($newim, $jpg, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        }
        imagejpeg($newim, $path.'/'.$newname);
        imagedestroy($newim);
    } else {
        imagejpeg($jpg, $path.'/'.$newname);
    }
    imagedestroy($jpg);
    return true;
}
?>