Friday, January 29, 2010

Print to PDF from the Linux Terminal

A while back I had to set up a system for printing reports to PDF files automatically. IE: I needed a script to do the conversion, retrieve the new file, fix the orientation, and return the new filename. Here is the script and the details and the requirements.

Prerequisites:
  • CUPS-PDF: This package provides a PDF printer that we can print to using lp.
  • TexLive: This is a large project with many tools and has a large footprint but it is necessary for possessing landscape jobs. The exact problem is that jobs printed in landscape will be miss oriented when viewed and this allows us to correct that problem.
  • pdfjam: The project listed but does not need to be installed, rather a custom version of the pdf90 script included in this project is needed. Specifically the script is customized to rotate the page counter-clockwise.
Files:
  • printpdf: This is the main script and can be called from any external tools.
  • pdf90minus: This is the modified version of pdf90 from pdfjam that rotates the pages of a PDF counter-clockwise.
  • texlive.profile: This is a installer configuration for TexLive with just the necessary components selected.
Configuration:

Install CUPS-PDF and configure a printer for it. To configure the printer you can use the CUPS web interface or add the following lines manually.
/etc/cups/printers.conf
<printer pdf="">
Info PDF Writer for CUPS
Location PDF Backend /usr/lib64/cups/backend/pdf-writer
DeviceURI pdf-writer:/tmp/
State Idle
Accepting Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
</printer>

Install TexLive. To install the minimal required components should download the installer and the profile and extract them to the same location. then run the following command from that folder.
sudo ./install-tl −profile texlive.profile

Save the pdf90minus and pdfprint scripts to the same location. You may wish to customize the working path /tmp but it must be the same in the printer configuration and printpdf script.

Usage:

You can now test the PDF printing, the job will print and the PDF will be generated at the same path with the same name as teh original file only suffixed with .pdf.
./printpdf myfile.txt [--rotate|-r] [--save|-s]

The --rotate option causes the job to be printed in landscape mode and then the PDF pages to be reoriented to display properly.
The --save option causes the source file to be left behind and not deleted once successful printed.
The script will block execution until the print job has finished and will then return passing the new filename to STDOUT.

Auto Mount NFS Shares via Bonjour

07/21/2011 - Update
It papers this site is no longer available.  As such I have posted the original script here and the automated workflow is  here.

There are also installation instructions for OSX 10.7 which is working but requires some extra setup and Xcode must also be installed before the scripts.

Installation:
  • Download the two file above and extract them someplace.
  • Drag the Automator action into our Applications folder and add it to your user accounts Login Items list so that it will automatically run each login.
  • Setup the background script to do the actual work.
Open up the Terminal application and locate the directory you saved the script into.
cd ~/Downloads

Run the following commands to install the script locally.
sudo mkdir -p /usr/local/bin
sudo cp bonjournfsmd.rb /usr/local/bin/bonjournfsmd.rb
sudo chown root:wheel /usr/local/bin/bonjournfsmd.rb
sudo chmod +x /usr/local/bin/bonjournfsmd.rb

Run the following commands to install some dependancies required on OSX 10.7 only.
sudo gem install dnssd
sudo gem install daemons

A wile back I switched my server over to FreeBSD/ZFS and ended up changing the shared volumes to use the NFS features of ZFS.
There were a few minor issues with the initial configuration and the OSX clients but this was not to difficult to resolve. Specifically the NFS system does not allow for user account mapping, but rather the accounts on both the server and client must be identical. Furthermore this mapping is not done by username, but rather by the UID of each user. On OSX the UID start at 501 where as on FreeBSD they default to 1000. The best approach to fix this problem is simply to update the account on the BSD box to use the same UID as OSX. (It is possible to change UID in OSX but not recommended.) After this fix everything was working correctly including write access and ACL lists.
The other thing I wanted to do, and the reason for this post, was to have my OSX desktop automatically map these volumes on boot. I found a simple Bonjour NFS share Mounter Daemon, http://svwtech.com/site, that can be run from the terminal and will do exactly that but I was unable to get the script to run at boot. In the end I had to create a Automator application to run the script and then add that application to my startup items.
You can download the Application with the above auto-mount script embedded in it here.

Find Duplicate Photos in an iPhoto Library

03/04/2011- Update: Updated the service with some fixes for invalid paths and quoting problems.
06/23/2016- Update: Updated download link. I'm not really sure if this still works with newer versions of iPhoto or Photo's though.

There are many shareware tools for finding duplicate pictures in an iPhoto library but this should be a simple operation, and honestly shouldn't require a fee to utilize. As a result I created a simple Service via Automator to solve this problem.