4

I am trying to print a file (a pdf), without intervention. Not Automator specific, I can deal with applescript or terminal or whatever. The specifics of what I want are:

  • Print to specific Printer
  • Print to specific Page Size (Non-Standard)
  • Auto-Rotated (Optional)
  • Scale to Fit, Print Entire Page (Ends up being 109 to 112%. 110% works)

I can do this manually through Preview's print menu, only the page size is non-default, but I want to automate it, drag and drop, somehow.

Things I tried:

  1. If I print from Finder, it uses Preview, but it's blown out of size, and wrong Page size.
  2. If I drag it onto the printer in ~/Library/Printers/printername, it's like using LP, but without the options.
  3. Applescript's printing options are poorly documented.
  4. Automator's "Print Finder Items" it's like #1.
  5. Automator's "Print Images" is like Preview but "Scale to Fit" does not work like Preview does, I can't choose the page size, and "Auto-Rotate" doesn't work at all.
  6. If I print from terminal via LP or LPR, I can set page size and orientation/fit-to-page, but I can't scale it to 110%. This is the closest thing to what I want.

How can I do this? It shouldn't be so hard, I want to do what the GUI can, not like I want extra features.

Worst case, how can I emulate mouse or keyboard control of Preview's print menu? All that would be needed is to 1. open file, 2. Bring up print menu, 3. switch to a preset or page size, as all the other options including scaling are default., 4. press print/enter.

1
  • For emulating mouse or keyboard control (and a slew of other features you'll wonder how you ever lived without) you might check out Better Touch Tool.
    – Wildcard
    Mar 18, 2016 at 3:47

2 Answers 2

3
+50

Let's split the problem into two parts: scaling and printing.

Scaling

You can scale a PDF on the command line. Consider creating a temporary, scaled, PDF to print.

See scaling PDF content and page dimensions from command line for suggested approaches to this. The highest voted answer suggests using cpdf with a command like:

cpdf -scale-page "1.1 1.1" in.pdf -o out.pdf

An alternative tool is sips. This tool is included with OS X and is surprisingly capable.

Printing

As you have discovered, OS X's printing system is the Common Unix Printing System (CUPS). CUPS includes the command line tool lpr which can accept all the settings and options you need.

For printing the PDF via the command line see:

To list the available printers, use:

lpstat -p -d

To print a PDF file to the default printer, use:

lpr file.pdf
4
  • might it be possible to use a pipe and do the whole shebang at once? like: cpdf -scale-page "1.1 1.1" in.pdf | lpr ? Mar 11, 2016 at 22:51
  • With cpdf that might be possible. With sips it would have to go via a file. I suspect a short bash script or even an AppleScript droplet would wrap the command up well. Mar 12, 2016 at 11:01
  • Considering CPDF can do cropping and scaling in a single command then piping to LP, this should be the best for me. I'll need to test it out.
    – cde
    Mar 22, 2016 at 0:10
  • 1
    You can install RWTS PDFwriter with brew install rwts-pdfwriter (requires macOS Big Sur, to install on macOS Catalina install Version 1.01). Jun 27 at 2:39
0

What I ended up doing was using open and osascript along with a tool pdfcrop from MacRumor's forum user Oligarch (Wayback Machine backup to archive)

./pdfcrop /path/to/file.pdf -u in -p 6.68 1.82 -a tr -o 0.14 0.23 > ./0.pdf && \
./pdfcrop /path/to/file.pdf -b m -u in -p 3.30 1.82 -a tr -o 0.14 0.23 > ./1.pdf && \
./pdfcrop /path/to/file.pdf -b m -u in -p 3.38 1.78 -a tr -o 3.43 0.24 > ./2.pdf && \
open ./1.pdf ./2.pdf && \
osascript -e 'tell application "Finder" to delete POSIX file "'/path/to/file.pdf'"' -e 'tell application "Finder" to delete POSIX file "'./1.pdf'"' -e 'tell application "Finder" to delete POSIX file "'./2.pdf'"'
  1. It takes the original file, and creates three copies. One with the pdf cropped to size (crop box, none destructive) that I keep, and two cropped to size (media box) that are temporary.
  2. It then opens them with Preview, as two pages/files in the same window.
  3. It then tells Finder via Applescript to delete the temporary files, in a Finder, move to the trash manner. Same as if I ⌘-Delete the files.
  4. Once in Preview, I can ⌘-P to print both files. Only thing I need to do is change the paper size, Preview handles the scaling.

Obviously, it doesn't do everything I want, but it helps reducing the amount of manual cropping and going back and forth to make sure everything I need printed is printed.

Since CPDF can scale and crop, It should be easier to do all via command line. I just need to test it out.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .