基于SpringBoot制作一個PDF切圖小工具
老婆需要一個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的動態(tài)SQL是基于OGNL表達(dá)式的,它可以幫助我們方便的在SQL語句中實現(xiàn)某些邏輯,下面這篇文章主要給大家介紹了關(guān)于Mybatis超級強(qiáng)大的動態(tài)SQL語句的相關(guān)資料,需要的朋友可以參考下2022-05-05SpringBoot統(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-09springboot整合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-01Mybatis日期格式自動轉(zhuǎn)換需要用到的兩個注解說明
這篇文章主要介紹了Mybatis日期格式自動轉(zhuǎn)換需要用到的兩個注解說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08