Java实现图片转换PDF文件的示例代码图片转换pdf工具

这篇文章主要介绍了Java实现图片转换PDF文件的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

最近因为一些事情,需要将一张简单的图片转换为PDF的文件格式,在网上找了一些工具,但是这些工具不是需要注册账号,就是需要下载软件。

而对于只是转换一张图片的情况下,这些操作显然是非常繁琐的,所以作者就直接使用Java写了一个图片转换PDF的系统,现在将该系统分享在这里

引入依赖

org.springframework.bootspring-boot-starter-parent2.0.4.RELEASEOrg.springframework.bootspring-boot-starter-webcom.itextpdfitextpdf5.4.2

前端页面

图片转换Pdf

图片转换pdf工具

控制层接口

package com.hrp.controller; import com.hrp.util.PdfUtils; import org.springframework.stereotype.Controller; 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.multipart.multipartfile; import javax.servlet.http.HttpServletResponse; /** * @description: 用于处理Pdf相关的请求 */ @Controller @RequestMapping("pdf") public class PdfController { @PostMapping("image/to") public void imagetoPdf(@RequestParam("file") multipartfile file,HttpServletResponse response) throws Exception{ PdfUtils.imagetoPdf(file,response); } }

PDF工具类

package com.hrp.util; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfWriter; import org.springframework.stereotype.Component; import org.springframework.web.multipart.multipartfile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; /** * @description: pdf相关的工具类 */ @Component public class PdfUtils { /** * 图片转换PDF的公共接口 * * @param file SpringMVC获取图片文件 * @param response HttpServletResponse * @throws IOException IO异常 * @throws DocumentException PDF文档异常 */ public static void imagetoPdf(multipartfile file, HttpServletResponse response) throws IOException, DocumentException { File pdfFile = generatePdfFile(file); downloadPdfFile(pdfFile, response); } /** * 将图片转换为PDF文件 * * @param file SpringMVC获取图片文件 * @return PDF文件 * @throws IOException IO异常 * @throws DocumentException PDF文档异常 */ private static File generatePdfFile(multipartfile file) throws IOException, DocumentException { String fileName = file.getoriginalFilename(); String pdfFileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf"; Document doc = new Document(PageSize.A4, 20, 20, 20, 20); PdfWriter.getInstance(doc, new FileOutputStream(pdfFileName)); doc.open(); doc.newPage(); Image image = Image.getInstance(file.getBytes()); float height = image.getHeight(); float width = image.getWidth(); int percent = getPercent(height, width); image.setAlignment(Image.MIDDLE); image.scalePercent(percent); doc.add(image); doc.close(); File pdfFile = new File(pdfFileName); return pdfFile; } /** * * 用于下载PDF文件 * * @param pdfFile PDF文件 * @param response HttpServletResponse * @throws IOException IO异常 */ private static void downloadPdfFile(File pdfFile, HttpServletResponse response) throws IOException { FileInputStream fis = new FileInputStream(pdfFile); byte[] bytes = new byte[fis.available()]; fis.read(bytes); fis.close(); response.reset(); response.setHeader("Content-Type", "application/pdf"); response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(pdfFile.getName(), "UTF-8")); OutputStream out = response.getoutputStream(); out.write(bytes); out.flush(); out.close(); } /** * 等比压缩,获取压缩百分比 * * @param height 图片的高度 * @param weight 图片的宽度 * @return 压缩百分比 */ private static int getPercent(float height, float weight) { float percent = 0.0F; if (height > weight) { percent = PageSize.A4.getHeight() / height * 100; } else { percent = PageSize.A4.getWidth() / weight * 100; } return Math.round(percent); } }

页面效果

这就是系统启动之后的页面效果,虽然页面比较简陋,但是功能却没有任何折扣,有兴趣或者有需要的同学可以自己搭建一下,试一试图片转换PDF文件效果

注意:作者自己测试了一下,普通图片基本是没有问题的,但是遇到一些特殊的图片可能会出现异常,毕竟只是一个比较简单的图片转换PDF系统,难以兼容所有图片

到此这篇关于Java实现图片转换PDF文件的示例代码文章就介绍到这了,更多相关Java 图片转换PDF内容搜索编程之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程之家!

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...