最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于SpringBoot制作一個(gè)PDF切圖小工具

 更新時(shí)間:2024年01月19日 10:42:27   作者:樂(lè)樂(lè)家的樂(lè)樂(lè)  
這篇文章主要為大家詳細(xì)介紹了如何基于SpringBoot制作一個(gè)PDF切圖小工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

老婆需要一個(gè)PDF工具,咱能說(shuō)做不出來(lái)?

最近老婆需要將PDF文件分頁(yè)切割并轉(zhuǎn)成圖片,但使用在線工具時(shí)經(jīng)常遇到繁瑣的廣告。她問(wèn)我能否做一個(gè)工具來(lái)簡(jiǎn)化這個(gè)過(guò)程。作為程序員,咱程序員能說(shuō)不會(huì)做嗎?當(dāng)然不能,于是便有了我摸魚(yú)寫(xiě)的一個(gè)小工具,【PDF切圖】,將源碼分享給大家。

【PDF切圖】源碼分享 跟著我一步一步來(lái)

1、首先我們需要一個(gè)SpringBoot項(xiàng)目

可以參考我的文章

【SpringBoot】從0~1 手把手教你搭建 分布式 項(xiàng)目

【SpringBoot】 一個(gè)優(yōu)秀Maven項(xiàng)目的各Model間最佳繼承設(shè)計(jì)

2、添加Maven依賴

在父Pom.xml文件中添加

        <pdfbox.version>2.0.9</pdfbox.version>    
?
            <dependency>
                <groupId>org.apache.pdfbox</groupId>
                <artifactId>pdfbox</artifactId>
                <version>${pdfbox.version}</version>
            </dependency>

pom.xml中添加

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
        </dependency>

3、封裝我們的工具類(lèi)(非常好用,直接分享給你們)

文件工具類(lèi)FileUtils

package com.le.common.utils;
?
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
?
public class FileUtils {
?
?
    /**
     * 獲取不帶擴(kuò)展名的文件名
     * @param fileName
     * @return
     */
    public static String getFileNameRemoveSuffix(String fileName) {
        int pos = fileName.lastIndexOf(".");
        if (pos > 0) {
            return fileName.substring(0, pos);
        }
        return fileName;
    }
?
    /**
     * 遞歸刪除指定目錄及其內(nèi)容。
     *
     * @param directory 要?jiǎng)h除的目錄
     */
    public static void deleteDirectory(File directory) {
        File[] allContents = directory.listFiles();
        if (allContents != null) {
            for (File file : allContents) {
                deleteDirectory(file);
            }
        }
        directory.delete();
    }
?
    /**
     * 將指定目錄中的圖像壓縮為zip文件,并將其寫(xiě)入輸出流中。
     *
     * @param directory     包含圖像文件的目錄
     * @param outputStream  要寫(xiě)入zip文件的輸出流
     * @throws IOException 如果發(fā)生I/O錯(cuò)誤
     */
    public static void zipImages(File directory, OutputStream outputStream) throws IOException {
        try (ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {
            File[] imageFiles = directory.listFiles();
            if (imageFiles != null) {
                for (File imageFile : imageFiles) {
                    ZipEntry zipEntry = new ZipEntry(imageFile.getName());
                    zipOut.putNextEntry(zipEntry);
                    try (FileInputStream fis = new FileInputStream(imageFile)) {
                        byte[] bytes = new byte[1024];
                        int length;
                        while ((length = fis.read(bytes)) >= 0) {
                            zipOut.write(bytes, 0, length);
                        }
                    }
                }
            }
        }
    }
?
    /**
     * 創(chuàng)建臨時(shí)文件
     * @return
     * @throws IOException
     */
    public static File createTempDirectory() throws IOException {
        File tempDir = File.createTempFile("temp", Long.toString(System.nanoTime()));
        if (!tempDir.delete() || !tempDir.mkdir()) {
            throw new IOException("Could not create temp directory: " + tempDir);
        }
        return tempDir;
    }
}

PDF工具類(lèi)PdfUtils

package com.le.common.utils;
?
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
?
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
?
import org.apache.pdfbox.multipdf.Splitter;
?
public class PdfUtils {
?
    /**
     * 將PDF文件拆分為單獨(dú)的圖像,并將它們保存在指定的目錄中。
     * PDF的每一頁(yè)被轉(zhuǎn)換為一個(gè)單獨(dú)的圖像文件。
     *
     * @param inputPdfFile       要拆分的輸入PDF文件
     * @param outputImageDirectory 圖像文件將要保存的目錄
     * @throws IOException 如果發(fā)生I/O錯(cuò)誤
     */
    public static void splitPdfToImages(File inputPdfFile, File outputImageDirectory) throws IOException {
        try (PDDocument document = PDDocument.load(inputPdfFile)) {
            Splitter splitter = new Splitter();
            List<PDDocument> pages = splitter.split(document);
            int pageIndex = 1;
            for (PDDocument page : pages) {
                PDFRenderer renderer = new PDFRenderer(page);
                BufferedImage image = renderer.renderImageWithDPI(0, 300, ImageType.RGB);
                // 寫(xiě)入圖像文件
                File outputImage = new File(outputImageDirectory, "page" + pageIndex + ".png");
                ImageIO.write(image, "png", outputImage);
                pageIndex++;
                page.close();
            }
        }
    }
?
}

4、接口:PDF文件切割成圖片輸出

package com.le.admin.after.api.mytools;
?
import com.le.common.utils.FileUtils;
import com.le.common.utils.PdfUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
?
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
?
@RestController
@RequestMapping("/tools/pdf")
public class PdfToolController {
?
    /**
     * pdf文件切割成圖片輸出
     * @param pdfFile
     * @return
     */
    @PostMapping("/pdfCutToImage")
    public ResponseEntity<byte[]> processPdf(@RequestParam("pdfFile") MultipartFile pdfFile) {
        try {
            File tempDir = FileUtils.createTempDirectory();
            File pdfInputFile = new File(tempDir, pdfFile.getOriginalFilename());
            pdfFile.transferTo(pdfInputFile);
?
            // 將PDF文件拆分為單獨(dú)的圖像
            PdfUtils.splitPdfToImages(pdfInputFile, tempDir);
?
            // 將圖像壓縮為zip文件
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            FileUtils.zipImages(tempDir, baos);
?
            // 刪除臨時(shí)目錄及其內(nèi)容
            FileUtils.deleteDirectory(tempDir);
?
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Disposition", "attachment; filename=images.zip");
?
            return ResponseEntity.ok()
                    .headers(headers)
                    .contentType(MediaType.parseMediaType("application/zip"))
                    .body(baos.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
}

以上就是基于SpringBoot制作一個(gè)PDF切圖小工具的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot PDF切圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

霍林郭勒市| 航空| 荥阳市| 闵行区| 徐汇区| 阿城市| 武鸣县| 博爱县| 枣阳市| 尤溪县| 合江县| 上饶市| 大冶市| 来宾市| 繁昌县| 淮阳县| 绥滨县| 苗栗市| 革吉县| 岳普湖县| 西和县| 思南县| 利辛县| 延庆县| 汾阳市| 忻城县| 高雄市| 嘉祥县| 汾阳市| 诏安县| 余庆县| 新竹县| 长子县| 克什克腾旗| 余庆县| 溧阳市| 顺义区| 锡林郭勒盟| 资阳市| 泸州市| 水富县|