利用LibreOffice组建内网预览OFFICE文档

微光 网络收藏 527 0

LibreOffice命令

# libreoffice4.4 --helpLibreOffice 4.4.7.2 f3153a8b245191196a4b6b9abd1d0da16eead600

Usage: soffice [options] [documents...]


Options:--minimized    keep startup bitmap minimized.--invisible    no startup screen, no default document and no UI.--norestore    suppress restart/restore after fatal errors.--quickstart   starts the quickstart service--nologo       don't show startup screen.--nolockcheck  don't check for remote instances using the installation--nodefault    don't start with an empty document--headless     like invisible but no userinteraction at all.--help/-h/-?   show this message and exit.--version      display the version information.--writer       create new text document.--calc         create new spreadsheet document.--draw         create new drawing.--impress      create new presentation.--base         create new database.--math         create new formula.--global       create new global document.--web          create new HTML document.-o             open documents regardless whether they are templates or not.

-n             always open documents as new files (use as template).--display <display>

      Specify X-Display to use in Unix/X11 versions.

-p <documents...>

      print the specified documents on the default printer.--pt <printer> <documents...>

      print the specified documents on the specified printer.--view <documents...>

      open the specified documents in viewer-(readonly-)mode.--show <presentation>

      open the specified presentation and start it immediately--accept=<accept-string>

      Specify an UNO connect-string to create an UNO acceptor through which

      other programs can connect to access the API--unaccept=<accept-string>

      Close an acceptor that was created with --accept=<accept-string>

      Use --unnaccept=all to close all open acceptors--infilter=<filter>[:filter_options]

      Force an input filter type if possible

      Eg. --infilter="Calc Office Open XML"

          --infilter="Text (encoded):UTF8,LF,,,"--convert-to output_file_extension[:output_filter_name[:output_filter_options]] [--outdir output_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

          --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] files

      Batch print files to file.      If --outdir is not specified then current working dir is used as output_dir.

      Eg. --print-to-file *.doc

          --print-to-file --printer-name nasty_lowres_printer --outdir /home/user *.doc--cat files

      Dump text content of the files to console

      Eg. --cat *.odt--pidfile file

      Store soffice.bin pid to file.

-env:<VAR>[=<VALUE>]      Set a bootstrap variable.

      Eg. -env:UserInstallation=file:///tmp/test to set a non-default user profile path.


Remaining arguments will be treated as filenames or URLs of documents to open.

libreoffice命令行方式将word、excel、ppt转成pdf

项目需要,上传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,直接使用:


  1. [root@instance-32spzihn /]# yum install libreoffice

提示是否是要安装,执行y就可以了。

一般情况下用yum安装的包都不会是最新版本的,想要安装最新版本,只能够自己下载最新的回来,手动编译安装:



libreoffice官网:http://zh-cn.libreoffice.org/ 

