How to save PDF as image in the clipboard when use preview to open this PDF file?

when i double click PDF file, I wanna save as image in the system default clipboard automatically, instead of using screenshot to copy to CB.

How can I do it?

Hey Michael,

You can't.

You can't piggyback onto the double-click event.

You can work with a selected file in the Finder, or any given file-path, and convert it/them.

I tested sips on my macOS 10.12.4 system, and it will convert an input-file-type to an output-file-type. I don't see a way to read the conversion directly into the clipboard though.

I was only able to get sips to convert page 1 of the pdf, and offhand I don't see a means to work with multiple pages.

I suspect the command-line-tool ImageMagick has significantly more sophisticated options than sips and will probably handle multi-page PDFs.

So — the upshot of all this is that your task is possible — but it's not real simple.

Why don't you post a more detailed description of your workflow.

For instance — you don't tell us whether the PDF will always be one page or whether it can be multi-page.

You don't tell us what kind of image data you want on the clipboard.

You don't tell us what the destination of that data is.

Etcetera...

-Chris

Thank you for your reply.

Normally my PDF is only ONE page. Currently, my workflow is to open pdf in preview app, then I use system screenshot shortcuts to copy this PDF as image in the system clipboard, then I will paste it in one appplication to my customer. Because this app is unable to input PDF files, so I have to convert to image to send.

Hey Michael,

Okay, give this a try.

It will copy page 1 of the selected PDF file in the Finder to the Clipboard as a PNG you can paste.

Convert Selected PDF in the Finder to a PNG on the Clipboard.kmmacros (4.7 KB)

This is a trifle kludgy, but it works without installing any 3rd party stuff.

I've got some feelers out about improving it, and we'll see what comes of them.

-Chris

Hey Michael,

I should have asked this first – what happens when you copy the PDF in the Finder with Cmd-C and then paste into your app?

And what app are you pasting into?

-Chris

Here is a solution that works with ghostscript:

‌

set tmpDir to path to temporary items from user domain
set tmpPath to tmpDir & (random number) & ".png" as text

tell application "Finder" to set theItem to the selection as alias
set theItem to POSIX path of theItem

do shell script "/usr/local/bin/gs -sDEVICE=png16m -r200 -dFirstPage=1 -dLastPage=1 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -o" & space & quoted form of POSIX path of tmpPath & space & quoted form of theItem

set the clipboard to (read alias tmpPath as «class PNGf»)

‌

  1. Select a PDF in the Finder
  2. Launch the script

By default the first page of the selected PDF is converted to a temporary file and from there copied as PNG to the clipboard. You can set the page number and other things in the script. (See “Options” below.)


Here is the script wrapped into a KM macro with a hotkey trigger:

PDF as Image to Clipboard.kmmacros (2.4 KB)


Requirements

ghostscript must be installed on your computer. I you don’t have it already you can easily install it via Homebrew or MacPorts.

Options

The ghostscript command line in the script has a few options you can set:

-r200

The resolution in ppi. Set it higher for better print quality, or lower if the images are only for screen viewing and don’t contain text. Affects processing speed and of course data size.

-sDEVICE=png16m

This produces compressed PNG data. Other interesting options are: png256 (pretty small file size, but reduced color palette), tiff24nc (needed for multi-page images, see below), jpeg (for photo-like content).

Details here.

-dFirstPage=1 -dLastPage=1

Page range. Set it to “2” and “2” if you need page 2, etc. (It is also possible to convert several PDF pages into one multi-page tiff, but not every app will display all pages.)

-dTextAlphaBits=4 -dGraphicsAlphaBits=4

Antialiasing. You can lower these values (or remove both switches entirely) for a smaller clipboard size, but visual quality will suffer. (Especially text will become jaggy at lower resolutions.)

Other options

For more settings and information see the File Format documentation and the Usage documentation.

1 Like