bash 命令行等效于从 Mac OS X finder 打印 .docx 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8879917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command line equiv to print a .docx file from Mac OS X finder
提问by TJChambers
I have an application need to print a MS Word .docx file from the command line. The background is this is on Snow Leopard Server with CUPS-PDF as the default printer. If in the finder I click once on the .docx file to select and then select print from the file menu it opens MS Word (2011 is installed) and prints the file to the spool (via CUPS) in PDF format. Perfect! Now what is the BASH command equivalent of that???
我有一个应用程序需要从命令行打印 MS Word .docx 文件。背景是这是在以 CUPS-PDF 作为默认打印机的 Snow Leopard 服务器上。如果在查找器中单击一次 .docx 文件进行选择,然后从文件菜单中选择打印,它会打开 MS Word(已安装 2011)并将文件以 PDF 格式打印到线轴(通过 CUPS)。完美的!现在什么是 BASH 命令等价物???
I have tried lpr, which only appears to work with straight text files. I have tried OPEN which OPENS MS Word but I see no "print" option for open. I have tried the usual MS Office equivalent (OpenOffice and LibreOffice) which both work but the generated PDF layout is so far from that generated by Word that it is shamefully unacceptable. I tried using osascript = AppleScript = which works but is spotty - it does not always trigger.
我试过 lpr,它似乎只适用于纯文本文件。我试过 OPEN,它可以打开 MS Word,但我看不到打开的“打印”选项。我已经尝试过通常的 MS Office 等价物(OpenOffice 和 LibreOffice),它们都可以工作,但生成的 PDF 布局与 Word 生成的布局相差甚远,令人无法接受。我尝试使用 osascript = AppleScript = 它有效但不稳定 - 它并不总是触发。
My challenge is I am creating the .docx outside of word from a template with a PHP program and just need to convert it to PDF retaining all formatting.
我的挑战是我正在使用 PHP 程序从模板创建 .docx 之外的文件,只需将其转换为 PDF 并保留所有格式。
回答by David Sanson
Take a look at launch, available here: http://sabi.net/nriley/software/, also easy to install using homebrew or macports.
看看launch,可在此处获得:http: //sabi.net/nriley/software/,也可以使用自制软件或 macports 轻松安装。
It is like open, but adds some extra features, including
它就像open,但增加了一些额外的功能,包括
-p Ask application(s) to print document(s).
回答by jon
If you have the Automator action "Convert Format of Word Document"installed, you could write an Automator workflow and then try to use the automatorcommand line tool (see man automator).
如果您安装了 Automator 操作"Convert Format of Word Document",您可以编写一个 Automator 工作流,然后尝试使用automator命令行工具(请参阅 参考资料man automator)。
Further information:
更多信息:
- Automator Tip of the Week #3: Convert Word Documents to PDF
- How to Batch Convert DOC files to PDF format Using Mac OSX Automator
Another option might be to use PDFwriter for Mac(hosted on sourceforge) or CUPS-PDF for Mac OS X(by codepoetry) and then use the command line ...
另一种选择可能是使用PDFwriter for Mac(托管在 sourceforge 上)或CUPS-PDF for Mac OS X(通过 codepoetry)然后使用命令行......
# cf. http://www.mac-forums.com/forums/os-x-apps-games/212770-how-print-files-preview-pdf-command-line.html
lpstat -v | awk '{ print ; }'
lpr -P <printer name> /path/to/file
回答by Deduplicator
OP's solution extracted from question:
从问题中提取的 OP 解决方案:
using launch and PDFwriter with the Ruby Script below.
require 'rb-fsevent' paths = ['/private/var/hosts/toPDF'] options = {:latency => 5, :no_defer => true } fsevent = FSEvent.new fsevent.watch paths, options do |directories| Dir['/private/var/hosts/toPDF/*.docx'].each do |f| puts 'processing '+f system( "launch -p "+f) end sleep 30 system("killall -u tj 'Microsoft Word'") system("killall -u tj 'Microsoft Error Reporting'") end fsevent.run
使用 launch 和 PDFwriter 以及下面的 Ruby 脚本。
require 'rb-fsevent' paths = ['/private/var/hosts/toPDF'] options = {:latency => 5, :no_defer => true } fsevent = FSEvent.new fsevent.watch paths, options do |directories| Dir['/private/var/hosts/toPDF/*.docx'].each do |f| puts 'processing '+f system( "launch -p "+f) end sleep 30 system("killall -u tj 'Microsoft Word'") system("killall -u tj 'Microsoft Error Reporting'") end fsevent.run
回答by Al Johri
You can use the docx2pdftool to convert a docx file to pdf retaining all formatting. It works on macOS and Windows and requires Microsoft Word to be installed.
您可以使用该docx2pdf工具将 docx 文件转换为 pdf 并保留所有格式。它适用于 macOS 和 Windows,需要安装 Microsoft Word。
pip install docx2pdf
docx2pdf input.docx output.pdf
Disclaimer: I wrote the docx2pdf tool. https://github.com/AlJohri/docx2pdf
免责声明:我编写了 docx2pdf 工具。https://github.com/AlJohri/docx2pdf
回答by claudio
Another way to convert a MS Word .docx document into a PDF file on Mac OS X would be to use the qlmanagecommand that ships with Mac OS X.
在 Mac OS X 上将 MS Word .docx 文档转换为 PDF 文件的另一种方法是使用qlmanageMac OS X 附带的命令。
qlmanagecan be used to transform a .docx document into a directory that contains a Preview.htmlfile, a PreviewProperties.plistfile and perhaps some dumped attachment files. After tweaking Preview.htmla bit, a PDF file can be generated by using wkhtmltopdf(or wkpdfor prince).
qlmanage可用于将 .docx 文档转换为一个目录,该目录包含一个Preview.html文件、一个PreviewProperties.plist文件,可能还有一些转储的附件文件。稍作调整后Preview.html,可以使用wkhtmltopdf(或wkpdf或prince)生成 PDF 文件。
For more information see:
有关更多信息,请参阅:

