基于SpringBoot制作一個(gè)PDF切圖小工具
老婆需要一個(gè)PDF工具,咱能說做不出來?
最近老婆需要將PDF文件分頁切割并轉(zhuǎn)成圖片,但使用在線工具時(shí)經(jīng)常遇到繁瑣的廣告。她問我能否做一個(gè)工具來簡化這個(gè)過程。作為程序員,咱程序員能說不會(huì)做嗎?當(dāng)然不能,于是便有了我摸魚寫的一個(gè)小工具,【PDF切圖】,將源碼分享給大家。
【PDF切圖】源碼分享 跟著我一步一步來
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、封裝我們的工具類(非常好用,直接分享給你們)
文件工具類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文件,并將其寫入輸出流中。
*
* @param directory 包含圖像文件的目錄
* @param outputStream 要寫入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工具類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)換為一個(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);
// 寫入圖像文件
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)文章
JAVA動(dòng)態(tài)代理模式(從現(xiàn)實(shí)生活角度理解代碼原理)
本文主要介紹了JAVA動(dòng)態(tài)代理模式(從現(xiàn)實(shí)生活角度理解代碼原理)的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
java中xml和對(duì)象之間的互相轉(zhuǎn)換方法
在java開發(fā)中我們經(jīng)常會(huì)遇到Xml與對(duì)象互相轉(zhuǎn)換的情況,這篇文章主要給大家介紹了關(guān)于java中xml和對(duì)象之間的互相轉(zhuǎn)換方法,文中給出了兩種解決方法,需要的朋友可以參考下2023-06-06
SpringBoot 利用thymeleaf自定義錯(cuò)誤頁面
這篇文章主要介紹了SpringBoot 利用thymeleaf自定義錯(cuò)誤頁面,幫助大家更好的理解和使用springboot 框架,感興趣的朋友可以了解下2020-11-11
IntelliJ IDEA 安裝 Grep Console插件 自定義控制臺(tái)輸出多顏色格式功能
由于Intellij idea不支持顯示ascii顏色,grep-console插件能很好的解決這個(gè)問題,下面就以開發(fā)JavaEE項(xiàng)目中,結(jié)合Log4j配置多顏色日志輸出功能,感興趣的朋友一起看看吧2020-05-05
Java線程方法之從線程角色到實(shí)戰(zhàn)避坑指南
Java語言支持多線程編程,它允許在同一個(gè)程序中同時(shí)運(yùn)行多個(gè)線程來執(zhí)行不同的任務(wù),這有助于提高程序的執(zhí)行效率和響應(yīng)速度,這篇文章主要介紹了Java線程方法之從線程角色到實(shí)戰(zhàn)避坑指南的相關(guān)資料,需要的朋友可以參考下2025-12-12
SpringCloud之服務(wù)注冊(cè)與發(fā)現(xiàn)Spring Cloud Eureka實(shí)例代碼
這篇文章主要介紹了SpringCloud之服務(wù)注冊(cè)與發(fā)現(xiàn)Spring Cloud Eureka實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04

