我用的方法
word 轉pdf
使⽤的是 libreoffice 開源office 系統。
使⽤命令是 soffice -h
僅僅使⽤了⼀個⽅法:
--convert-to output_file_extension[:output_filter_name] [-outdir ouput_dir]
files
Batch convert files.
If -outdir is not specified then current working dir is used as output_dir.
Eg. -convert-to pdf *.doc
-convert-to pdf:writer_pdf_Export -outdir /home/user *.doc
1、 1 使⽤java jod
libreoffice 第三⽅office⼯具
安裝命令
yum install libreoffice-headless
libreoffice-writer
啟動命令
libreoffice5.4 --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --
nofirststartwizard &
javabridge
java -jar /opt/java/webapps/JavaBridge/WEB-INF/lib/JavaBridge.jar
SERVLET_LOCALb8080
查看tomcat的⽅法
tail -f /opt/java/logs/localhost.2017-09-01.log
tail -f /opt/java/logs/catalina.log
java在 /opt/java/
程序在 /opt/java/webapps/ 下, enrollCertDemo 天威CA , jodconverter為轉換
word to pdf ⼯具
/root/ghostscript-9.21-linux-x86_64/ 為pdf轉圖⽚⼯具
參考1
我用的centos系統版本,是6.5版本64位
1、配置centos的yum源之前我的阿里雲服務器的centos,好像因為沒有配置yum源而使用系統自帶的,通過yum install libreoffice沒找到Libreoffice安裝包(當然,這可能是我系統版本的問題,具體就不得而知了,因為后來搞不定又繼續更換了其他版本的centos)。
yum源配置,因為我使用的是阿里雲服務器,所以我使用阿里雲的yum源。
配置方法:http://mirrors.aliyun.com/help/centos
2、安裝Libreoffice
- yum install libreoffice
然后我使用soffice命令轉化doc測試文檔
- soffice --headless --invisible --convert-to pdf doctest.docx
然后出現報錯了:
- /usr/lib64/libreoffice/program/soffice.bin X11 error: Can't open display:
- Set DISPLAY environment variable, use -display option
- or check permissions of your X-Server
- (See "man X" resp. "man xhost" for details)
- [root@iZ2zec51rnaynn5uqhupucZ ~]# man x
- Cannot open the message catalog "man" for locale "zh_CN.UTF-8"
- (NLSPATH="/usr/share/locale/%l/LC_MESSAGES/%N")
- No manual entry for x
后來在這里找到了解決辦法:http://superuser.com/questions/6 ... r-cant-open-display
我執行下面這個命令,然后解決了上面的那個報錯問題:
- yum install libreoffice-headless
然后我再通過soffice命令將doc測試文檔轉成pdf,終於順利轉化成功!
然而又出現問題了。。。
轉化后的pdf文件,我看到了一個一個密密麻麻的小方框。。亂碼!
然后我通過“locale -a”查看了下,是不是系統不支持中文的問題。然后發現系統是支持中文的。
- ....
那么問題就可能是出現在libreoffice上,然后通過搜索引擎找到了解決辦法:http://www.linuxidc.com/Linux/2012-12/77017.htm
- yum -y install libreoffice-langpack-zh-Han*
centos上運行上面的命令之后,給libreoffice安裝了中文的語言支持,然后順利地把doc測試文檔無亂碼地轉成了pdf文件。
其實我已經不想說為了解決Libreoffice將office文檔轉pdf的問題已經讓我折騰了三天了。。。Ubuntu和centos系統來回地更換安裝測試。不過,還是終於把問題解決了。
剩下的,pdf.js實現文檔在線瀏覽,請看這個帖子:
http://www.hc-cms.com/forum.php?mod=viewthread&tid=14
參考2
http://blog.csdn.net/ljihe/article/details/77250206
項目需要,上傳doc、excel、ppt等格式文件到服務器,用戶可以在線閱讀。經過討論,決定在上傳文件的同時,轉換出一份pdf文件保存,用戶閱讀就只需要展示pdf文件即可,下載可以下載原文件。因為pdf文件需要在web頁面上展示還是比較方便的,直接使用html的embed標簽就能實現,也可以使用一些pdf.js的插件實現:
- <!DOCTYPE HTML>
- <html>
- <body>
- <embed src="zpdf.pdf" width="100%" height="600">
- </body>
- </html>
首先我們需要將doc等格式的文檔轉換成pdf,網上搜索一翻,實現方式也就那幾種,最主流的就是通過openoffice.org或者libreoffice兩種方式實現,其實libreoffice也是從openoffice.org分離出來的,功能上面應該相差不大。對於要實現格式轉換,兩種庫都可以。
我這里直接使用libreoffice的方式。
首先安裝libreoffice,我的服務器是centos,直接使用:
- [root@instance-32spzihn /]# yum install libreoffice
提示是否是要安裝,執行y就可以了。
一般情況下用yum安裝的包都不會是最新版本的,想要安裝最新版本,只能夠自己下載最新的回來,手動編譯安裝:
libreoffice官網:http://zh-cn.libreoffice.org/
目前最新版本:LibreOffice_5.4.0_Linux_x86-64_rpm.tar.gz
- [root@instance-32spzihn src]# tar -zxvf LibreOffice_5.4.0_Linux_x86-64_rpm.tar.gz
- [root@instance-32spzihn src]# cd LibreOffice_5.4.0.3_Linux_x86-64_rpm/RPMS
- [root@instance-32spzihn RPMS]# yum install *.rpm
安裝完成后執行:
- [root@instance-32spzihn /]# /usr/bin/libreoffice --invisible --convert-to pdf zzz.docx
如果是自行編譯需要帶版本號:
- [root@instance-32spzihn /]# /usr/bin/libreoffice5.4 --invisible --convert-to pdf zzz.docx
具體情況可以先用 find / -name 'libreoffice*' 查找出相關的執行程序再處理。
竟然報錯:
- [root@instance-32spzihn /]# /usr/bin/libreoffice --invisible --convert-to pdf zzz.docx
- /usr/lib64/libreoffice/program/soffice.bin X11 error: Can't open display:
- Set DISPLAY environment variable, use -display option
- or check permissions of your X-Server
- (See "man X" resp. "man xhost" for details)
此時只需要安裝另外一個庫即可解決:
- [root@instance-32spzihn /]# yum install libreoffice-headless
再執行轉換命令,轉換成功。
sz zzz.pdf,下載pdf回到本地打開發現中文亂碼,這是因為缺少中文字體支付所致,所以先把 c:\Windows\Fonts文件夾復制一份到其它盤,然后打包成Fonts.zip,通過rz Fonts.zip 將壓縮包傳到服務器上面。
- [root@instance-32spzihn /]# cd /usr/share/fonts
- [root@instance-32spzihn fonts]# rz
- [root@instance-32spzihn fonts]# unzip Fonts.zip
- [root@instance-32spzihn fonts]# mv Fonts win
- [root@instance-32spzihn fonts]# cd win
- [root@instance-32spzihn win]# chmod -Rf 755 *
- [root@instance-32spzihn win]# mkfontscale
- [root@instance-32spzihn win]# mkfontdir
- [root@instance-32spzihn win]# fc-cache –fv
若沒有生效可以試下重啟服務器,再重新執行轉換命令就可以了。
完成對文件轉換后,那接下來就要將此功能加入項目開發上面,當前需要加入到一個php項目:
- $retval = 1;
- // exec() might be disabled
- $cmd = '/usr/bin/libreoffice --invisible --convert-to pdf zzz.docx';
- if (function_exists('exec')){
- @exec($cmd, $output, $retval);
- }
- // Did it work?
- if ($retval > 0){
- exit('process_failed');
- }
具體實現邏輯可以根據自己實際需求做更改,原理就是程序內執行shell命令處理。