如何以编程方式从 OSX 上的任何文档生成 PDF?
我正在开发一个 OSX 项目,用户可以(从任何应用程序中)选择我需要从中生成 PDF 的文档集合。 标准 Macintosh 打印对话框有一个 PDF 按钮,其中包含许多与 PDF 相关的命令,包括“另存为 PDF...”。 但是,我需要生成 PDF 文件而不需要用户交互。 我理想地希望它适用于任何类型的文档。
以下是我迄今为止探索过的选项:
- 自动化操作。 Automator 有一个 PDF 库,但它提供了处理 PDF 文件的操作,而不是生成它们。 有一个 Finder 操作可以打印任何文件,但只能打印到真正的打印机。
- AppleScript。 某些应用程序能够生成 PDF 文件(例如,如果您将“在“test.pdf”中保存文档””发送到 Pages,它将生成 PDF(但这仅适用于 Pages - 我需要支持任何类型的文档)自
- 用户与打印列表中的额外打印机混淆的想法。
定义打印机。我可以创建一个虚拟打印机驱动程序,然后使用自动操作,但我不喜欢让 应用程序就像用户正在执行以下步骤:
- 执行 Cmd-P(打开打印对话框)
- 单击“PDF”按钮
- 选择“另存为 PDF...”(菜单中的第二项)
- 在保存对话框中键入文件名
- 单击“保存”
如果这是最好的方法(是吗?),那么真正的问题是:如何将 UI 事件发送到外部应用程序(击键、鼠标事件、菜单选择)
更新: 只是为了?澄清一点:我需要转换为 PDF 的文档是由其他应用程序创建的文档,例如,用户可能会选择 Word 文档、Numbers 电子表格、OmniGraffle 绘图或网页。 。 共同点是每个文档都有一个关联的应用程序,并且该应用程序知道如何打印它(并且 OSX 知道如何将打印输出呈现为 PDF 文件)。
因此,Cocoa Dev Central 中的示例没有帮助,因为它们是关于从我的应用程序生成 PDF。
I'm working on a project for OSX where the user can pick a collection of documents (from any application) which I need to generate PDF's from. The standard Macintosh Print dialog has a PDF button which has a number of PDF-related commands including "Save as PDF...". However, I need to generate the PDF file without requiring user interactions. I ideally want this to work with any type of document.
Here's the options I've explored so far:
- Automator actions. There's a PDF library for Automator but it provides actions for working with PDF files, not generating them. There's a Finder action for printing any file but only to a real printer.
- AppleScript. Some applications have the ability to generate PDF files (for instance, if you send 'save doc in "test.pdf"' to Pages it will generate a PDF (but this only works for Pages - I need support for any type of document).
- Custom Printer. I could create a virtual printer driver and then use the automator action but I don't like the idea of confusing the user with an extra printer in the print list.
My hope is that there's some way to interact with the active application as if the user was carrying out the following steps:
- Do Cmd-P (opens the print dialog)
- Click the "PDF" button
- Select "Save as PDF..." (second item in menu)
- Type in filename in save dialog
- Click "Save"
If that's the best approach (is it?) then the real problem is: how do I send UI Events to an external application (keystrokes, mouse events, menu selections) ?
Update: Just to clarify one point: the documents I need to convert to PDF are documents that are created by other applications. For example, the user might pick a Word document or a Numbers spreadsheet or an OmniGraffle drawing or a Web Page. The common denominator is that each of these documents has an associated application and that application knows how to print it (and OSX knows how to render print output to a PDF file).
So, the samples at Cocoa Dev Central don't help because they're about generating a PDF from my application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为您可以使用 applescript 打开文档,然后使用 applescript UI 脚本 调用打印菜单。
例如 :
I think you could use applescript to open a document and then use applescript UI scripting to invoke print menu.
For example :
看一下名为 CUPS-PDF
它是 OS X 的虚拟打印机,它执行通过普通打印机打印时“另存为 PDF”方法执行的操作,只不过通过它的每个打印作业都会生成 pdf 输出。
安装后,您可以使用 lp 命令创建 shell 或 AppleScript。
例如,一旦设置了虚拟打印机,您就可以打印 test.txt 并将其自动保存为 pdf。 要使用 AppleScript 执行此操作,您将使用以下代码:
CUPS-PDF 应用程序将所有输出保存到 /Users/Shared/CUPS-PDF。 我不确定您是否可以更改该路径,但您可以在脚本中检索该文件并移动它。
但有一些注意事项。
首先,lp命令无法打印.doc文件。 我认为还有一些其他第三方应用程序可以让您执行此操作。
其次,CUPS-PDF 应用程序在系统首选项的打印机窗格中显示其名称中包含连字符,但 CUPS 将队列名称显示为包含下划线。 因此,在命令行上您需要引用 CUPS 队列名称,即带下划线的 CUPS_PDF。
即使您发现通过 lp 命令构建脚本不是很有用
并且仍然想要涉及 GUI 脚本,那么拥有虚拟打印机应该可以节省您一些步骤。
Take a look at a program called CUPS-PDF
It is a virtual printer for OS X which does what the "Save As PDF" method does when print through your normal printer except every print job passed through it results in a pdf output.
Once you install it then you could create shell or AppleScripts using the lp command.
For example, once the virtual printer is setup you could print test.txt and have it automatically save as a pdf. To do this using an AppleScript you would use the following code:
The CUPS-PDF app saves all output to /Users/Shared/CUPS-PDF. I am not sure if you can change that path but you could retrieve the file in your script and move it.
There are a few caveats though.
First, the lp command cannot print .doc files. I think there are some other third party apps which will allow you to do this though.
Second, the CUPS-PDF app shows in the Printer pane of System Preferences as having the hyphen in its name but CUPS shows the queue name as having an underscore. So, on the command line you need to refer to the CUPS queue name which is CUPS_PDF with an underscore.
Even if you don't find it very useful to build a script via the lp command
and still want to involve GUI scripting then having a virtual printer should save you some steps.
你可以用这样的杯子
you could use cups like this
我为此在 bash 中创建了一个别名:
I have created an alias in bash for this:
我在 Automator 的帮助下输入了下面的代码(记录一个动作,然后将特定动作拖出“Watch Me Do”窗口以获得 Applescript)。 如果您想从 Safari 以外的应用程序打印 PDF,您可能必须运行相同的过程并围绕“打印”对话框调整此 Applescript,因为每个程序可能有不同的打印 GUI。
I typed up the code below with the assistance of Automator (recording an action, and then dragging the specific action out of the "Watch Me Do" window in order to get the Applescript). If you want to print a PDF from an application other than Safari, you might have to run through the same process and tweak this Applescript around the Print dialogue, since each program might have a different Print GUI.
“/系统/库/打印机/库/./convert”
在 MacOS 的更高版本中已被删除。
您现在可以使用 sip。 AppleScript 示例:
执行shell脚本:
sips -s format pdf Filename.jpg --out Filename.pdf
"/System/Library/Printers/Libraries/./convert"
has been removed in later version of MacOS.
You can use sips now. AppleScript example:
do shell script:
sips -s format pdf Filename.jpg --out Filename.pdf