Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.4: Create a virtual PDF printer' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Create a virtual PDF printer
Authored by: Michael Longval on Sep 06, '05 11:44:33PM
How to setup a virtual printer on MacOSX 10.4 (Tiger).

I decided to condense the Virtual Printer HOW-TO on macosxhints.com, since it spans many postings and deals with both 10.3 and 10.4, and therefor was a little too confusing at first.

NB: I have installed and tested this on a updated 10.4.2 system (Powerbook), and it works as advertised.

(Please excuse my liberal use of cut and paste from the previous postings.)

Why create a virtual printer?I know that you can always select "Save as PDF" when printing, but I had a situation where I wanted to be able to print to PDF without having to go through the Print dialog. Also sometimes the application disables "Save as PDF".

Here's what you need to do (in a Terminal):

- Copy the included script to:
         /usr/libexec/cups/backend/pdf

- Make it executable by everyone: 

     % sudo chmod a+x /usr/libexec/cups/backend/pdf

- You must restart the CUPS daemon, so it finds your new backend and model :  
    % sudo killall -HUP cupsd

- Now you can configure your virtual printer using the Printer Setup Utility. 

- In the Printer Setup Utility click the 'Add' button.  
- In the 'Printer Browser' window that opens you hold down the key while clicking 'More Printers...'.

- Then, finally, you will see the 'Advanced' option in the drop-down menu at the top.

- After you've selected 'Advanced' you can select 'PDF Writing' from the 'Device:' drop-down menu. (This comes from the "pdf" script that you added to /usr/libexec/cups/backend/)

- For "Device Name:", give it something descriptive: like PDFWriter or VPrinter.

- For the device URI, use "pdf://tmp" , it is a good choice as it gets cleaned out automatically when you restart.If you want to put the files somewhere else, specify it.

- For the printer model leave it as Generic.(I deleted the reference to using the Adobe distiller driver that was present in the original posting.)

- Click on Add and you are ready to go! 

The script creates a log file (/tmp/pdf.log) each time it prints, which might be helpful if something goes wrong.

The script checks to see if the output file exists, it creates a new name. ( This was important because printing multiple worksheets out of excel causes each worksheet to be seen as a unique print job, and each subsequent sheet overwrote the previous one.)

Remember your files will be printed to /tmp and will therefor be lost if you don't copy them to somewhere safe before you restart the system.(/tmp get cleaned out at system restart.)

In OSX 10.4 (Tiger):Create a SmartFolder on the Desktop, that searches /tmp for PDFs and call it "PDFWriter_spool".

Your printed files will always be easy to reach this way.

Here is the latest version of the script:(version as of July 16 2004 by n9yty)


#!/bin/sh
#
# Michael Goffioul
# Updated by P T Withington for Mac OS X
# Updated by Richard Bronosky
# Updated by Steve Palm (N9YTY) - case insensitive URI, unique output files

LOGFILE=/tmp/PDF.log
GSBIN=/usr/bin/pstopdf
FILENAME=

echo "Script:           $0" > $LOGFILE
echo "Executable:       $GSBIN" >> $LOGFILE
echo "job:              $1" >> $LOGFILE
echo "user:             $2" >> $LOGFILE
echo "title:            $3" >> $LOGFILE
echo "num-copies:       $4" >> $LOGFILE
echo "options:          $5" >> $LOGFILE
echo "filename:         $6" >> $LOGFILE

