命令 `libreoffice --headless --convert-to pdf test.docx --outdir /pdf` 不起作用

Command `libreoffice --headless --convert-to pdf test.docx --outdir /pdf` is not working

使用这个旨在将 DOCX 转换为 PDF 的 LibreOffice 命令后,我不得不等待很长时间:

libreoffice --headless --convert-to pdf test.docx --outdir  /pdf

但我没有收到任何回复,也没有收到任何错误。没有文件被转换。看起来终端挂了。

那么,有什么方法可以追踪错误吗?

  1. 我会首先尝试为命令提供绝对路径。

  2. 我怀疑 libreoffice 二进制文件不起作用 - 您必须找到 soffice 二进制文件并查看它是否有效。

  3. 那么,你的--convert-to pdf是不够的。必须是:

    --convert-to pdf:writer_pdf_Export
    

    一定要严格按照这个大小写!

  4. 接下来,如果您的系统上已经有一个 LibreOffice GUI 实例启动并且运行正在运行,该命令将不起作用。这是由错误 known since 2011 引起的。将此附加参数添加到您的命令:

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

    这将创建一个新的独立环境,可供第二个无头 LO 实例使用,而不会干扰可能由同一用户启动的 运行ning 第一个 GUI LO 实例。

  5. 此外,请确保您指定的 --outdir /pdf 确实存在,并且您有写入权限。或者,而是使用不同的输出目录。即使只是第一次测试和本次调试:

    $ mkdir ${HOME}/lo_pdfs
    
  6. 因此:

    /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 的特定路径,这对于反正你...).

    如果这一切都不起作用:

  7. 您尝试使用命令的特定 DOCX 文件可能有问题...所以首先创建一个非常简单的您自己的 DOCX 文档。为此使用 LibreOffice 本身。在空白页上写 "Hello World!"。将其保存为 DOCX。

  8. 再试一次。 是否适用于简单的 DOCX?

  9. 如果还是不行,重复第7步,但这次保存为ODT。

  10. 重复第 8 步,但确保这次引用 ODT。

  11. 最后:使用 sofficesoffice.binlibreoffice 和 运行 的完整路径,每个路径都带有 -h 参数:

    $ /path/to/libreoffice -h
    $ /path/to/soffice -h
    $ /path/to/soffice.bin -h
    
    • 这里有输出吗?
    • 三个中的哪一个binaries/symlinks?
    • 记录输出。
    • 告诉我们你的产出!!!

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

    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.
    
  12. 在您的命令行中再添加一个参数,以在 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:

  • 它可以处理任何它可以打开的输入格式和"read": DOC, XLS, XLSX, PPT, PPTX, ...
  • 它可以转换成任何输出格式"write":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="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

更新

当前运行的 libreoffice 版本:v5.2.6.2 (mac)

我正在使用以下命令

/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf --outdir . the_file_to_convert

似乎转换在较新版本 (> 4.2.4.2) 中不起作用 我正在使用 libreoffice 4.2.4.2 您可以从 http://downloadarchive.documentfoundation.org/libreoffice/old/4.2.4.2/

下载旧版本
  • linux(使用deb包):

    libreoffice4.2 --headless --convert-to pdf  myfile.odt
    
  • windows:

    swriter.exe --headless --convert-to pdf myfile.odt
    

听起来可能很愚蠢,但我就是这样受苦的:在 Ubuntu 14.04 中,仅安装 libreoffice-core-common 包来转换为 PDF 是不够的,但您需要安装 libreoffice-writer instead/additionally。需要注意的重要一点是 libreoffice-core|common 似乎引入了一些功能性的 libreoffice 基础,因此得名 ;-)。如果执行转换某些文件,那不会出错,而是似乎只是在等待某个套接字或类似的输入或其他任何东西......在我安装 libreoffice-writer 之后,它具有 [...]-core|common 作为依赖项, 将一些 ODT 文件转换为 PDF 就像以前使用的完全相同的命令行一样有魅力。

似乎有点疯狂,应用程序看起来以某种方式工作,但由于缺少包而根本无法工作,并且没有说明任何事情。

我也在使用 LibreOffice 4.2.8.2(在 GNU/Linux 上),但我试图将 DOC 文件转换为 HTML 文件。我已经安装了 libreoffice-headless,但是终端也挂了。我试过 Kurt Pfeifle 的每一颗子弹,但都没有用……

解决方案很简单:安装libreoffice-writer(也许libreoffice-filters也可以,但这取决于libreoffice-writer加上其他大LibreOffice 的组件)。

希望这会有所帮助。

我遇到了同样的问题,卡了一整天;愚蠢的我最终发现我正在尝试与网络文件系统进行转换;即使我写对了,libreoffice 似乎也不支持它,或者至少不在我的配置中。

我通过转换为 /tmp 然后手动移动文件解决了这个问题。希望对你有帮助。

我在使用 libreoffice 将 HTML 转换为 xlsx 时遇到问题。其他转换为选项工作正常。原来它还需要 --calc 标志。以防其他人遇到同样的麻烦。在 LibreOffice-6.4.3.2 上测试。

感谢:Libreoffice converting html to xls or xlsx