springboot各种格式转pdf的实例代码
程序员文章站
2022-03-28 17:43:14
添加依赖 com.documents4j ...
添加依赖
<!--转pdf--> <dependency> <groupid>com.documents4j</groupid> <artifactid>documents4j-local</artifactid> <version>1.0.3</version> </dependency> <dependency> <groupid>com.documents4j</groupid> <artifactid>documents4j-transformer-msoffice-word</artifactid> <version>1.0.3</version> </dependency> <dependency> <groupid>com.itextpdf</groupid> <artifactid>itextpdf</artifactid> <version>5.5.10</version> </dependency>
测试方法
package com.ruoyi.mlogin.util; import com.documents4j.api.documenttype; import com.documents4j.api.iconverter; import com.documents4j.job.localconverter; import com.itextpdf.text.*; import com.itextpdf.text.pdf.pdfwriter; import java.io.*; import java.net.malformedurlexception; /** * @author cai * @version 1.0 * @date 2021/1/4 14:58 */ public class topdf { /** * 转pdf doc docx xls xlsx * @param path */ public void doctopdf(string path) { file inputword = new file("c:\\users\\29934\\documents\\tencent files\\2993481541\\filerecv\\1111.docx"); file outputfile = new file("c:\\users\\29934\\documents\\tencent files\\2993481541\\filerecv\\1111.pdf"); try { inputstream docxinputstream = new fileinputstream(inputword); outputstream outputstream = new fileoutputstream(outputfile); iconverter converter = localconverter.builder().build(); string filetyle=path.substring(path.lastindexof("."),path.length());//获取文件类型 if(".docx".equals(filetyle)){ converter.convert(docxinputstream).as(documenttype.docx).to(outputstream).as(documenttype.pdf).execute(); }else if(".doc".equals(filetyle)){ converter.convert(docxinputstream).as(documenttype.doc).to(outputstream).as(documenttype.pdf).execute(); }else if(".xls".equals(filetyle)){ converter.convert(docxinputstream).as(documenttype.xls).to(outputstream).as(documenttype.pdf).execute(); }else if(".xlsx".equals(filetyle)){ converter.convert(docxinputstream).as(documenttype.xlsx).to(outputstream).as(documenttype.pdf).execute(); } outputstream.close(); system.out.println("pdf转换成功"); } catch (exception e) { e.printstacktrace(); } } /** * * 生成pdf文件 * 需要转换的图片路径的数组 */ public static void main(string[] args) { try { string imagespath = "c:\\users\\29934\\documents\\tencent files\\2993481541\\filerecv\\1111.jpg"; file file = new file("c:\\users\\29934\\documents\\tencent files\\2993481541\\filerecv\\1111.pdf"); // 第一步:创建一个document对象。 document document = new document(); document.setmargins(0, 0, 0, 0); // 第二步: // 创建一个pdfwriter实例, pdfwriter.getinstance(document, new fileoutputstream(file)); // 第三步:打开文档。 document.open(); // 第四步:在文档中增加图片。 if (true) { image img = image.getinstance(imagespath); img.setalignment(image.align_center); // 根据图片大小设置页面,一定要先设置页面,再newpage(),否则无效 document.setpagesize(new rectangle(img.getwidth(), img.getheight())); document.newpage(); document.add(img); //下面是对应一个文件夹的图片 // file files = new file(imagespath); // string[] images = files.list(); // int len = images.length; // // for (int i = 0; i < len; i++) // { // if (images[i].tolowercase().endswith(".bmp") // || images[i].tolowercase().endswith(".jpg") // || images[i].tolowercase().endswith(".jpeg") // || images[i].tolowercase().endswith(".gif") // || images[i].tolowercase().endswith(".png")) { // string temp = imagespath + "\\" + images[i]; // image img = image.getinstance(temp); // img.setalignment(image.align_center); // // 根据图片大小设置页面,一定要先设置页面,再newpage(),否则无效 // document.setpagesize(new rectangle(img.getwidth(), img.getheight())); // document.newpage(); // document.add(img); // } // } // 第五步:关闭文档。 document.close(); } } catch (filenotfoundexception e) { e.printstacktrace(); } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (badelementexception e) { e.printstacktrace(); } catch (documentexception e) { e.printstacktrace(); } } }
补充:下面看下springboot:扩展类型转换器
需求:提交一个字符串到后端的java.sql.time类型,就报错了:
failed to convert property value of type [java.lang.string] to required type [java.sql.time]
正常提交到java.util.date类型是没有问题的。
所以这里就需要扩展内置的springmvc的转换器
代码如下:
webconfig : 添加新的类型转换器
import javax.annotation.postconstruct; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.configuration; import org.springframework.core.convert.support.genericconversionservice; import org.springframework.web.bind.support.configurablewebbindinginitializer; import org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter; import com.csget.web.converter.stringtotimeconverter; @configuration public class webconfig { @autowired private requestmappinghandleradapter requestmappinghandleradapter; @postconstruct public void addconversionconfig() { configurablewebbindinginitializer initializer = (configurablewebbindinginitializer) requestmappinghandleradapter .getwebbindinginitializer(); if (initializer.getconversionservice() != null) { genericconversionservice genericconversionservice = (genericconversionservice) initializer.getconversionservice(); genericconversionservice.addconverter(new stringtotimeconverter()); } } }
stringtotimeconverter :类型转换器的具体实现
import java.sql.time; import java.text.simpledateformat; import java.util.date; import org.apache.commons.lang3.stringutils; import org.springframework.core.convert.converter.converter; public class stringtotimeconverter implements converter<string, time> { public time convert(string value) { time time = null; if (stringutils.isnotblank(value)) { string strformat = "hh:mm"; int intmatches = stringutils.countmatches(value, ":"); if (intmatches == 2) { strformat = "hh:mm:ss"; } simpledateformat format = new simpledateformat(strformat); date date = null; try { date = format.parse(value); } catch (exception e) { e.printstacktrace(); } time = new time(date.gettime()); } return time; } }
到此这篇关于springboot各种格式转pdf的文章就介绍到这了,更多相关springboot格式转pdf内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 插件化开发实战,手游SDK使用插件化
下一篇: AU中设置轻微刻录吉他套件效果的教程