vue 做企业网站行不,seo深度优化公司,企业宣传推广方案,什么网站用来做商城好我这边是因为业务需要将之前导出的word文档转换为PDF文件#xff0c;然后页面预览下载这样的情况。之前导出word文档又不是我做的#xff0c;所以为了不影响业务#xff0c;只是将最后在输出流时转换成了PDF#xff0c;当时本地调用没什么问题#xff0c;一切正常#xf…我这边是因为业务需要将之前导出的word文档转换为PDF文件然后页面预览下载这样的情况。之前导出word文档又不是我做的所以为了不影响业务只是将最后在输出流时转换成了PDF当时本地调用没什么问题一切正常后面发布测试环境使用时才发现导出时PDF文件内容乱码了中文没有一个显示的。 这里记录下当时遇到的问题和解决方式
1解决中文不显示乱码处理情况
我这里是使用的POI进行的转换直接将word转换成PDF转换方式放在后面。 当时转换后的PDF长这样 正常格式下是有很多中文说明的。下面就是处理方式 当时就想到了是服务器上不支持中文所以百度了一圈果然是然后就开始加中文字体 Linux 服务器上字体目录是在/user/share/fonts 下的 1在/user/share/fonts 下创建自己的文件夹字体我这里是my-fonts 2找到Windows中的字体将字体上传到这个 my-fonts中 这里面有很多字体我们需要的是中文字体可以选择性上传选择需要的中文字体上传比如宋体要和你文件模板中字体一致就行。
3安装 接着根据当前目录下的字体建立scale文件 mkfontscale 接着建立dir文件 mkfontdir 然后运行 fc-cache fc-list #查看字体列表
4赋予权限 chmod 777/usr/share/fonts/my-fonts chmod 755 /usr/share/fonts/my-fonts/*
使用命令查看 fc-list :langzh
2Word转PDF实现的几种方式
1使用POI的方式将word转换为PDF 引入依赖
dependencygroupIdfr.opensagres.xdocreport/groupIdartifactIdfr.opensagres.poi.xwpf.converter.pdf-gae/artifactIdversion2.0.1/version
/dependency在关闭流之前添加并修改reponse中.docx为.pdf
response.setHeader(Content-Disposition, attachment; filename java.net.URLEncoder.encode(日报-datetime.pdf, UTF-8));
//转为PDF
PdfOptions options PdfOptions.create();
PdfConverter.getInstance().convert(document, outStream, options);
//下面再是转word里面最后的代码关闭流2使用aspose.words的Document方式将word转换为PDF 1下载jar包jar包下载 2将jar包放入项目中resources目录下的lib文件夹中 3将jar包转为library 转换后就会出现上面图中箭头处的样子可以打开。
4引入jar包依赖
dependencygroupIdcom.aspose.words/groupIdartifactIdaspose-words/artifactIdversion15.8.0/versionscopesystem/scopesystemPath${project.basedir}/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar/systemPath
/dependency在打包的依赖中添加 pluginconfigurationincludeSystemScopetrue/includeSystemScope/configuration/plugin5转换
String s LicenseDataProductsProductAspose.Total for Java/ProductProductAspose.Words for Java/Product/ProductsEditionTypeEnterprise/EditionTypeSubscriptionExpiry20991231/SubscriptionExpiryLicenseExpiry20991231/LicenseExpirySerialNumber8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7/SerialNumber/DataSignaturesNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppod0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU/Signature/License;//去除水印ByteArrayInputStream is new ByteArrayInputStream(s.getBytes());License license new License();license.setLicense(is);//将XWPFDocument转换为InputStreamByteArrayOutputStream b new ByteArrayOutputStream();//这里的documentXWPFDocument document在下面的word转换中document.write(b);InputStream inputStream new ByteArrayInputStream(b.toByteArray());//这里的Document 的引入是//import com.aspose.words.Document;//import com.aspose.words.License;//import com.aspose.words.SaveFormat;Document doc new Document(inputStream);doc.save(outStream, SaveFormat.PDF);b.close();inputStream.close();//下面再是转word里面最后的代码关闭流3使用documents4j 的方式将word转换为PDF
1引入依赖 !-- word 转 pdf 通过documents4j实现 --dependencygroupIdcom.documents4j/groupIdartifactIddocuments4j-local/artifactIdversion1.0.3/version/dependencydependencygroupIdcom.documents4j/groupIdartifactIddocuments4j-transformer-msoffice-word/artifactIdversion1.0.3/version/dependency2转换如下 //将XWPFDocument转换为InputStreamByteArrayOutputStream b new ByteArrayOutputStream();//这里的documentXWPFDocument document在下面的word转换中document.write(b);InputStream docxInputStream new ByteArrayInputStream(b.toByteArray());//下面的引入类为//import com.documents4j.api.DocumentType;//import com.documents4j.api.IConverter;//import com.documents4j.job.LocalConverter;IConverter converter LocalConverter.builder().build();boolean execute converter.convert(docxInputStream).as(DocumentType.DOCX).to(outStream).as(DocumentType.PDF).schedule().get();b.close();docxInputStream.close();3这里之前转换word方式记录如下
1制作word模板将需要转换的数值写成了${变量名}。 2转换
//模板文件的地址
String filePath /usr/local/data/模板.docx;
//Map存储需要替换的值
MapString, Object map new HashMap();
map.put(${date}, date);
map.put(${datetime}, datetime);
//写入
try {// 替换的的关键字存放到Set集合中SetString set map.keySet();// 读取模板文档XWPFDocument document new XWPFDocument(new FileInputStream(filePath ));/*** 替换段落中的指定文字*/// 读取文档中的段落回车符为一个段落。// 同一个段落里面会被“:”等符号隔开为多个对象IteratorXWPFParagraph itPara document.getParagraphsIterator();while (itPara.hasNext()) {// 获取文档中当前的段落文字信息XWPFParagraph paragraph (XWPFParagraph) itPara.next();ListXWPFRun run paragraph.getRuns();// 遍历段落文字对象for (int i 0; i run.size(); i) {// 获取段落对象if (run.get(i) null) { //段落为空跳过continue;}String sectionItem run.get(i).getText(run.get(i).getTextPosition()); //段落内容//System.out.println(替换前 sectionItem);// 遍历自定义表单关键字替换Word文档中的内容IteratorString iterator set.iterator();while (iterator.hasNext()) {// 当前关键字String key iterator.next();// 替换内容sectionItem sectionItem.replace(key, String.valueOf(map.get(key)));}//System.out.println(sectionItem);run.get(i).setText(sectionItem, 0);}}/*** 替换表格中的指定文字*///获取文档中所有的表格每个表格是一个元素IteratorXWPFTable itTable document.getTablesIterator();while (itTable.hasNext()) {XWPFTable table (XWPFTable) itTable.next(); //获取表格内容int count table.getNumberOfRows(); //表格的行数//遍历表格行的对象for (int i 0; i count; i) {XWPFTableRow row table.getRow(i); //表格每行的内容ListXWPFTableCell cells row.getTableCells(); //每个单元格的内容//遍历表格的每行单元格对象for (int j 0; j cells.size(); j) {XWPFTableCell cell cells.get(j); //获取每个单元格的内容ListXWPFParagraph paragraphs cell.getParagraphs(); //获取单元格里所有的段落for (XWPFParagraph paragraph : paragraphs) {//获取段落的内容ListXWPFRun run paragraph.getRuns();// 遍历段落文字对象for (int o 0; o run.size(); o) {// 获取段落对象if (run.get(o) null || run.get(o).equals()) {continue;}String sectionItem run.get(o).getText(run.get(o).getTextPosition()); //获取段落内容if (sectionItem null || sectionItem.equals()) { //段落为空跳过continue;}//遍历自定义表单关键字替换Word文档中表格单元格的内容for (String key : map.keySet()) {// 替换内容sectionItem sectionItem.replace(key, String.valueOf(map.get(key)));run.get(o).setText(sectionItem, 0);}}}}}}SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd);String datetime sdf.format(new Date());response.setStatus(200);response.setHeader(Content-Disposition, attachment; filename java.net.URLEncoder.encode(模板-datetime.docx, UTF-8));response.setCharacterEncoding(utf8);OutputStream outStream response.getOutputStream();//这里将插入转换成PDF的代码outStream.close();document.close();} catch (Exception e) {e.printStackTrace();}上面就是别人之前业务场景中的转换word的代码。