BSDnexus
10Oct/10Off

Debian cups-pdf

The Common UNIX Printing System (CUPS) has long been the standard of printing and print drivers for Linux. Having a PDF printer can be extremely useful - so here's how to set one up in Debian.

First, you have to install the CUPS PDF print driver

$ aptitude install cups-pdf

Next, we need to actually configure the printer itself for use. In CUPS this can be easily done via a URL and a browser of your choice (even a text based browser). Simply head over to http://localhost:631/ and in the following screen select the "Administration" option

CUPS administration option

CUPS default screen

Then select to "Add Printer"

CUPS printer administration

CUPS printer administration

How you configure the printer name and details is up to you and your setup, but it is easiest to simply use "PDF" as the printer name

CUPS add printer

CUPS add printer

Next select the "Virtual PDF Printer" as the device

CUPS select device

CUPS select device

For the make and manufacturer of printer select Generic

CUPS make and manufacturer

CUPS make and manufacturer

Finally, for the model select the "Generic CUPS-PDF Printer(en)" option

CUPS model

CUPS model

At this point you will now have a PDF printer. Some customisation is possible in the file /etc/cups/cups-pdf.conf - for example, the default location for creating PDF files is in a PDF directory in the user's home directory as identified by this entry:

Out ${HOME}/PDF

Clearly you can adjust this to suit your needs. To demonstrate the post processing options available, uncomment the PostProcessing option and alter to read as follows

PostProcessing /usr/local/bin/cups-pdf-renamer

Edit the file /usr/local/bin/cups-pdf-renamer to look as follows:

1
2
3
4
5
6
7
#!/bin/bash
 
FILENAME=`basename $1`
DIRNAME=`dirname $1`
DATE=`date +”%Y-%m-%d_%H:%M:%S”`
 
mv $1 $DIRNAME/$DATE”.pdf”

Make sure the file is executable

$ chmod +x /usr/local/bin/cups-pdf-renamer

Essentially this will rename the file to a date and time stamp. Clearly you can alter this to suit your needs. This example was taken from here

27Sep/10Off

Anonymous Windows PDF generation via SAMBA

On Ubuntu I'm completely used to the idea of printing out a PDF document of what I want/need. It saves on paper and allows me to keep digital copies; it can also prove indispensable if I need to send a copy via email.

At work, on Microsoft Windows, I have struggled with trying to do this. The only products available for PDF generation appear to be commercial. Through some tinkering with a Ubuntu server in the office I setup a PDF printer for all to use anonymously. Here's how...

Configuring Samba and PDF

The important thing to remember is that PDF printer on the server produces PDF files locally, and therefore access to the produced PDFs must be via SAMBA too.

Samba is usually installed by default, but if not, run:

$ sudo apt-get install samba

Changes need to be made to the config file /etc/samba/smb.conf to allow printing. Uncomment/add the following entries:

printing = cups
printcap name = cups

We now need a location that anybody can read or write to (which is where our PDFs will be placed for pickup). In this example I will use the /tmp directory. To do this we add the following entry to the samba config file:

[tmp]
 comment = Samba server's tmp directory
 locking = no
 path = /tmp
 guest ok = yes
 create mask = 0666
 read only = no

Restart Samba

$ sudo /etc/init.d/samba restart

You may already have the PDF printer installed (as cups is installed by default) however, if you don't, simply run:

$ sudo apt-get install cups-pdf

We now need to tell the cups-pdf printer where to otput its files. System users have their files printed to the folder ~/PDF. This has implications for samba users too. If you mount samba shares as a system user on the server then your PDFs will be printed to that user's ~/PDF directory. This may be what you want.

The file we need to alter is /etc/cups/cups-pdf.conf, specifically the following two entries:

Out ${HOME}/PDF

The above entry details where PDFs are created for system users. Altering this affects all system users AND samba users who use system user credentials to mount drives. Change this to suit your needs. Next is the AnonDirName entry, change it to:

AnonDirName /tmp

Now any anonymous user will have their PDFs placed in /tmp to pickup via samba. Restart CUPS - on Hardy (and earlier?) this appears to be:

$ sudo /etc/init.d/cupsys restart

but on Jaunty (and later?) it may be:

$ sudo /etc/init.d/cups restart

Configuring Windows

The example here has been completed on a Windows Vista machine, but should be similar for other versions. First you need to browse to the computer:

Windows printers

Windows printers

and double-click the "PDF" printer, which will start to install the printer. A printer driver error message will be displayed indicating that the printer driver could not be found:

Windows printer driver error message

Windows printer driver error message

This is not a problem. Simply click "Ok" to continue the install. You will now need to select a printer driver to use with the printer. The printer works with PostScript documents so I selected a HP PostScript printer driver to use with it, the 'HP LaserJet Series 1200 PS' - CafeNinja tested with the 'IBM 4079 Color Jetprinter PS' driver and achieved successful results

Windows add printer wizard

Windows add printer wizard

Once the driver has been installed the printer's queue will be displayed. Don't worry about the "Access denied, unable to connect" message - this has never stopped it working for me:

Windows printer queue

Windows printer queue

From the printer queue window select Printer > Properties to get to the properties window. Then select to "Print Test Page" - closing any "sent test page" window box that may appear

Windows printer properties

Windows printer properties

Now browse to the /tmp directory and collect the Windows (recognisable) printed PDF. Take note that the output PDF is in color too!

Windows test page PDF

Windows test page PDF

Caveats
A couple of things to remember...

  1. The PDF gets its name from the file that was being printed - this could lead to accidental overwrites
  2. The files do not get auto-deleted so you may wish to setup a cron job to do this periodically
16Sep/10Off

Concatinating PDF files

Sometimes there is a need to amalgamate many PDFs into a single PDF. Ghostscript is fully capable of achieving this and is usually available by default.

To concatenate any number of PDFs, cd to the appropriate directory containing the PDF files and issue the following command:

$ gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=finished.pdf -dBATCH *.pdf

Note that they are added to the finalised PDF in the order given - so "*.pdf" may not result in the order you would like, in which case you will need to manually specify each file in the desired order.

A quick breakdown to the options:

-dBATCH  :  Exit once Ghostscript processes the PDF files, otherwise Ghostscript will keep running
-dNOPAUSE  :  tells Ghostscript to process each page without pausing for user interaction
-sDEVICE=pdfwrite  :  tells Ghostscript to use its built-in PDF writer to process the files
-sOutputFile=finished.pdf  :  tells Ghostscript to save the output to the named file, in this case "finished.pdf"

Additional options you may want:

-sPAPERSIZE=letter  :  sets the paper size to American sized "letter"
-q  :  "quiet" mode, stops Ghostscript displaying messages while it works