目前最新版本:LibreOffice_5.4.0_Linux_x86-64_rpm.tar.gz 


  1. [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*' 查找出相关的执行程序再处理。


竟然报错:



  1. [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)

此时只需要安装另外一个库即可解决:


  1. [root@instance-32spzihn /]# yum install libreoffice-headless

再执行转换命令,转换成功。

sz zzz.pdf,下载pdf回到本地打开发现中文乱码,这是因为缺少中文字体支付所致,所以先把 c:\Windows\Fonts文件夹复制一份到其它盘,然后打包成Fonts.zip,通过rz Fonts.zip 将压缩包传到服务器上面。


  1. [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命令处理。


进入“组件服务”->"计算机"->"我的电脑"->”DCOM配置“,找到OpenOffice。。。。开头的,右击属性,安全,把”启动和激活权限“选择自定义,然后”编辑“添加”guest“


然后、、、

function MakePropertyValue($name,$value,$osm){  
    $oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");  
    $oStruct->Name = $name;  
    $oStruct->Value = $value;  
    return $oStruct;  
}function word2pdf($doc_url, $output_url){  
    $osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.n");  
    $args = array(MakePropertyValue("Hidden",true,$osm));  
    $oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");  
    $oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args);  
    $export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));  
    $oWriterDoc->storeToURL($output_url,$export_args);  
    $oWriterDoc->close(true);  
}function topdf($file1,$pdf_file){     $output_dir = "C:/AppServ/www/swust/public/uploads/pdf/";  
    $doc_file = "C:/AppServ/www/swust/public/uploads/".$file1;  
    $output_file= $output_dir.$pdf_file;  
    $doc_file = "file:///" . $doc_file;  
    $output_file = "file:///" . $output_file;

word2pdf($doc_file,$output_file);

>我会首先尝试给绝对路径命令。
>我怀疑libreoffice二进制不工作 – 你必须找到soffice二进制,看看是否工作。
>然后,你的–convert-到pdf是不够的。它需要:



--convert-to pdf:writer_pdf_Export

一定要遵循这个大小写!
>接下来,如果您的系统上已经启动并运行了一个LibreOffice GUI实例,该命令将不会工作。它是由bug known since 2011引起的。将此附加参数添加到命令中:


"-env:UserInstallation=file:///tmp/LibreOffice_Conversion_${USER}"

这将创建一个新的,单独的环境,其可以由第二个无头LO实例使用,而不干扰由相同用户启动的可能运行的第一GUI LO实例。
>另外,请确保您指定的–outdir / pdf确实存在,并且您具有写入权限。或者,使用不同的输出dir。即使它只是第一次测试和这个调试回合:


$ mkdir ${HOME}/lo_pdfs

>因此:


/path/to/soffice                                                     \
  --headless                                                         \  "-env:UserInstallation=file:///tmp/LibreOffice_Conversion_${USER}" \
  --convert-to pdf:writer_pdf_Export                                 \
  --outdir ${HOME}/lo_pdfs                                           \
/path/to/test.docx

这对我来说在Mac OS X Mavericks 10.9.5与LibreOffice v4.4.3.2(使用我的特定路径的二进制soffice,这将是不同的你反正…)。

如果所有这些都不起作用:
>这可能是一个问题,具体的DOCX文件您尝试命令与…所以创建一个非常简单的DOCX文档您自己的第一。使用LibreOffice本身。写“Hello World!”在其他空页面上。将其另存为DOCX。
>再试一次。它与简单的DOCX工作吗?
>如果它再次不工作,重复步骤7,但此时保存为ODT。
>重复步骤8,但请确保此时引用ODT。
> Last:使用soffice的完整路径,soffice.bin和libreoffice,并使用-h参数运行每个:


$ /path/to/libreoffice -h$ /path/to/soffice -h$ /path/to/soffice.bin -h

>你甚至得到一个输出吗?
>对于三个二进制文件/符号链接中的哪一个?
>记录输出。
>告诉我们你的输出!

将它们与您使用的命令行进行比较:


Are there any changes in parameter names, capitalizations, number of dashes used, etc.??

为了比较,我自己的输出在这里:


$ /Applications/LibreOffice.app/Contents/MacOS/soffice -h

  LibreOffice 4.4.3.2 88805f81e9fe61362df02b9941de8e38a9b5fd16

  Usage: soffice [options] [documents...]

  Options:  --minimized    keep startup bitmap minimized.
  --invisible    no startup screen, no default document and no UI.
  --norestore    suppress restart/restore after fatal errors.
  --quickstart   starts the quickstart service
  --nologo       don't show startup screen.
  --nolockcheck  don't check for remote instances using the installation
  --nodefault    don't start with an empty document
  --headless     like invisible but no userinteraction at all.
  --help/-h/-?   show this message and exit.
  --version      display the version information.
  --writer       create new text document.
  --calc         create new spreadsheet document.
  --draw         create new drawing.
  --impress      create new presentation.
  --base         create new database.
  --math         create new formula.
  --global       create new global document.
  --web          create new HTML document.
  -o             open documents regardless whether they are templates or not.
  -n             always open documents as new files (use as template).  --display <display>
        Specify X-Display to use in Unix/X11 versions.
  -p <documents...>
        print the specified documents on the default printer.  --pt <printer> <documents...>
        print the specified documents on the specified printer.  --view <documents...>
        open the specified documents in viewer-(readonly-)mode.  --show <presentation>
        open the specified presentation and start it immediately  --accept=<accept-string>
        Specify an UNO connect-string to create an UNO acceptor through which
        other programs can connect to access the API  --unaccept=<accept-string>
        Close an acceptor that was created with --accept=<accept-string>
        Use --unnaccept=all to close all open acceptors
  --infilter=<filter>[:filter_options]
        Force an input filter type if possible
        Eg. --infilter="Calc Office Open XML"
            --infilter="Text (encoded):UTF8,LF,,,"
  --convert-to output_file_extension[:output_filter_name[:output_filter_options]] [--outdir output_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
            --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] files
        Batch print files to file.        If --outdir is not specified then current working dir is used as output_dir.
        Eg. --print-to-file *.doc
            --print-to-file --printer-name nasty_lowres_printer --outdir /home/user *.doc
  --cat files
        Dump text content of the files to console
        Eg. --cat *.odt
  --pidfile file
        Store soffice.bin pid to file.
  -env:<VAR>[=<VALUE>]        Set a bootstrap variable.
        Eg. -env:UserInstallation=file:///tmp/test to set a non-default user profile path.

  Remaining arguments will be treated as filenames or URLs of documents to open.

>向命令行添加一个参数,以在soffice打开DOCX文件时强制应用输入过滤器:


--infilter="Microsoft Word 2007/2010/2013 XML"

要么


--infilter="Microsoft Word 2007/2010/2013 XML"
--infilter="Microsoft Word 2007-2013 XML"
--infilter="Microsoft Word 2007-2013 XML Template"
--infilter="Microsoft Word 95 Template"
--infilter="MS Word 95 Vorlage"
--infilter="Microsoft Word 97/2000/XP Template"
--infilter="MS Word 97 Vorlage"
--infilter="Microsoft Word 2003 XML"
--infilter="MS Word 2003 XML"
--infilter="Microsoft Word 2007 XML Template"
--infilter="MS Word 2007 XML Template"
--infilter="Microsoft Word 6.0"
--infilter="MS WinWord 6.0"
--infilter="Microsoft Word 95"
--infilter="MS Word 95"
--infilter="Microsoft Word 97/2000/XP"
--infilter="MS Word 97"
--infilter="Microsoft Word 2007 XML"
--infilter="MS Word 2007 XML"
--infilter="Microsoft WinWord 5"
--infilter="MS WinWord 5"

更新

LibreOffice不仅可以在命令行上将DOCX转换为PDF:

>它可以处理任何可以打开和“读取”的输入格式:DOC,XLS,XLSX,PPT,PPTX,…
>它可以转换为任何输出格式,它可以“写”:DOC,XLS,XLSX,PPT,PPTX,…

当然,结果永远不会是完美的,有时他们甚至是你不能接受的。

回覆。输出为PDF:

要控制哪个LibreOffice组件生成PDF输出,您可以使用以下变量:


--convert-to pdf:writer_pdf_Export
--convert-to pdf:calc_pdf_Export
--convert-to pdf:draw_pdf_Export
--convert-to pdf:impress_pdf_Export
--convert-to pdf:writer_web_pdf_Export

回覆。输入不是DOCX:

要对非DOCX输入格式强制使用infilter,您可以使用(列表不完整):


--infilter="HTML Document"                      # for HTML input
--infilter="MediaWiki"                          # for MediaWiki input
--infilter="Text CSV"                           # for CSV spreadsheet input
--infilter="Microsoft PowerPoint 2007/2010 XML" # for PPTX input
--infilter="Microsoft PowerPoint 97/2000/XP"    # for PPT input
--infilter="Windows Metafile"                   # for WMF input
--infilter="Enhanced Metafile"                  # for EMF input
--infilter="Scalable Vector Graphics"           # for SVG input
--infilter="Microsoft Excel 2007/2010 XML"      # for XLSX input
--infilter="Microsoft Excel 97/2000/XP"         # for XLS input
--infilter="Microsoft Excel 95"                 # for some XLS input
--infilter="Microsoft Excel 5.0"                # for some XLS input

回覆。输出不是PDF:

要转换为特定的输出格式,您可以使用(列表不完整):


--convert-to html:HTML
--convert-to html:draw_html_Export                 # force "Draw" to generate the HTML
--convert-to mediawiki:MediaWiki_Web               # generate MediaWiki output
--convert-to csv:"Text - txt - csv (StarCalc)"     # generate CSV spreadsheet output
--convert-to pptx:"Impress MS PowerPoint 2007 XML" # generate PPTX
--convert-to ppt:"MS PowerPoint 97"                # generate PPT
--convert-to wmf:impress_wmf_Export                # force "Impress" to generate the WMF
--convert-to wmf:draw_wmf_Export                   # force "Draw" to generate the WMF
--convert-to emf:impress_emf_Export                # force "Impress" to generate the EMF
--convert-to emf:draw_emf_Export                   # force "Draw" to generate the EMF
--convert-to svg:impress_svg_Export                # force "Impress" to generate the SVG
--convert-to svg:draw_svg_Export                   # force "Draw" to generate the SVG
--convert-to xlsx:"Calc MS Excel 2007 XML"         # generate XLSX
--convert-to xls:"MS Excel 97"                     # generate XLS like Excel 97
--convert-to xls:"MS Excel 95"                     # generate XLS like Excel 95
--convert-to xls:"MS Excel 5.0/95"                 # generate XLS like Excel 5.0/95


发布评论 0条评论)

  • Refresh code

还木有评论哦,快来抢沙发吧~