Search code examples
macosdelphipdfprint-preview

How to access Print dialog's 'Open PDF in Preview' in os x programmatically


I am building a Delphi application which opens an image and its metadata and prints it. For the Windows version I build a form to generate the PrintPreview, but in Mac I can use the Print Dialog's 'Open PDF in Preview' instead. When I click on it, a PDF file is generated and I can see it, its OK. The problem is I want to access this option directly from a button, so when the button is clicked, the PDF in Preview is opened and the user does not have to open the Print Dialog, then click the 'PDF' and then select 'Open PDF in Preview'. How can I do this?

I read about using Automator, apple scripts etc, but I still can't find it. Is there any path this generated PDF Preview is stored, so maybe I can open it from there?...

TIA


Solution

  • Possible duplicate of Using Automator or Applescript or both to recursively print documents to PDF but I'll answer anyways.

    To answer your question directly see the question I linked to. Basically you need to use System Events from applescript to accomplish that exactly


    However, there's a quicker solution using /usr/sbin/cupsfilter. Check the man page for more.

    You can call cupsfilter <an-image-file> and you'll get a PDF on stdout, courtesy of OSX's printing daemon. It looks quite configurable but I just learned about it a while ago.

    If you want this to open for the user you can save it in a nice place or you can do it the one-shot way and do cupsfilter <your-image> | open -f -a "Preview" to open the PDF right up.