Tap*_*per 102
您还可以根据libreoffice自己的目的使用命令行。这为您提供了批量转换的优势。但单个文件也是可能的。本示例将当前目录中的所有 ODT 文件转换为 PDF:
libreoffice --headless --convert-to pdf *.odt
Run Code Online (Sandbox Code Playgroud)
通过以下方式获取有关命令行选项的更多信息:
man libreoffice
Run Code Online (Sandbox Code Playgroud)
Tak*_*kat 76
只需使用 libre office 打开文档并选择Export as PDF...:

unoconv -f pdf mydocument.odt
Run Code Online (Sandbox Code Playgroud)
注意: unoconv 依赖于 Libre Office。
以下是有关“非 GUI”方法的更多详细信息。
您不仅可以使用此方法将 ODT 文件转换为 PDF。它也适用于 MS Word DOCX 文件(它与 LibreOffice 能够处理特定 ODT 一样有效),并且通常适用于 LibreOffice 可以打开的所有文件类型。
我不认为有一个二进制文件被命名libreoffice为建议的其他答案之一。但是,有soffice(.bin)-- 可用于从命令行启动 LibreOffice 的二进制文件。它通常位于/usr/lib/libreoffice/program/; 并且很多时候,符号链接/usr/bin/soffice指向那个位置。
那么,在大多数情况下,参数--headless --convert-to pdf是不够的。它必须是:
--headless --convert-to pdf:writer_pdf_Export
Run Code Online (Sandbox Code Playgroud)
一定要完全遵循这个大写!
接下来,如果您的系统上已经启动并运行了 LibreOffice GUI 实例,则该命令将不起作用。它是由自 2011 年以来已知的错误#37531引起的。将此附加参数添加到您的命令中:
"-env:UserInstallation=file:///tmp/LibreOffice_Conversion_${USER}"
Run Code Online (Sandbox Code Playgroud)
这将创建一个新的独立环境,可供第二个无头 LO 实例使用,而不会干扰由同一用户启动的可能正在运行的第一个 GUI LO 实例。
另外,请确保--outdir /pdf您指定的 确实存在,并且您对它有写权限。或者,使用不同的输出目录。即使它只是用于第一轮测试和调试:
$ mkdir ${HOME}/lo_pdfs
Run Code Online (Sandbox Code Playgroud)因此:
/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
Run Code Online (Sandbox Code Playgroud)
这在 Mac OS X Yosemite 10.10.5 和 LibreOffice v5.1.2.2 上对我有用(使用我的二进制文件的特定路径,soffice无论如何在 Ubuntu 上会有所不同......)。它也适用于 Debian Jessie 8.0(使用 path /usr/lib/libreoffice/program/soffice)。抱歉,现在无法在 Ubuntu 上测试它....
如果这一切都不起作用,当您尝试处理 DOCX 时:
您尝试使用该命令的特定 DOCX 文件可能有问题...因此,请先创建一个您自己的非常简单的 DOCX 文档。为此,请使用 LibreOffice 本身。写下“你好世界!” 在一个空白的页面上。将其另存为 DOCX。
再试一次。它适用于简单的 DOCX 吗?
如果它再次不起作用,请重复第 7 步,但这次保存为 ODT。
重复步骤 8,但这次一定要引用 ODT。
最后:使用完整路径 to soffice, tosoffice.bin和 tolibreoffice并使用-h参数运行每个:
$ /path/to/libreoffice -h # if that path exists, which I doubt!
$ /path/to/soffice -h
$ /path/to/soffice.bin -h
Run Code Online (Sandbox Code Playgroud)
将它们与您使用的命令行进行比较:
为了比较,我自己的(Mac OS X)输出在这里:
$ /Applications/LibreOffice.app/Contents/MacOS/soffice -h
LibreOffice 5.1.2.2 d3bf12ecb743fc0d20e0be0c58ca359301eb705f
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 user interaction 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 (implies --headless).
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.
Run Code Online (Sandbox Code Playgroud)在命令行中再添加一个参数,以在soffice打开 DOCX 文件时强制应用输入过滤器:
--infilter="Microsoft Word 2007/2010/2013 XML"
Run Code Online (Sandbox Code Playgroud)
或者
--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"
Run Code Online (Sandbox Code Playgroud)