Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming a Filter/Backend to 'Print to PDF' with CUPS from any Mac OS X application

Okay so here is what I want to do. I want to add a print option that prints whatever the user's document is to a PDF and adds some headers before sending it off to a device.

I guess my questions are: how do I add a virtual "printer" driver for the user that will launch the application I've been developing that will make the PDF (or make the PDF and launch my application with references to the newly generated PDF)? How do I interface with CUPS to generate the PDF? I'm not sure I'm being clear, so let me know if more information would be helpful.

I've worked through this printing with CUPS tutorial and seem to get everything set up okay, but the file never seems to appear in the appropriate temporary location. And if anyone is looking for a user-end PDF-printer, this cups-pdf-for-mac-os-x is one that works through the installer, however I have the same issue of no file appearing in the indicated directory when I download the source and follow the instructions in the readme. If anyone can get either of these to work on a mac through the terminal, please let me know step-by-step how you did it.

like image 494
Katie Avatar asked Jun 25 '12 15:06

Katie


Video Answer


1 Answers

The way to go is this:

  • Set up a print queue with any driver you like. But I recommend to use a PostScript driver/PPD. (A PostScript PPD is one which does not contain any *cupsFilter: ... line.):

  • Initially, use the (educational) CUPS backend named 2dir. That one can be copied from this website: KDE Printing Developer Tools Wiki. Make sure when copying that you get the line endings right (Unix-like).

  • Commandline to set up the initial queue:

    lpadmin \
        -p pdfqueue \
        -v 2dir:/tmp/pdfqueue \
        -E \
        -P /path/to/postscript-printer.ppd
    
    The 2dir backend now will write all output to directory /tmp/pdfqueue/ and it will use a uniq name for each job. Each result should for now be a PostScript file. (with none of the modifications you want yet).
  • Locate the PPD used by this queue in /etc/cups/ppd/ (its name should be pdfqueue.ppd).

  • Add the following line (best, near the top of the PPD):

    *cupsFilter: "application/pdf  0  -"
    (Make sure the *cupsFilter starts at the very beginning of the line.) This line tells cupsd to auto-setup a filtering chain that produces PDF and then call the last filter named '-' before it sends the file via a backend to a printer. That '-' filter is a special one: it does nothing, it is a passthrough filter.
  • Re-start the CUPS scheduler:

    sudo launchctl unload /System/Library/LaunchDaemons/org.cups.cupsd.plist
    sudo launchctl load /System/Library/LaunchDaemons/org.cups.cupsd.plist
  • From now on your pdfqueue will cause each job printed to it to end up as PDF in /tmp/pdfqueue/*.pdf.

  • Study the 2dir backend script. It's simple Bash, and reasonably well commented.

  • Modify the 2dir in a way that adds your desired modifications to your PDF before saving on the result in /tmp/pdfqueue/*.pdf...


Update: Looks like I forgot 2 quotes in my originally prescribed *cupsFilter: ... line above. Sorry!

like image 100
Kurt Pfeifle Avatar answered Oct 16 '22 17:10

Kurt Pfeifle