目錄
轉(zhuǎn)PDF
一、Linux安裝libreoffice
二、Java代碼實(shí)現(xiàn)
這里介紹的是在linux環(huán)境(windows環(huán)境類(lèi)似)下實(shí)現(xiàn)講word、Excel、ppt、txt以及png圖片轉(zhuǎn)換為PDF文件后實(shí)現(xiàn)的預(yù)覽。由于需要轉(zhuǎn)換為PDF文件,當(dāng)Excel表格太大太寬的時(shí)候,可能出現(xiàn)換頁(yè)等格式被破壞的情況。
一、Linux安裝libreoffice
從官網(wǎng)下載對(duì)應(yīng)版本的libreoffice
下載地址:https://www.libreoffice.org/download/download/
下載完成后,把包上傳到服務(wù)器。
1、安裝步驟網(wǎng)上很多,大致如下:
(1)、解壓文件
tar -zxvf LibreOffice_7.1.0.2_Linux_x86-64_rpm.tar.gz
(2)、進(jìn)入文件RPMS目錄下
cd /opt/libreoffice7.1/LibreOffice_7.1.0.2_Linux_x86-64_rpm/RPMS
(3)、安裝rpm文件
執(zhí)行命令:yum localinstall *.rpm (或rpm -Uivh *.rpm --nodeps)。默認(rèn)安裝到opt/下
2、安裝后一般Linux環(huán)境可能缺乏一些依賴(lài),從而導(dǎo)致libreoffice會(huì)啟動(dòng)失敗的情況。
(1)
Caused by: org.jodconverter.office.OfficeException: A process with acceptString 'socket,host=127.0.0.1,port=8100,tcpNoDelay=1;urp;StarOffice.ServiceManager' started but its pid could not be found
報(bào)錯(cuò)的原因是因?yàn)轫?xiàng)目目錄出現(xiàn)中文?。?!
(2)
/opt/libreoffice7.0/program/soffice.bin: error while loading shared libraries: libSM.so.6: cannot open shared object file: No such file or directory
sudo yum install libSM
sudo yum install libICE
sudo yum install libX11-xcb
(3)
error while loading shared libraries: libXext.so.6: cannot open shared object file: No such file or directory.
sudo?yum install libXext.x86_64
(4)
/opt/libreoffice5.3/program/oosplash: error while loading shared libraries: libXinerama.so.1: cannot open shared object file: No such file or directory
sudo yum isntall -y libXinerama
(5)
/opt/libreoffice5.3/program/soffice.bin: error while loading shared libraries: libcairo.so.2: cannot open shared object file: No such file or directory
sudo yum install -y ibus
3、PDF文件預(yù)覽中文出現(xiàn)亂碼
[app@centos76-m-pod4-03-i-6 RPMS]$ libreoffice7.4 --headless --invisible --convert-to pdf /tmp/test.doc --outdir /tmp/
javaldx: Could not find a Java Runtime Environment!
Warning: failed to read path from javaldx
convert /tmp/test.doc -> /tmp/test.pdf using filter : writer_pdf_Export
安裝完成后,按如上執(zhí)行如上命令進(jìn)行轉(zhuǎn)換,得到的PDF文檔如下:
?
出現(xiàn)亂碼的問(wèn)題一般是libreoffice不支持中文字體,可以將windows下(C:\Windows\Fonts目錄)的字體庫(kù)打包上傳到libreoffice目錄下
/opt/libreoffice5.3/share/fonts/truetype
fc-list #查看字體列表
mkfontscale #建立字體緩存
mkfontdir
fc-cache -fv #刷新緩存
存放字體的目錄放在/usr/share/fonts下面
可以在這個(gè)目錄下創(chuàng)建一個(gè)目錄,在目錄里放你的字體,記住目錄權(quán)限是755,字體權(quán)限是644,完事刷新一下,不行重啟。
執(zhí)行以下命令即可:
另外將字體copy到目錄下?/usr/share/fonts
sudo mkfontscale
sudo mkfontdir
#-f強(qiáng)制掃描,-v過(guò)程
sudo fc-cache ?-fv ?
如果提示mkfontscale命令未找到,則安裝
sudo yum install mkfontscale
二、Java代碼實(shí)現(xiàn)
1、引入maven依賴(lài)
<!--jodconverter 核心包 -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.4.6</version>
</dependency>
<!--springboot支持包,里面包括了自動(dòng)配置類(lèi) -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.4.6</version>
</dependency>
<!--jodconverter 本地支持包 -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local-lo</artifactId>
<version>4.4.6</version>
</dependency>
LibreOffice 和OpenOffice依賴(lài)的包不一樣,詳見(jiàn)GitHub - jodconverter/jodconverter: JODConverter automates document conversions using LibreOffice or Apache OpenOffice.
With LibreOffice libraries:
Gradle:
implementation 'org.jodconverter:jodconverter-local-lo:4.4.6'
Maven:
<dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local-lo</artifactId> <version>4.4.6</version> </dependency>
With OpenOffice libraries:
Gradle:
implementation 'org.jodconverter:jodconverter-local:4.4.6'
or
implementation 'org.jodconverter:jodconverter-local-oo:4.4.6'
Maven:
<dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local</artifactId> <version>4.4.6</version> </dependency>
2、以@Bean方式添加配置DocumentConverter(也可以用yml添加)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-482006.html
@Configuration
public class JodConverterConfig {
private final String linuxOfficeHome = "/opt/libreoffice7.4";
private final String windowsOfficeHome = "E:\\software\\LibreOffice";
@Bean(
initMethod = "start",
destroyMethod = "stop"
)
public OfficeManager officeManager(){
String os = System.getProperty("os.name").toLowerCase();
return LocalOfficeManager.builder()
.officeHome(os.contains("windows") ? windowsOfficeHome : linuxOfficeHome)
.portNumbers(9080,9081)
.maxTasksPerProcess(100)
.build();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnBean({OfficeManager.class})
public DocumentConverter jodConverter(OfficeManager officeManager) {
return LocalConverter.make(officeManager);
}
}
3、自動(dòng)裝配轉(zhuǎn)換類(lèi)DocumentConverter并實(shí)現(xiàn)轉(zhuǎn)換文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-482006.html
@Resource(name = "jodConverter")
private DocumentConverter jodConverter;
---------其他代碼---------
// 轉(zhuǎn)為PDF(輸入為InputStream,然后向前端輸出OutputStream文件流-----看自己的具體場(chǎng)景進(jìn)行改動(dòng))
jodConverter.convert(response.body().asInputStream()).to(httpResponse.getOutputStream())
.as(DefaultDocumentFormatRegistry.PDF).execute();
到了這里,關(guān)于Linux環(huán)境Libreoffice實(shí)現(xiàn)Word、Excel等在線(xiàn)預(yù)覽的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!