Libreoffice实现office转pdf、html、jpg等格式数据

其实libreoffice有好多功能,完全可以替代office

1.windows下将word转为pdf

1  安装libreoffice

到官网下载后安装即可。https://donate.libreoffice.org/

安装完成后目录:

其实安装完我们发现其有好多功能,现在介绍几个重要的功能。

soffice.exe --- 类似于一个全收录功能,双击可以新建好多格式文本。

sweb.exe---类似于一个html的编辑器,可以编辑好多文件,可能与notpad++更像。

scalc.exe---类似于excel,对表格处理。

simpress.exe---类似于ppt

swriter.exe---类似于word,编辑文档(当然可以打开docx文档)

sbase.exe----对数据库进行操作,可以通过JDBC、ODBC连接数据库,没有可视化工具的时候可以用这个。

2.配置环境变量(为了我们能在任何情况下调用命令)

执行命令测试soffice

C:\Users\liqiang>
LibreOffice 6.0.6.2 0c292870b25a325b5ed35f6b45599d2ea4458e77Usage: soffice [argument...]argument - switches, switch parameters and document URIs (filenames).Using without special arguments:
Opens the start center, if it is used without any arguments.{file}              Tries to open the file (files) in the componentssuitable for them.{file} {macro:///Library.Module.MacroName}Opens the file and runs specified macros fromthe file.Getting help and information:--help | -h | -?    Shows this help and quits.--helpwriter        Opens built-in or online Help on Writer.--helpcalc          Opens built-in or online Help on Calc.--helpdraw          Opens built-in or online Help on Draw.--helpimpress       Opens built-in or online Help on Impress.--helpbase          Opens built-in or online Help on Base.--helpbasic         Opens built-in or online Help on Basic scriptinglanguage.--helpmath          Opens built-in or online Help on Math.--version           Shows the version and quits.--nstemporarydirectory(MacOS X sandbox only) Returns path of the temporarydirectory for the current user and exits. Overridesall other arguments.General arguments:--quickstart[=no]   Activates[Deactivates] the Quickstarter service.--nolockcheck       Disables check for remote instances using oneinstallation.--infilter={filter} Force an input filter type if possible. For example:--infilter="Calc Office Open XML"--infilter="Text (encoded):UTF8,LF,,,"--pidfile={file}    Store soffice.bin pid to {file}.--display {display} Sets the DISPLAY environment variable on UNIX-likeplatforms to the value {display} (only supported by astart script).User/programmatic interface control:--nologo            Disables the splash screen at program start.--minimized         Starts minimized. The splash screen is not displayed.--nodefault         Starts without displaying anything except the splashscreen (do not display initial window).--invisible         Starts in invisible mode. Neither the start-up logo northe initial program window will be visible. Applicationcan be controlled, and documents and dialogs can becontrolled and opened via the API. Using the parameter,the process can only be ended using the taskmanager(Windows) or the kill command (UNIX-like systems). Itcannot be used in conjunction with --quickstart.--headless          Starts in "headless mode" which allows using theapplication without GUI. This special mode can be usedwhen the application is controlled by external clientsvia the API.--norestore         Disables restart and file recovery after a system crash.--safe-mode         Starts in a safe mode, i.e. starts temporarily with afresh user profile and helps to restore a brokenconfiguration.--accept={UNO-URL}  Specifies an UNO-URL connect-string to create an UNOacceptor through which other programs can connect toaccess the API. UNO-URL is string the such kinduno:connection-type,params;protocol-name,params;ObjectName.--unaccept={UNO-URL} Closes an acceptor that was created with --accept. Use--unaccept=all to close all open acceptors.--language={lang}   Uses specified language, if language is not selectedyet for UI. The lang is a tag of the language in IETFlanguage tag.Developer arguments:--terminate_after_initExit after initialization complete (no documents loaded).--eventtesting      Exit after loading documents.New document creation arguments:
The arguments create an empty document of specified kind. Only one of them may
be used in one command line. If filenames are specified after an argument,
then it tries to open those files in the specified component.--writer            Creates an empty Writer document.--calc              Creates an empty Calc document.--draw              Creates an empty Draw document.--impress           Creates an empty Impress document.--base              Creates a new database.--global            Creates an empty Writer master (global) document.--math              Creates an empty Math document (formula).--web               Creates an empty HTML document.File open arguments:
The arguments define how following filenames are treated. New treatment begins
after the argument and ends at the next argument. The default treatment is to
open documents for editing, and create new documents from document templates.-n                  Treats following files as templates for creation of newdocuments.-o                  Opens following files for editing, regardless whetherthey are templates or not.--pt {Printername}  Prints following files to the printer {Printername},after which those files are closed. The splash screendoes not appear. If used multiple times, only last{Printername} is effective for all documents of all--pt runs. Also, --printer-name argument of--print-to-file switch interferes with {Printername}.-p                  Prints following files to the default printer, afterwhich those files are closed. The splash screen doesnot appear. If the file name contains spaces, then itmust be enclosed in quotation marks.--view              Opens following files in viewer mode (read-only).--show              Opens and starts the following presentation documentsof each immediately. Files are closed after the showing.Files other than Impress documents are opened indefault mode , regardless of previous mode.--convert-to OutputFileExtension[:OutputFilterName][--outdir output_dir] [--convert-images-to]Batch convert files (implies --headless). If --outdirisn't specified, then current working directory is usedas output_dir. If --convert-images-to is given, itsparameter is taken as the target MIME format for *all*images written to the output format. If --convert-to isused more than once, the last value of OutputFileExtension[:OutputFilterName] is effective. If --outdir is used morethan once, only its last value is effective. For example:--convert-to pdf *.odt--convert-to epub *.doc--convert-to pdf:writer_pdf_Export --outdir /home/user *.doc--convert-to "html:XHTML Writer File:UTF8" *.doc--convert-to "txt:Text (encoded):UTF8" *.doc--print-to-file [--printer-name printer_name] [--outdir output_dir]Batch print files to file. If --outdir is not specified,then current working directory is used as output_dir.If --printer-name or --outdir used multiple times, onlylast value of each is effective. Also, {Printername} of--pt switch interferes with --printer-name.--cat               Dump text content of the following files to console(implies --headless). Cannot be used with --convert-to.--script-cat        Dump text content of any scripts embedded in the files to console(implies --headless). Cannot be used with --convert-to.-env:<VAR>[=<VALUE>] Set a bootstrap variable. For example: to seta non-default user profile path:-env:UserInstallation=file:///tmp/testIgnored switches:-psn                Ignored (MacOS X only).-Embedding          Ignored (COM+ related; Windows only).--nofirststartwizard Does nothing, accepted only for backward compatibility.--protector {arg1} {arg2}Used only in unit tests and should have two arguments.

4.命令行转换pdf

转换到当前目录:

liqiang@root MINGW64 ~/Desktop/新建文件夹 (3)
$ soffice --headless --convert-to pdf ./Java开发-太原科技大学-软件工程-乔利强.docx
convert C:\Users\liqiang\Desktop\▒½▒▒ļ▒▒▒ (3)\Java▒▒▒▒-̫ԭ▒Ƽ▒▒▒ѧ-▒▒▒▒▒▒▒-▒▒▒▒ǿ.docx -> C:\Users\liqiang\Desktop\▒½▒▒ļ▒▒▒ (3)\Java▒▒▒▒-̫ԭ▒Ƽ▒▒▒ѧ-▒▒▒▒▒▒▒-▒▒▒▒ǿ.pdf using filter : writer_pdf_Export
func=xmlSecCheckVersionExt:file=..\src\xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26liqiang@root MINGW64 ~/Desktop/新建文件夹 (3)
$ ls
Java开发-太原科技大学-软件工程-乔利强.docx
Java开发-太原科技大学-软件工程-乔利强.pdf

如果需要转换到指定目录可以加--outdir参数

5.java程序实现word转pdf(原理是通过cmd调用上述命令)

import java.io.IOException;
import java.io.InputStream;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public final class Test {private static final Logger logger = LoggerFactory.getLogger(Test.class);public static void main(String[] args) throws NullPointerException {long start = System.currentTimeMillis();String srcPath = "C:/Users/liqiang/Desktop/ww/tt.docx", desPath = "C:/Users/liqiang/Desktop/ww";String command = "";String osName = System.getProperty("os.name");if (osName.contains("Windows")) {command = "soffice --headless --convert-to pdf " + srcPath + " --outdir " + desPath;exec(command);}long end = System.currentTimeMillis();logger.debug("用时:{} ms", end - start);}public static boolean exec(String command) {Process process;// Process可以控制该子进程的执行或获取该子进程的信息try {logger.debug("exec cmd : {}", command);process = Runtime.getRuntime().exec(command);// exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。// 下面两个可以获取输入输出流InputStream errorStream = process.getErrorStream();InputStream inputStream = process.getInputStream();} catch (IOException e) {logger.error(" exec {} error", command, e);return false;}int exitStatus = 0;try {exitStatus = process.waitFor();// 等待子进程完成再往下执行,返回值是子线程执行完毕的返回值,返回0表示正常结束// 第二种接受返回值的方法int i = process.exitValue(); // 接收执行完毕的返回值logger.debug("i----" + i);} catch (InterruptedException e) {logger.error("InterruptedException  exec {}", command, e);return false;}if (exitStatus != 0) {logger.error("exec cmd exitStatus {}", exitStatus);} else {logger.debug("exec cmd exitStatus {}", exitStatus);}process.destroy(); // 销毁子进程process = null;return true;}}

另一种命令的方式为  cmd /c soffice ..... .

另外写的时候最好pdf后面跟上  :writer_pdf_Export,例如: --convert-to pdf:writer_pdf_Export  可能会在转换失败后调用过滤器重写。

 结果:

2018-10-25 21:56:35 [Test]-[DEBUG] exec cmd : soffice --headless --convert-to pdf C:/Users/liqiang/Desktop/ww/tt.docx --outdir C:/Users/liqiang/Desktop/ww
2018-10-25 21:56:45 [Test]-[DEBUG] i----0
2018-10-25 21:56:45 [Test]-[DEBUG] exec cmd exitStatus 0
2018-10-25 21:56:45 [Test]-[DEBUG] 用时:9980 ms

2.linux实现将word转为pdf,以centos为例

1.linux下安装libreoffice

1.下载

  我们安装采用yum安装,首先下载rpm包。这里需要三个包。

wget http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.0.6/rpm/x86_64/LibreOffice_6.0.6_Linux_x86-64_rpm.tar.gz
wget http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.0.6/rpm/x86_64/LibreOffice_6.0.6_Linux_x86-64_rpm_sdk.tar.gz
wget http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.0.6/rpm/x86_64/LibreOffice_6.0.6_Linux_x86-64_rpm_langpack_zh-CN.tar.gz

  

  其实我们在windows下通过浏览器访问上面链接也是可以下载tar.gz包的,如果需要不同的版本只需要修改url上的版本号即可。比如我想下载6.0.3的我可以访问下面url:

http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.0.3/rpm/x86_64/LibreOffice_6.0.3_Linux_x86-64_rpm.tar.gz

  其实好多时候我们采用wget下载的时候如果下载不下来, 我们可以先在windows下访问url下载完只会传到linux服务器,这也是一种思路。

2.上传到服务器并解压

采用  tar -xvf xxxxxx.tar.gz解压即可。解压结果如下:

[root@VM_0_12_centos libreoffice]# ll
total 263748
drwxr-xr-x 4 root root      4096 Jul 28 06:07 LibreOffice_6.0.6.2_Linux_x86-64_rpm
drwxr-xr-x 3 root root      4096 Jul 28 07:32 LibreOffice_6.0.6.2_Linux_x86-64_rpm_langpack_zh-CN
drwxr-xr-x 3 root root      4096 Jul 28 06:27 LibreOffice_6.0.6.2_Linux_x86-64_rpm_sdk
-rw-r--r-- 1 root root    798421 Oct 25 12:13 LibreOffice_6.0.6_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
-rw-r--r-- 1 root root  36919386 Oct 25 12:24 LibreOffice_6.0.6_Linux_x86-64_rpm_sdk.tar.gz
-rw-r--r-- 1 root root 213845646 Oct 25 10:33 LibreOffice_6.0.6_Linux_x86-64_rpm.tar.gz

3.采用yum localinstall *.rpm安装rpm文件

[root@VM_0_12_centos RPMS]# pwd
/opt/libreoffice/LibreOffice_6.0.6.2_Linux_x86-64_rpm/RPMS
[root@VM_0_12_centos RPMS]# yum localinstall *.rpm

  RPMS下存放的是需要安装的rpm文件,进入该文件夹下采用通配符的方式安装即可。(三个tar.gz解压后的都需要安装)

4.测试libreoffice

[root@VM_0_12_centos RPMS]# libreoffice6.0 -help
Warning: -help is deprecated.  Use --help instead.
LibreOffice 6.0.6.2 0c292870b25a325b5ed35f6b45599d2ea4458e77Usage: soffice [argument...]argument - switches, switch parameters and document URIs (filenames).Using without special arguments:
Opens the start center, if it is used without any arguments.{file}              Tries to open the file (files) in the componentssuitable for them.{file} {macro:///Library.Module.MacroName}Opens the file and runs specified macros fromthe file.Getting help and information:--help | -h | -?    Shows this help and quits.--helpwriter        Opens built-in or online Help on Writer.--helpcalc          Opens built-in or online Help on Calc.--helpdraw          Opens built-in or online Help on Draw.--helpimpress       Opens built-in or online Help on Impress.--helpbase          Opens built-in or online Help on Base.--helpbasic         Opens built-in or online Help on Basic scriptinglanguage.--helpmath          Opens built-in or online Help on Math.--version           Shows the version and quits.--nstemporarydirectory(MacOS X sandbox only) Returns path of the temporarydirectory for the current user and exits. Overridesall other arguments.General arguments:--quickstart[=no]   Activates[Deactivates] the Quickstarter service.--nolockcheck       Disables check for remote instances using oneinstallation.--infilter={filter} Force an input filter type if possible. For example:--infilter="Calc Office Open XML"--infilter="Text (encoded):UTF8,LF,,,"--pidfile={file}    Store soffice.bin pid to {file}.--display {display} Sets the DISPLAY environment variable on UNIX-likeplatforms to the value {display} (only supported by astart script).User/programmatic interface control:--nologo            Disables the splash screen at program start.--minimized         Starts minimized. The splash screen is not displayed.--nodefault         Starts without displaying anything except the splashscreen (do not display initial window).--invisible         Starts in invisible mode. Neither the start-up logo northe initial program window will be visible. Applicationcan be controlled, and documents and dialogs can becontrolled and opened via the API. Using the parameter,the process can only be ended using the taskmanager(Windows) or the kill command (UNIX-like systems). Itcannot be used in conjunction with --quickstart.--headless          Starts in "headless mode" which allows using theapplication without GUI. This special mode can be usedwhen the application is controlled by external clientsvia the API.--norestore         Disables restart and file recovery after a system crash.--safe-mode         Starts in a safe mode, i.e. starts temporarily with afresh user profile and helps to restore a brokenconfiguration.--accept={UNO-URL}  Specifies an UNO-URL connect-string to create an UNOacceptor through which other programs can connect toaccess the API. UNO-URL is string the such kinduno:connection-type,params;protocol-name,params;ObjectName.--unaccept={UNO-URL} Closes an acceptor that was created with --accept. Use--unaccept=all to close all open acceptors.--language={lang}   Uses specified language, if language is not selectedyet for UI. The lang is a tag of the language in IETFlanguage tag.Developer arguments:--terminate_after_initExit after initialization complete (no documents loaded).--eventtesting      Exit after loading documents.New document creation arguments:
The arguments create an empty document of specified kind. Only one of them may
be used in one command line. If filenames are specified after an argument,
then it tries to open those files in the specified component.--writer            Creates an empty Writer document.--calc              Creates an empty Calc document.--draw              Creates an empty Draw document.--impress           Creates an empty Impress document.--base              Creates a new database.--global            Creates an empty Writer master (global) document.--math              Creates an empty Math document (formula).--web               Creates an empty HTML document.File open arguments:
The arguments define how following filenames are treated. New treatment begins
after the argument and ends at the next argument. The default treatment is to
open documents for editing, and create new documents from document templates.-n                  Treats following files as templates for creation of newdocuments.-o                  Opens following files for editing, regardless whetherthey are templates or not.--pt {Printername}  Prints following files to the printer {Printername},after which those files are closed. The splash screendoes not appear. If used multiple times, only last{Printername} is effective for all documents of all--pt runs. Also, --printer-name argument of--print-to-file switch interferes with {Printername}.-p                  Prints following files to the default printer, afterwhich those files are closed. The splash screen doesnot appear. If the file name contains spaces, then itmust be enclosed in quotation marks.--view              Opens following files in viewer mode (read-only).--show              Opens and starts the following presentation documentsof each immediately. Files are closed after the showing.Files other than Impress documents are opened indefault mode , regardless of previous mode.--convert-to OutputFileExtension[:OutputFilterName][--outdir output_dir] [--convert-images-to]Batch convert files (implies --headless). If --outdirisn't specified, then current working directory is usedas output_dir. If --convert-images-to is given, itsparameter is taken as the target MIME format for *all*images written to the output format. If --convert-to isused more than once, the last value of OutputFileExtension[:OutputFilterName] is effective. If --outdir is used morethan once, only its last value is effective. For example:--convert-to pdf *.odt--convert-to epub *.doc--convert-to pdf:writer_pdf_Export --outdir /home/user *.doc--convert-to "html:XHTML Writer File:UTF8" *.doc--convert-to "txt:Text (encoded):UTF8" *.doc--print-to-file [--printer-name printer_name] [--outdir output_dir]Batch print files to file. If --outdir is not specified,then current working directory is used as output_dir.If --printer-name or --outdir used multiple times, onlylast value of each is effective. Also, {Printername} of--pt switch interferes with --printer-name.--cat               Dump text content of the following files to console(implies --headless). Cannot be used with --convert-to.--script-cat        Dump text content of any scripts embedded in the files to console(implies --headless). Cannot be used with --convert-to.-env:<VAR>[=<VALUE>] Set a bootstrap variable. For example: to seta non-default user profile path:-env:UserInstallation=file:///tmp/testIgnored switches:-psn                Ignored (MacOS X only).-Embedding          Ignored (COM+ related; Windows only).--nofirststartwizard Does nothing, accepted only for backward compatibility.--protector {arg1} {arg2}Used only in unit tests and should have two arguments.

  安装后的命令是libreoffice6.0

5.为了使用libreoffice我们创建别名

[root@VM_0_12_centos ~]# alias libreoffice='libreoffice6.0'
[root@VM_0_12_centos ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias libreoffice='libreoffice6.0'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'

2.linux下面命令行测试word转pdf(其参数与windows下的参数大体相同)

[root@VM_0_12_centos tmpFile]# ls
tt.docx
[root@VM_0_12_centos tmpFile]# libreoffice6.0 --convert-to pdf:writer_pdf_Export ./tt.docx
func=xmlSecCheckVersionExt:file=xmlsec.c:line=188:obj=unknown:subj=unknown:error=19:invalid version:mode=abi compatible;expected minor version=2;real minor version=2;expected subminor version=25;real subminor version=26
convert /root/tmpFile/tt.docx -> /root/tmpFile/tt.pdf using filter : writer_pdf_Export
[root@VM_0_12_centos tmpFile]# ls
tt.docx  tt.pdf
[root@VM_0_12_centos tmpFile]#

  我们将上面生成的pdf传回windows下面查看发现中文乱码。

3.关于word转pdf中文乱码问题的解决办法

1.查看fonts目录

[root@VM_0_12_centos tmpFile]# cat /etc/fonts/fonts.conf | grep fon
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- /etc/fonts/fonts.conf file to configure system font access -->
<fontconfig>problems to the fontconfig bugzilla system located at fontconfig.orgNote that the normal 'make install' procedure for fontconfig is toreplace any existing fonts.conf file with the new version.  Place<dir>/usr/share/fonts</dir><dir>/usr/share/X11/fonts/Type1</dir> <dir>/usr/share/X11/fonts/TTF</dir> <dir>/usr/local/share/fonts</dir><dir prefix="xdg">fonts</dir><dir>~/.fonts</dir><include ignore_missing="yes">/etc/fonts/conf.d</include><cachedir>/var/cache/fontconfig</cachedir><cachedir prefix="xdg">fontconfig</cachedir><cachedir>~/.fontconfig</cachedir>in fonts.  All other blank chars are assumed to be broken and
</fontconfig>

发现上面的字体存在/usr/share/fonts目录下。

2.把Windows下的字体C:\Windows\Fonts下的宋体,即simsun.ttc上传到linux服务器并赋值到上面的字体目录下赋予读写权限

[root@VM_0_12_centos libreoffice]# ll | grep simsun.ttc
-rw-r--r-- 1 root root  18214472 Oct 25 13:19 simsun.ttc
cp simsun.ttc /usr/share/fonts
cd /usr/share/fonts

赋予权限(默认权限也可以,如果不可以就手动赋予权限即可)

chmod 644 simsun.ttc

3.更新字体缓存

fc-cache -fv

  再次转换pdf发现完美解决。

4.linux下Java程序调用libreoffice转换pdf

  文件的位置与输出目录通过主函数参数传递进去。

(1)先写一个简单的程序进行测试

import java.io.IOException;public class Test {public static void main(String[] args) throws NullPointerException {String filePath = args[0];String destDir = args[1];String osName = System.getProperty("os.name");System.out.println(filePath);System.out.println(destDir);System.out.println(osName);String cmd = "libreoffice6.0 --convert-to pdf:writer_pdf_Export " + filePath + " --outdir " + destDir;System.out.println(cmd);try {Runtime.getRuntime().exec(cmd);} catch (IOException e) {System.err.println(e.getMessage());}}}

我们在linux下面进行编译并且运行:

[root@VM_0_12_centos tmpFile]# javac Test.java
[root@VM_0_12_centos tmpFile]# java Test ./tt.docx ./
./tt.docx
./
Linux
libreoffice6.0 --convert-to pdf:writer_pdf_Export ./tt.docx --outdir ./
[root@VM_0_12_centos tmpFile]# ls
Test.class  Test.java  tt.docx  tt.pdf

(2)接下来简单的编写程序获取转换时间:(使线程等待抓换完成)

import java.io.IOException;public class Test {public static void main(String[] args) throws NullPointerException {long start = System.currentTimeMillis();String filePath = args[0];String destDir = args[1];String osName = System.getProperty("os.name");System.out.println(filePath);System.out.println(destDir);System.out.println(osName);String cmd = "libreoffice6.0 --convert-to pdf:writer_pdf_Export " + filePath + " --outdir " + destDir;System.out.println(cmd);try {Process process = Runtime.getRuntime().exec(cmd);try {// 获取返回状态int status = process.waitFor();// 销毁processprocess.destroy();process = null;System.out.println("status -> " + status);} catch (InterruptedException e) {System.err.println(e.getMessage());}} catch (IOException e) {System.err.println(e.getMessage());}long end = System.currentTimeMillis();System.out.println("用时:" + (end - start) + "ms");}}

再次在linux下面编译运行:

[root@VM_0_12_centos tmpFile]# java Test ./tt.docx ./
./tt.docx
./
Linux
libreoffice6.0 --convert-to pdf:writer_pdf_Export ./tt.docx --outdir ./
status -> 0
用时:1463ms
[root@VM_0_12_centos tmpFile]# ls
Test.class  Test.java  tt.docx  tt.pdf

至此完成了使用libreoffice在windows与linux下面转换pdf,这种方式感觉比较稳定。同时也学会了Runtime 调用本地程序以单线程方式运行的方法。

  文中用到的所有的tar包以及字体simsun.ttc下载地址:http://qiaoliqiang.cn/fileDown/linuxlibreoffice.zip

补充:word也可以转为html,测试word转html

word内容:

soffice.exe --headless --convert-to html .\通用功能需求收集20180723.docx

结果:

 补充:word可以转jpg

soffice.exe --headless --convert-to jpg .\通用功能需求收集20180723.docx

结果生成jpg:

 补充:word可以转txt

soffice.exe --headless --convert-to txt .\通用功能需求收集20180723.docx

结果:

补充:其实excel和ppt也可以转为pdf和html以及jpg,下面研究excel转换(只是边框被去掉,如果需要显示边框在excel中的样式需要显示边框;而且内容过长会折行,解决办法就是缩小列宽、减少列数)

原来excel内容:

转换:

soffice.exe --headless --convert-to jpg ./test.xlssoffice.exe --headless --convert-to html ./test.xlssoffice.exe --headless --convert-to pdf ./test.xls

(1)转换后的jpg

(2)转换的html

(3)转换后的pdf

补充:直接拷贝目录遇到的问题:

  今天拷贝下载好的目录使用时,发现报错缺失VCRUNTIME140.dll和MSVCP140.dll,于是拷贝另外一台电脑到缺失的电脑上就可以了。记住是C:\Windows\System32目录和C:\Windows\SysWOW64目录下对应的dll,这两个文件夹下的dll不一样,虽然文件名一样,但是大小不一样,所以要复制对应的dll。

补充;java也可以用jodconverter进行转换,我用的是jodconverter2.2版本(该工具包依赖openoffice或libreoffice插件)

依赖的jar包如下:

代码如下:

import java.io.File;
import java.io.IOException;import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;public class Office2Pdf {// 将word格式的文件转换为pdf格式public static void WordToPDF(String startFile, String overFile) throws IOException {// 源文件目录File inputFile = new File(startFile);if (!inputFile.exists()) {System.out.println("源文件不存在!");return;}// 输出文件目录File outputFile = new File(overFile);if (!outputFile.getParentFile().exists()) {outputFile.getParentFile().exists();}// 调用openoffice服务线程/** 我把openOffice下载到了 C:/Program Files (x86)/下 ,下面的写法自己修改编辑就可以 **/String command = "D:/zdc8/lo/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8300;urp;\"";Process p = Runtime.getRuntime().exec(command);// 连接openoffice服务OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8300);connection.connect();// 转换DocumentConverter converter = new OpenOfficeDocumentConverter(connection);converter.convert(inputFile, outputFile);// 关闭连接connection.disconnect();// 关闭进程p.destroy();}public static void main(String[] args) {String start = "C:\\Users\\Administrator\\Desktop\\123.xlsx";String over = "C:\\Users\\Administrator\\Desktop\\123.xlsx.pdf";try {WordToPDF(start, over);} catch (IOException e) {e.printStackTrace();}}
}

如果想去掉留痕,需要反编译jodconverter-2.2.2.jar,获取类OpenOfficeDocumentConverter.java,修改方法loadAndExport,如下:(加粗部分是添加的代码)

    private void loadAndExport(String inputUrl, Map/* <String,Object> */ loadProperties, String outputUrl,Map/* <String,Object> */ storeProperties) throws OpenOfficeException {XComponent document;try {document = loadDocument(inputUrl, loadProperties);} catch (ErrorCodeIOException errorCodeIOException) {throw new OpenOfficeException("conversion failed: could not load input document; OOo errorCode: " + errorCodeIOException.ErrCode,errorCodeIOException);} catch (Exception otherException) {throw new OpenOfficeException("conversion failed: could not load input document", otherException);}if (document == null) {throw new OpenOfficeException("conversion failed: could not load input document");}        XPropertySet mxDocProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, document);try {mxDocProps.setPropertyValue("RedlineDisplayType", RedlineDisplayType.NONE);} catch (Exception e) {throw new OpenOfficeException("dispose RedlineDisplay failed", e);}refreshDocument(document);try {storeDocument(document, outputUrl, storeProperties);} catch (ErrorCodeIOException errorCodeIOException) {throw new OpenOfficeException("conversion failed: could not save output document; OOo errorCode: " + errorCodeIOException.ErrCode,errorCodeIOException);} catch (Exception otherException) {throw new OpenOfficeException("conversion failed: could not save output document", otherException);}}

补充:基于libreoffice和jodconverter的文件在线预览插件,这个插件功能强大,使用简单

git地址:  https://github.com/kekingcn/kkFileView

博客地址:  https://my.oschina.net/keking/blog/3064732

Libreoffice实现office转pdf、html、jpg等格式数据相关推荐

  1. Office转PDF,Aspose太贵,怎么办?

    在程序开发中经常需要将Office文件转换成PDF,著名的Aspose的三大组件可以很容易完成这个功能,但是Aspose的每个组件都单独收费,而且每个都卖的不便宜.在老大的提示下,换了一种思路来解决这 ...

  2. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  3. Libreoffice php使用命令行office转pdf,pdf转图片

    我用的方法 word 转pdf 使⽤的是 libreoffice 开源office 系统. 使⽤命令是 soffice -h 仅仅使⽤了⼀个⽅法: --convert-to output_file_e ...

  4. word转图片 java_Java 利用LibreOffice将Office文档转换成 PDF,进而转图片,实现在线预览功能...

    项目中需要将 Office 文档上传并实现在线预览,用到了 LibreOffice 将 Office 文档转换为 PDF 文档,然后再用 pdfbox 将 PDF 转为图片. 本文介绍借助 Libre ...

  5. 【文件处理】【libreoffice】office文件转为pdf

    官网链接:官网:​​​​​​Linux 下的安装方法 | LibreOffice 简体中文官方网站 - 自由免费的办公套件 1. libreoffice安装 1.1 在ubuntu下安装 安装libr ...

  6. 智慧讲台:office 文件 pdf 文件 转成 png图片

    java 请使用poi ,pdfbox 试了以下java的方式还是不错的,poi的问题就是版本匹配问题,能把人迷惑,不过确实可以用,官方网站资料齐全,笔者尝试了以下,虽然可以但是没有使用libreof ...

  7. office 转 pdf文件

    找过很多地方都找不到用php实现"office 转 pdf文件"的,最后只能用Linux命令行来做了.如下: 主要就是centos+libreoffice+unoconv+Xvfb ...

  8. LibreOffice操作office文档

    Java 利用 LibreOffice/OpenOffice 将 Office 文档(.doc/.docx .ppt/.pptx )转换成 PDF,进而转图片,实现在线预览功能 2020-02-23 ...

  9. 借助 libreoffice,word 转 pdf 体验

    libreoffice 官网 https://zh-cn.libreoffice.org/ 软件下载镜像: https://mirrors.cloud.tencent.com/libreoffice/ ...

最新文章

  1. 39岁程序员住进养老院,早6晚9的作息,网友羡慕哭了
  2. Pandas Apply函数
  3. Mybatis中DAO层接口没有写实现类,Mapper中的方法和DAO接口方法是怎么绑定到一起的...
  4. spring源码:循环依赖源码学习
  5. IEEE Access的模板的问题
  6. 红外接收头图片_常见红外接收头、遥控接收头图片、引脚定义
  7. 【AMAD】beaker -- 用于session和缓存的WSGI中间件
  8. 计算机windows无响应,win10电脑开软件自动无响应怎么办_win10打开软件没响应的解决方法...
  9. excel整列动态联动下拉_在Excel下拉列表中显示多列
  10. 夜神安卓模拟器怎么设置代理
  11. 什么软件可以测试sd卡,如何做SD卡测试和SD卡测试工具
  12. 【dht】dht简介以及使用nodejs查找dht网络学习笔记
  13. json对象遍历输出key和value
  14. 急急急!寻找程序员的祖师爷
  15. AI云边端EasyCVR平台新功能解析:支持为角色选择多级分组
  16. Java数据结构——堆排序
  17. c语言程序设计 张曙光,第6章C语言程序设计(张曙光)教材各章习题解答.doc
  18. 【调剂】哈尔滨工程大学信通学院卫星通信导航智能处理团队招收2021级硕士研究生...
  19. jquery课后练习
  20. Python爬虫入门——3.7 Scrapy爬虫框架安装

热门文章

  1. mysql —— 分表分区
  2. 流量劫持能有多大危害?
  3. Swift中文教程(十九) 类型嵌套
  4. 9 Fresh jQuery Media Players
  5. 剑指offer 31.栈的、压入弹出序列
  6. 动态规划——有趣的数(ccf)
  7. 【今日CV 计算机视觉论文速览】Mon, 4 Mar 2019
  8. Java—格式化日期/时间
  9. 媒体控件的播放暂停继续与停止 winform
  10. 谷歌浏览器安装过程 0911