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

前提是你 提交于 2019-11-28 04:13:34

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!

I really wish I could accept two answers because I don't think I could have done this without all of @Kurt Pfeifle 's help for Mac specifics and just understanding printer drivers and locations of files. But here's what I did:


  1. Download the source code from codepoet cups-pdf-for-mac-os-x. (For non-macs, you can look at http://www.cups-pdf.de/) The readme is greatly detailed and if you read all of the instructions carefully, it will work, however I had a little trouble getting all the pieces, so I will outline exactly what I did in the hopes of saving someone else some trouble. For this, the directory with the source code is called "cups-pdfdownloaddir".

  2. Compile cups-pdf.c contained in the src folder as the readme specifies:

    gcc -09 -s -lcups -o cups-pdf cups-pdf.c

    There may be a warning: ld: warning: option -s is obsolete and being ignored, but this posed no issue for me. Copy the binary into /usr/libexec/cups/backend. You will likely have to the sudo command, which will prompt you for your password. For example:

    sudo cp /cups-pdfdownloaddir/src/cups-pdf /usr/libexec/cups/backend

    Also, don't forget to change the permissions on this file--it needs root permissions (700) which can be changed with the following after moving cupd-pdf into the backend directory:

    sudo chmod 700 /usr/libexec/cups/backend/cups-pdf

  3. Edit the file contained in /cups-pdfdownloaddir/extra/cups-pdf.conf. Under the "PDF Conversion Settings" header, find a line under the GhostScript that reads #GhostScript /usr/bin/gs. I did not uncomment it in case I needed it, but simply added beneath it the line Ghostscript /usr/bin/pstopdf. (There should be no pre-cursor # for any of these modifications)

    Find the line under GSCall that reads #GSCall %s -q -dCompatibilityLevel=%s -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile="%s" -dAutoRotatePage\ s=/PageByPage -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dPDFSETTINGS=/prepress -c .setpdfwrite \ -f %s Again without uncommenting this, under this I added the line GSCall %s %s -o %s %s

    Find the line under PDFVer that reads #PDFVer 1.4 and change it to PDFVer, no spaces or following characters.

    Now save and exit editing before copying this file to /etc/cups with the following command

    sudo cp cups-pdfdownloaddir/extra/cups-pdf.conf /etc/cups

    Be careful of editing in a text editor because newlines in UNIX and Mac environments are different and can potentially ruin scripts. You can always use a perl command to remove them, but I'm paranoid and prefer not to deal with it in the first place.

  4. You should now be able to open a program (e.g. Word, Excel, ...) and select File >> Print and find an available printer called CUPS-PDF. Print to this printer, and you should find your pdfs in /var/spool/cups-pdf/yourusername/ by default.


*Also, I figured this might be helpful because it helped me: if something gets screwed up in following these directions and you need to start over/get rid of it, in order to remove the driver you need to (1) remove the cups-pdf backend from /usr/libexec/cups/backend (2) remove the cups-pdf.conf from /etc/cups/ (3) Go into System Preferences >> Print & Fax and delete the CUPS-PDF printer.


This is how I successfully set up a pdf backend/filter for myself, however there are more details, and other information on customization contained in the readme file. Hope this helps someone else!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!