Installation of the cups-pdf driver and virtual printer
1.1 Install the cups-pdf package:
# emerge cups-pdf
1.2 Launch a Web browser and enter http://localhost:631/ in the Address bar to display the CUPS Printer Manager.
1.3 Click on ‘Administration’ to call up the
Administration page, then click ‘Add Printer’. If prompted, enter the
username ‘root’ (without the quotes) and the root user’s password.
1.4 Select ‘CUPS-PDF (Virtual PDF Printer)’ in the list of local printers, and click ‘Continue’.
1.5 Enter a name, decription and location for the
virtual printer. For example, I entered ‘Virtual_PDF_Printer’, ‘Virtual
PDF Printer’ and ‘Mesh Edge DX’ (without the quotes), respectively. Then
click ‘Continue’.
1.6 Select ‘Generic’ in the ‘Make:’ box, and click ‘Continue’.
1.7 Select ‘Generic CUPS-PDF Printer (w/ options)
(en)’ in the ‘Model:’ box, and click ‘Add Printer’. The virtual printer
should now be available for you to use.
1.8 When you come to print from an application, if you select Virtual_PDF_Printer from the list of available printers the PDF file will be saved in the directory /var/spool/cups-pdf/<your username>/. Note that the directory will be created automatically the first time you ‘print’ to PDF.
1.9 Optionally, you could create a shortcut (Desktop
Config File) on your Desktop to open the directory mentioned in Step 1.8
above.
1.10 Optionally, instead of Step 1.9 you could edit the file /etc/cups/cups-pdf.conf as root user to get cups-pdf to ‘print’ PDF documents to your Desktop instead of in /var/spool/cups-pdf/<your username>/, by changing the line:
#Out /var/spool/cups-pdf/${USER}
|
to:
2.0 How to display a dialog box prompting for the directory and file name of the PDF file
If you prefer to be prompted for a file name so that you can save the
PDF file wherever you want and give it any name you want, perform the
steps below instead of Steps 1.9 or 1.10 above. Either perform
the steps in 2.1 (Zenity) or the steps in 2.2 (KDialog). Then perform
the steps in 2.3, which apply in both cases.
2.1 Zenity (GTK+)
2.1.1 Install Zenity:
# emerge zenity
2.1.2 Check it is installed:
# eix -I zenity
[I] gnome-extra/zenity
Available versions: 3.12.1 {debug libnotify test +webkit}
Installed versions: 3.12.1(13:49:47 04/12/14)(libnotify webkit -debug -test)
Homepage: https://wiki.gnome.org/Projects/Zenity
Description: Tool to display dialogs from the commandline and shell scripts
2.1.3 Using a text editor, create the following shell script in your home directory and give it a meaningful name, such as cups-pdf_script.sh:
#!/bin/bash
CURRENT_PDF="${1}"
CURRENT_USER="${2}"
DISPLAY=:0.0
export DISPLAY
XAUTHORITY=/home/${CURRENT_USER}/.Xauthority
export XAUTHORITY
PDFNAME=$(zenity --file-selection --save --confirm-overwrite)
mv "$CURRENT_PDF" "$PDFNAME"
|
2.2 KDialog (KDE/Qt)
2.2.1 Install KDialog:
# emerge kde-base/kdialog
2.2.2 Check it is installed:
# eix -I kde-base/kdialog
[I] kde-base/kdialog
Available versions: (4) 4.12.5(4/4.12) (~)4.14.2(4/4.14) (~)4.14.3(4/4.14)
{aqua debug}
Installed versions: 4.14.3(4)(00:56:36 04/12/14)(-aqua -debug)
Homepage: http://www.kde.org/
Description: KDialog can be used to show nice dialog boxes from shell scripts
2.2.3 Using a text editor, create the following shell script in your home directory and give it a meaningful name such as cups-pdf_script.sh:
#!/bin/bash
get_dbus()
{
if [ -z $1 ]; then
echo "specify user" >> $HOME/cups-pdf_script.log
return 1
fi
compatiblePrograms=( kdeinit kded4 pulseaudio trackerd )
for index in ${compatiblePrograms[@]}; do
PID=$(ps -ef | grep $1 | grep ${index} | head -1 | awk '{print $2}')
if [[ "${PID}" != "" ]]; then
break
fi
done
if [[ "${PID}" == "" ]]; then
echo "Could not detect active login session" >> $HOME/cups-pdf_script.log
return 1
fi
QUERY_ENVIRON="$(tr '\0' '\n' < /proc/${PID}/environ | grep "DBUS_SESSION_BUS_ADDRESS" | cut -d "=" -f 2-)"
if [[ "${QUERY_ENVIRON}" != "" ]]; then
return 0
else
echo "Could not find dbus session ID in user environment." >> $HOME/cups-pdf_script.log
return 1
fi
}
CURRENT_PDF="$1"
CURRENT_USER="$2"
export DISPLAY=:0
export XAUTHORITY=/home/${CURRENT_USER}/.Xauthority
export HOME=/home/${CURRENT_USER}/
CONFIG_FILE="/home/${CURRENT_USER}/.pdf-writer.conf"
CANCELLED="No"
date > $HOME/cups-pdf_script.log
get_dbus ${CURRENT_USER}
if [ $? -eq 0 ]; then
export DBUS_SESSION_BUS_ADDRESS=${QUERY_ENVIRON}
while [ True ]; do
CURDATE=$(/bin/date +%Y%m%d)
CURNAME=$(/bin/cat "$CONFIG_FILE")
BASENAME=$(basename $CURRENT_PDF)
FILENAME=$(/usr/bin/kdialog --getsavefilename "$CURNAME$CURDATE-" "*.pdf" --title="Save PDF")
if [ $? -eq 1 ]; then
CANCELLED="Yes"
break
fi
echo $FILENAME
if [ ! "$FILENAME" = "" ]; then
if [ -e "$FILENAME" ]; then
/usr/bin/kdialog --warningcontinuecancel "File already exists"
if [ $? -eq 1 ]; then
continue;
fi
fi
FILENAME=$(echo $FILENAME.pdf | sed -re "s/(\.pdf)+$/.pdf/g")
break;
else
/usr/bin/kdialog --error "You must select a file or hit Cancel."
fi
break
done
if [ "${CANCELLED}" == "No" ]; then
/bin/cp "${CURRENT_PDF}" "${FILENAME}"
okular "${FILENAME}" &
fi
/bin/rm "${CURRENT_PDF}"
echo "No errors encountered." >> $HOME/cups-pdf_script.log
exit 0
else
/bin/rm "${CURRENT_PDF}"
echo "Errors encountered." >> $HOME/cups-pdf_script.log
exit 1
fi
|
2.3 Whichever of the above two options (Zenity or KDialog) you chose, do the following:
2.3.1 Make the shell script file executable:
# chmod +x /home/<your username>/cups-pdf_script.sh
2.3.2 Edit the file /etc/cups/cups-pdf.conf, find the line starting with ‘#PostProcessing‘, and change it to:
PostProcessing /home/<your username>/cups-pdf_script.sh
|
2.3.3 Restart CUPS:
# /etc/init.d/cupsd restart # If your installation uses OpenRC.
# systemctl restart cups.service # If your installation uses systemd.
Now, when you select Virtual_PDF_Printer from your
applications’ list of available printers and click ‘Print’, a pop-up
window should appear, allowing you to select the target directory for
the PDF file and enter a file name of your choice.