mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
--yum update
yum install libreoffice -y
yum install -y fontconfig mkfontscale
fc-list
fc-list :lang=zh
接下来我们先在字体文件夹下创建一个文件夹,将需要的字体文件放入进去
mkdir -p /usr/share/fonts/win_fonts
cd /usr/share/fonts/win_fonts
然后建立字体索引信息,更新字体缓存 进入目录 cd /usr/share/fonts/win_fonts,执行索引字体生成
mkfontscale && mkfontdir && fc-cache -fv
查看黑体常规字体是否安装成功
fc-list :lang=zh
public static void convertWordToPdf(String wordFilePath, String pdfFilePath) {
File inputFile = new File(wordFilePath);
File outputFile = new File(pdfFilePath);
LocalOfficeManager officeManager = LocalOfficeManager.install();
try {
officeManager.start();
DocumentConverter converter = LocalConverter.builder().officeManager(officeManager).build();
converter.convert(inputFile).as(DefaultDocumentFormatRegistry.DOCX).to(outputFile).as(DefaultDocumentFormatRegistry.PDF).execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
OfficeUtils.stopQuietly(officeManager);
}
}
linux mvn依赖
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.4.2</version>
</dependency>
window下
mvn
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.1.12</version>
</dependency>
// 待转换的word文件File wordFile = new File("/home/document/word/11.word");// 转换后的pdf文件File pdfFile = new File("/home/document/pdf/11.pdf)";// 获取当前系统名称String osName = System.getProperty("os.name").toLowerCase();// 根据系统选择执行方法
if (osName.contains("win")) {
PdfUtil.winWordToPdf(pdfFile, wordFile);
} else if (osName.contains("nux") || osName.contains("nix")) {
PdfUtil.linuxWordToPdf(pdfFile, wordFile);
}
public class PdfUtil {
private static final Logger log = LoggerFactory.getLogger(PdfUtil.class);
/**
* windows系统word转pdf
* @param pdfFile 转换后的pdf文件
* @param wordFile word源文件
*/
public static void winWordToPdf(File pdfFile, File wordFile) {
try {
IConverter converter = LocalConverter.builder().build();
converter.convert(new FileInputStream(wordFile))
.as(DocumentType.DOCX)
.to(new FileOutputStream(pdfFile))
.as(DocumentType.PDF).execute();
} catch (FileNotFoundException e) {
log.erorr("word转换pdf失败", e);
}
}
/**
* linux系统word转pdf * 使用LibreOffice转换。系统需安装LibreOffice * 转换命令 libreoffice --invisible --convert-to pdf --outdir output_dir source_path * 转换后的pdf文件名使用的是源文件的名称,所以如果要指定输出文件名称,就需把源文件名称改成想要输出的名称
* @param pdfFile 转换后的pdf文件
* @param wordFile word源文件
*/
public static void linuxWordToPdf(File pdfFile, File wordFile) {
// 获取word文件的绝对路径
String sourcePath = wordFile.getAbsolutePath(); // 获取pdf文件存放文件夹的绝对路径
String outDir = pdfFile.getAbsolutePath().substring(0, pdfFile.getAbsolutePath().lastIndexOf(File.separator));
// 构建LibreOffice的命令行工具命令
String command = "libreoffice --invisible --convert-to pdf --outdir " + outDir + " " + sourcePath;
log.info(command);
// 执行转换命令
try {
executeLinuxCmd(command);
} catch (Exception e) {
log.error("linuxWordToPdf linux环境word转换为pdf时出现异常:", e);
}
}
/**
* 执行命令行
*
* @param cmd 命令行
* @return
*/
private static boolean executeLinuxCmd(String cmd) {
try {
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
} catch (InterruptedException e) {
log.error("executeLinuxCmd 执行Linux命令异常:", e);
Thread.currentThread().interrupt();
return false;
} catch (IOException e) {
log.error("获取系统命令执行环境异常", e);
}
return true;
}
}

1793

被折叠的 条评论
为什么被折叠?



