欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于SpringBoot制作一個PDF切圖小工具

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

老婆需要一個PDF工具,咱能說做不出來?

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

【PDF切圖】源碼分享 跟著我一步一步來

1、首先我們需要一個SpringBoot項目

可以參考我的文章

【SpringBoot】從0~1 手把手教你搭建 分布式 項目

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

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、封裝我們的工具類(非常好用,直接分享給你們)

文件工具類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 要刪除的目錄
     */
    public static void deleteDirectory(File directory) {
        File[] allContents = directory.listFiles();
        if (allContents != null) {
            for (File file : allContents) {
                deleteDirectory(file);
            }
        }
        directory.delete();
    }
?
    /**
     * 將指定目錄中的圖像壓縮為zip文件,并將其寫入輸出流中。
     *
     * @param directory     包含圖像文件的目錄
     * @param outputStream  要寫入zip文件的輸出流
     * @throws IOException 如果發(fā)生I/O錯誤
     */
    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)建臨時文件
     * @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工具類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的每一頁被轉(zhuǎn)換為一個單獨(dú)的圖像文件。
     *
     * @param inputPdfFile       要拆分的輸入PDF文件
     * @param outputImageDirectory 圖像文件將要保存的目錄
     * @throws IOException 如果發(fā)生I/O錯誤
     */
    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);
                // 寫入圖像文件
                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);
?
            // 刪除臨時目錄及其內(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制作一個PDF切圖小工具的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot PDF切圖的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Mybatis超級強(qiáng)大的動態(tài)SQL語句大全

    Mybatis超級強(qiáng)大的動態(tài)SQL語句大全

    MyBatis的動態(tài)SQL是基于OGNL表達(dá)式的,它可以幫助我們方便的在SQL語句中實現(xiàn)某些邏輯,下面這篇文章主要給大家介紹了關(guān)于Mybatis超級強(qiáng)大的動態(tài)SQL語句的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • springboot整合EHCache的實踐方案

    springboot整合EHCache的實踐方案

    EhCache是一個純Java的進(jìn)程內(nèi)緩存框架,具有快速、精干等特點(diǎn),是Hibernate中默認(rèn)的CacheProvider。這篇文章給大家介紹了springboot整合EHCache的實踐方案,需要的朋友參考下
    2018-01-01
  • SpringBoot統(tǒng)一返回處理出現(xiàn)cannot?be?cast?to?java.lang.String異常解決

    SpringBoot統(tǒng)一返回處理出現(xiàn)cannot?be?cast?to?java.lang.String異常解決

    這篇文章主要給大家介紹了關(guān)于SpringBoot統(tǒng)一返回處理出現(xiàn)cannot?be?cast?to?java.lang.String異常解決的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • Java中synchronized的幾種使用方法

    Java中synchronized的幾種使用方法

    本文主要介紹了Java中synchronized的幾種使用方法,synchronized可用于修飾普通方法、靜態(tài)方法和代碼塊,下面詳細(xì)內(nèi)容介紹,需要的小伙伴可以參考一下
    2022-05-05
  • Java簡單計算兩個日期月數(shù)差的方法

    Java簡單計算兩個日期月數(shù)差的方法

    這篇文章主要介紹了Java簡單計算兩個日期月數(shù)差的方法,結(jié)合實例形式分析了java使用Calendar類進(jìn)行日期時間操作相關(guān)技巧,需要的朋友可以參考下
    2017-06-06
  • springboot整合nacos的入門Demo及Nacos安裝部署

    springboot整合nacos的入門Demo及Nacos安裝部署

    Nacos?提供了一組簡單易用的特性集,幫助您快速實現(xiàn)動態(tài)服務(wù)發(fā)現(xiàn)、服務(wù)配置、服務(wù)元數(shù)據(jù)及流量管理,Nacos?致力于幫助您發(fā)現(xiàn)、配置和管理微服務(wù),這篇文章主要介紹了springboot整合nacos的入門Demo,需要的朋友可以參考下
    2024-01-01
  • 解析HikariCP一百行代碼輕松掌握多線程

    解析HikariCP一百行代碼輕松掌握多線程

    這篇文章主要為大家介紹了HikariCP一百行代碼解析,輕松掌握多線程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 深入淺出了解happens-before原則

    深入淺出了解happens-before原則

    一提到happens-before原則,就讓人有點(diǎn)“丈二和尚摸不著頭腦”。這個涵蓋了整個JMM中可見性原則的規(guī)則,究竟如何理解,把我個人一些理解記錄下來。下面可以和小編一起學(xué)習(xí)
    2019-05-05
  • k8s部署java項目的實現(xiàn)

    k8s部署java項目的實現(xiàn)

    本文主要介紹了k8s部署java項目的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Mybatis日期格式自動轉(zhuǎn)換需要用到的兩個注解說明

    Mybatis日期格式自動轉(zhuǎn)換需要用到的兩個注解說明

    這篇文章主要介紹了Mybatis日期格式自動轉(zhuǎn)換需要用到的兩個注解說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評論