# case of no argument, prints available URIs
if [ $# -eq 0 ]; then
  if [ ! -x "$GSBIN" ]; then
    exit 0
  fi
  echo "direct PDF \"Unknown\" \"PDF Writing\""
  exit 0
fi

# case of wrong number of arguments
if [ $# -ne 5 -a $# -ne 6 ]; then
  echo "Usage: PDF job-id user title copies options [file]"
  exit 1
fi

# get PDF directory from device URI, and check write status
URI_PRE=`echo $DEVICE_URI | cut -c 1-3 | tr "[a-z]" "[A-Z]"`
URI_BODY=`echo $DEVICE_URI | cut -c 4-`
DEVICE_URI="${URI_PRE}${URI_BODY}"
echo "Device URI: $DEVICE_URI" >> $LOGFILE

PDFDIR=${DEVICE_URI#PDF:}
if [ `echo $PDFDIR|cut -c1-3` = //~ ]; then
  PDFDIR=/Users/$2`echo $PDFDIR|cut -c4-`
  # This step added by Richard Bronosky to allow referencing the users home directory
fi
if [ ! -d "$PDFDIR" -o ! -w "$PDFDIR" ]; then
  echo "ERROR: directory $PDFDIR not writable"
  exit 1
fi

echo "PDF directory:    $PDFDIR" >> $LOGFILE

# generate output filename
OUTPUTFILENAME=
if [ "$3" = "" ]; then
  OUTPUTFILENAME="$PDFDIR/unknown.PDF"
else
  OUTPUTFILENAME="$PDFDIR/${3//[^[:alnum:]]/_}.PDF"
fi

FBASE=`basename ${OUTPUTFILENAME} .PDF`
typeset -i I=1
while [ -e $OUTPUTFILENAME ]
do
   OUTPUTFILENAME="${PDFDIR}/${FBASE}_$I.PDF"
   I=$I+1
done

echo "Output file name: $OUTPUTFILENAME" >> $LOGFILE

# run ghostscript
if [ $# -eq 6 ]; then
  $GSBIN $6 -o $OUTPUTFILENAME >> $LOGFILE
else
  $GSBIN -i -o $OUTPUTFILENAME >> $LOGFILE
fi

# modify ownership and permissions on the file
#  - world readable
#  - owns to user specified in argument
chmod a+r $OUTPUTFILENAME
if [ "$2" != "" ]; then
  chown $2 $OUTPUTFILENAME
fi

exit 0



[ Reply to This | # ]
10.4: Create a virtual PDF printer
Authored by: johnpateman on Oct 29, '05 07:00:40AM

This is a great hint and works really well. I have one problem though - the owner of the file stubbornly remains as root. I have checked through the tail end of the code and inspected the logfile but no avail. The log file shows $2 as my corrrect username. The chmod step works fine, $2 is obviously !="" so can't understand while file remains root:staff. This is really relativly minor but a real annoyance! I see this problem has been reported previously.
Any advice?



[ Reply to This | # ]
10.4: Create a virtual PDF printer
Authored by: Dezro on Mar 14, '06 08:44:49AM

This is a little late, but change the chown line to:
/usr/sbin/chown $2 $OUTPUTFILENAME

Apparently /usr/sbin isn't in the printer thing's $PATH.



[ Reply to This | # ]
10.4: Create a virtual PDF printer
Authored by: después on Mar 27, '07 01:00:28PM
I changed
chown $2 $OUTPUTFILENAME
to
/usr/sbin/chown $2 $OUTPUTFILENAME
and it worked. (Better than giving rights to everyone.)

[ Reply to This | # ]
10.4: Create a virtual PDF printer
Authored by: zacht on Apr 04, '06 10:04:20PM

Not working in 10.4.5.



[ Reply to This | # ]
10.4: Create a virtual PDF printer WORKS
Authored by: mizraith on Jun 15, '06 06:20:49PM
Works like a charm on latest 10.4.x as of June, 06.

MINOR PROBLEM: Even though, I made sure to edit the chown line of the above posting to add the path (/usr/sbin/chown $2 $OUTPUTFILENAME) the doc is still owned by root. Anybody good at scripts who can tell me how to change that chown line or chmod line?
WORKAROUND: Open the PDF, save it to another folder somewhere. Create a chron job to clean the originals nightly.

+ Also, got it to work from my Windoze XP box. Here's what I did:

- After following the above instructions to the letter (well, almost, I did set the print path to inside a SAMBA shared directory), I opened up the Control Panel and verified that the PDFWriter was shared (under Printers & Faxes).

- I also went to the CUPS control panel (browse to 127.0.0.1:631) to verify that the PDFWriter was there.

- On WinXP, I clicked on Printers and went through the Add Printer 'wizard', selected Network Printer.

- Entered my server's IP address:
http://ipaddress:631/printers/PDFWriter

- Selected print driver by GENERIC and used the "MS Publisher Color Printer" driver.

Works like a freakin charm! Thanks to all those above who have been hammering this one out.


[ Reply to This | # ]
10.4: Virtual PDF Printer (ownership)
Authored by: mizraith on Jun 15, '06 06:38:54PM

Duh, To answer my own above "PROBLEM"

Simple enough to make ownership for all, change the chmod line (by adding "+w") to:

chmod a+r+w $OUTPUTFILENAME

Lets users grab their file and clear out the queue on their own.
-Red



[ Reply to This | # ]