aspose win/linux WORD转PDF(及其解决乱码方式)

  • 1.工具类
  • 2.控制台
  • 3.解决乱码
  • 4.JAR包

之前自己用的docm4j 本地进行转换是ok 在服务器中就异常了; 后来在网上查询之后 do4j无法支持liunx系统;

1.工具类

package com.aostar.ida.framework.util.excel;import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;import java.io.*;public class WordPdfUtil {/*** The constant LOG.**/private final static Logger LOGGER = Logger.getLogger(ExcelPdToWord.class);/*** 获取license** @return*/private static boolean getLicense() {boolean result = false;try {// 凭证String licenseStr ="<License>\n" +"  <Data>\n" +"    <Products>\n" +"      <Product>Aspose.Total for Java</Product>\n" +"      <Product>Aspose.Words for Java</Product>\n" +"    </Products>\n" +"    <EditionType>Enterprise</EditionType>\n" +"    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +"    <LicenseExpiry>20991231</LicenseExpiry>\n" +"    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +"  </Data>\n" +"  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +"</License>";InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));License asposeLic = new License();asposeLic.setLicense(license);result = true;} catch (Exception e) {LOGGER.error("error:", e);}return result;}/*** Word 2 pdf.*  windos 测试* @param pdfFilePath   the pdf file path*/public static void word2Pdf(String pdfFilePath,String wordFilePath) {FileOutputStream fileOS = null;// 验证Licenseif (!getLicense()) {LOGGER.error("验证License失败!");return;}File inputWord = new File(wordFilePath);try {Document doc = new Document(new FileInputStream(inputWord));fileOS = new FileOutputStream(new File(pdfFilePath));// 保存转换的pdf文件doc.save(fileOS, SaveFormat.PDF);} catch (Exception e) {LOGGER.error("error:", e);} finally {try {if(fileOS != null){fileOS.close();}} catch (IOException e) {LOGGER.error("error:", e);}}}/*** Word 2 pdf.* liunx * @param pdfFilePath   the pdf file path*/public static void word3Pdf(String pdfFilePath,String wordFilePath) {FileOutputStream fileOS = null;// 验证Licenseif (!getLicense()) {LOGGER.error("验证License失败!");return;}File inputWord = new File(wordFilePath);try {//此处处理乱码和小方块//如果在本地运行,此处报错,请注释这个这是字体,主要是为了解决linux环境下面运行jar时找不到中文字体的问题//指定文件库内容路径FontSettings.getDefaultInstance().setFontsFolders(new String[] {"/usr/share/fonts", "/usr/share/fonts/chinese"}, true);Document doc = new Document(new FileInputStream(inputWord));fileOS = new FileOutputStream(new File(pdfFilePath));// 保存转换的pdf文件doc.save(fileOS, SaveFormat.PDF);} catch (Exception e) {LOGGER.error("error:", e);} finally {try {if(fileOS != null){fileOS.close();}} catch (IOException e) {LOGGER.error("error:", e);}}}//*-----------------------------------------------------------------/*** Word 2 pdf.** @param multipartFile the multipart file* @param pdfFilePath   the pdf file path*/public static void word3Pdf(MultipartFile multipartFile, String pdfFilePath) {FileOutputStream fileOS = null;// 验证Licenseif (!getLicense()) {LOGGER.error("验证License失败!");return;}try {Document doc = new Document(multipartFile.getInputStream());fileOS = new FileOutputStream(new File(pdfFilePath));// 保存转换的pdf文件doc.save(fileOS, SaveFormat.PDF);} catch (Exception e) {LOGGER.error("error:", e);} finally {try {if(fileOS != null){fileOS.close();}} catch (IOException e) {LOGGER.error("error:", e);}}}}

2.控制台

输入路径==>"  sourcePath
输出路径==>" + targetPath
WORD转pdf
//本地调用
WordPdfUtil.word2Pdf(targetPath, sourcePath);
//服务器调用(需要安装字体库,否则乱码)
WordPdfUtil.word3Pdf(targetPath, sourcePath);

3.解决乱码

解决方案1:
环境解决安装字库,将win机器的c:\windows\fonts目录下的全部文件拷贝到生产服务器字体安装目录下。

将window中字体解压拷贝放到linux中,上传至/usr/shared/fonts/chinese或者/usr/share/fonts目录下,上面的liunx代码已经指定路径;fonts/和fonts/chinese 我这边是没有的自己创建即可;2个地址写放一个即可;

解决方案2:
环境解决安装字库,将win机器的c:\windows\fonts目录下的全部文件拷贝到生产服务器字体安装目录下,然后执行以下命令更新字体缓存。(此步和上面一样)

查看linux目前的所有字体

fc-list

查看Linux目前的所有中文字体

fc-list :lang=zh

拷贝到linux下的字体目录

mkdir /usr/share/fonts/win
cp /local/src/fonts/* /usr/share/fonts/win

执行安装字体命令

cd /usr/share/fonts
sudo mkfontscale
sudo mkfontdir
sudo fc-cache -fv

执行命令让字体生效

source /etc/profile

如果安装失败,可以考虑修改字体权限

chmod 755 *.ttf

(此方法来源网上我这边并未尝试,我这边并无太高权限所以无法支撑此操作)

4.JAR包

     <dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>16.8.0</version></dependency>

https://download.csdn.net/download/qq_42055933/87269529
csdn的我这边是免费不知道他会不会收费(如收费大家需要可私信或者留言)

百度云:链接:https://pan.baidu.com/s/19clf3JPKMkr_O9uUFF8C0Q 密码:bbu0

记录下自己以后用的时候好抄;

aspose win/linux WORD转PDF(及其解决乱码方式)相关推荐

  1. Aspose.Java实现word转pdf,添加水印等操作

    Aspose.Java实现word转pdf,添加水印等操作 一. word转pdf 二. 文档插入水印 Aspose是一款商用版控件,支持各类文档操作,这里主要介绍如何在Springboot项目中使用 ...

  2. 使用Aspose组件将WORD、PDF、PPT转为图片

    using System; using System.Collections.Generic; using System.Text; using OMCS.Engine.WhiteBoard; usi ...

  3. aspose-words 解决Linux word转pdf 乱码和出现小方块问题

    Windows正常转换,在linux中转换就出现小方块 原因分析: 在window下没有问题但是在linux下有问题,就说明不是代码或者输入输出流编码的问题,根本原因是两个平台环境的问题.出现乱码说明 ...

  4. 在线查看word,excel,pdf文件解决

    眼看着项目要结束,提出新的需求(-_-!!习惯成自然了)需要支持在线查看word,excel,pdf文件,网页中嵌套word的时候,不让word文档占据整个网页,页面内还要有审批等功能,第一感觉想到控 ...

  5. JAVA 使用 com.aspose.words将word转换PDF等

    因为公司前端需要在线查看word和PDF,后台上传需求将word等文件转换为PDF,原本使用的是liboffice进行转换,后来部署到服务器端之后,发现并不是很适合,由此找到com.aspose.wo ...

  6. 怎么把word插入pdf?分享一个方式

    怎么把word插入pdf?PDF文件相信大家都很熟悉,平时工作学习中都会接触到这一格式.因为其良好的稳定性我们很多时候都会用这种格式来发文件,但也正因如此,当需要对其内容进行编辑时,往往就会出现麻烦. ...

  7. 使用Aspose txt转Word或PDF乱码问题

    出现乱码问题有两种可能 1. 系统无中文字体(通常发生在Linux系统下,自行百度Linux如何安装中文字体) 2. TXT的byte流没有选择合适的编码,不能写死为UTF-8,需要根据实际文本的编码 ...

  8. java根据word模板导出_java根据模板生成,导出word和pdf(aspose.words实现word转换pdf)...

    相关一部分java文件和jar包 pom文件 com.aspose aspose-words 18.2 word模板 其中的内容要在设置在表格里面 js导出方法 functionf_export(){ ...

  9. linux word 转 pdf 上类似百度文库开发研究与实战

    缘起 由于项目需要开发了类似百度文库和DOCIN类似的Flash播放器读取上传文档的系统,虽然最终技术问题都得以解决,但开发的过程中走了不少弯路,浪费了不少时间,特别是FlexPaper去掉自带的Lo ...

最新文章

  1. 刻意练习:LeetCode实战 -- Task30.通配符匹配
  2. Codeforces Global Round 14 E. Phoenix and Computers 思维 + dp
  3. 安卓逆向_24 ( 二 ) --- frida 学习记录
  4. 雷神开机logo更改_黑武士再度来袭 雷神第三代911黑武士游戏台式机评测
  5. 拳王寻你项目公社:普通人怎么创业,普通人的创业法宝,容易上手的兼职副业项目
  6. 后端研发菜鸟成长记 第一章 入门 之 存活下来
  7. 微信公众号接入和获取用户信息
  8. incsgo 可直接立刻取回皮肤的CSGO饰品皮肤开箱网站
  9. Flutter Scaffold的详细解说
  10. 如何实现Shell脚本开机自运行
  11. 舌苔发白是什么原因造成的?
  12. 【java实现二维码的生成(源码)】
  13. mapbox-gl提升建筑渐变效果(视频)
  14. 测量地球半径的古希腊方法
  15. 微信登录设备android22,安卓微信 7.0.22 内测版发布,支持手机和平板同时登录
  16. 数字图像处理(修正的阿尔法均值滤波)
  17. Numpy百题斩(一)
  18. zookeeper krb5认证时报 (Mechanism level: Clock skew too great (37) - PROCESS_TGS)])
  19. 【DSP开发】德州仪器达芬奇五年之路七宗罪,嵌入式处理器架构之争决战2012
  20. 【译文】利用Palette为android应用着色

热门文章

  1. 第三方百度网盘下载工具
  2. 实时音频编解码之八 频带扩展
  3. T5-small的encoder,decoder模型结构
  4. 进程的休眠与唤醒(等待队列)
  5. 企业该如何选择积分兑换商城系统
  6. windows7激活文件备份
  7. 【100%通过率】华为OD机试真题 Java 实现【最小调整顺序次数】【2022.11 Q4 新题】
  8. Python学习笔记---------廖雪峰(基础和函数)
  9. GPUImage--美颜滤镜GPUImageBeautifyFilter
  10. 2022第二届网刃杯网络安全大赛-ICS