Reduce the File Size of PDF Documents with Preview on OS X

The PDF file format is ubiquitous for good reason, mostly because it allows for perfect preservation of a documents formatting, text, and other elements. However, sometimes PDF can be quite bloated and a file that should be not more than 200KB can suddenly be 1.5MB for no obvious reason. Adobe’s Acrobat X has functions to reduce/optimise the size of a pdf. On OSX you can achieve the same with Preview.

For PDF files that have not been optimized yet, the Preview app in OS X can often reduce the file size considerably by passing it through an export filter.
export-pdf-to-compress-file-size
reduce-file-size-filter-for-pdf-preview

While it works great for text heavy files, it’s not a perfect solution for image-heavy slides. The “Reduce File Size” Quartz filter will indeed drastically shrink the file size of a pdf but the great savings in size come at a price. It will also make the slides look thoroughly awful. This is because the filter achieves its file size reduction by scaling all the images down by at least 50% and to no more than 512 pixels on a side, plus it uses aggressive JPEG compression. So not only will the images suffer from compression artifacts, they will also tend to get that lovely up-scaling blur.

Fortunatenly, anyone can create their own Quartz Filter with the ColorSync Utility shipped with OSX. The following zip file: PDF compression filters contains various files that reduce the file size while trying to minimise the amount of artifcats introduced in the images. It’s nowhere near as aggressive as the default Quartz-Filter.

The ColorSync Utility will by default save your own Quartz Filters in the following directory: /Users/YourName/Library/Filters/.

In order for the Filters to appear in the Save AS/Export menu the *.qfilter files need to be moved into the following directory

/Library/PDF Services/ Notice not in home folder (~/) but system-wide.

Afterwards the filters will appear in the dropdown menu of the export function:
Screenshot 2014-11-28 13.43.34

Bootable OS X 10.10 Yosemite USB installation drive

With the announcement of new iPads and iMacs and a refreshed Mac mini, Apple also released the final version of OSX 10.10 after multiple public betas. In case you are setting up multiple Macs or need an offline installer for older Macs that do not support “Internet Recovery” you can create an USB Installer. You will need the following:

  • A Mac with OSX Mavericks/Yosemite installed
  • 8GB or larger USB flash drive – preferably USB 3.0 if your Mac supports it
  • OSX 10.10 Installer from the AppStore in the Appllications folder.
  • Administative Access to your Mac (No restricted account)

Beware: All date on the USB key will be deleted (create a backup if necessary)

Using the Disk Utility format the Stick so it contains 1 HFS+ Partition and uses the GUID partitoning scheme (not the default MBR)

DiskUtility Partition Table

Leave the name “Untitled” as it is for now and format it using the Mac OSX Extended (Journaled) filesystem.

Open command line terminal and enter the following command

[bash]

sudo /Applications/Install\ OS\ X\ Yosemite.app/Contents/Resources/createinstallmedia –volume /Volumes/Untitled –applicationpath /Applications/Install\ OS\ X\ Yosemite.app –nointeraction

[/bash]

This command will erase the disk and copy over all necessary installation files. After a while the USB key will automatically be mounted and the volume will soon load the OSX installer. Additionally, a recovery partition has been created that may come in handy if the hard drive dives and not internet connection is available.

Unpacking OSX .PKG Files

It used to be that .PKG files were simply folders wrapped up as a bundle using a special “bundle” folder attribute which you could either right-click and use “Show Contents” or just cd into the .pkg files from the command line. For Mac applications that you drag and drop into the “/Applications” folder this is still the case.

However more recently, the format of the installation packages seems to have changed, making it more difficult into extracting the application from pkg file in case you are not interested in installing a pkg.
As an example I will extract the TeamViewer application from the PKG-File in the disc image that can be provided from Teamviewer’s Download Site

Unpacking the .PKG File

The .pkg file itself is an “xar” – archive, an archive format that is able to handle files of arbitrary large file size by storing the toc (“table of content”) at the beginning of the archive and thereby allowing for a efficient stream-like handling of the files within an archive.

First copy the .pkg file from the disk image into a temporary folder and unpack it using the xar cmd tool.
[bash]
chris@chris-rmbp:~/Downloads$ cp /Volumes/TeamViewer/Install\ TeamViewer.pkg ~/Downloads/_tmp/
chris@chris-rmbp:~/Downloads/_tmp$ xar -xf Install\ TeamViewer.pkg
chris@chris-rmbp:~/Downloads/_tmp$ ls -la
total 32336
drwxr-xr-x 11 chris staff 374B Feb 18 21:56 ./
drwx—r-x+ 155 chris staff 5.1K Feb 18 21:02 ../
-rw-r–r–@ 1 chris staff 6.0K Feb 18 21:56 .DS_Store
-rw-r–r– 1 chris staff 2.0K Feb 18 21:56 Distribution
drwx—— 5 chris staff 170B Jan 1 1970 Font.pkg/
-rw-r–r–@ 1 chris staff 16M Feb 18 21:48 Install TeamViewer.pkg
drwx—— 6 chris staff 204B Jan 1 1970 LauncherFull.pkg/
drwx—— 4 chris staff 136B Jan 1 1970 Resources/
drwx—— 6 chris staff 204B Jan 1 1970 TeamViewerApp.pkg/
chris@chris-rmbp:~/Downloads/_tmp$ cd TeamViewerApp.pkg/
chris@chris-rmbp:~/Downloads/_tmp/TeamViewerApp.pkg$ ls -la
total 32632
drwx—— 6 chris staff 204B Jan 1 1970 ./
drwxr-xr-x 11 chris staff 374B Feb 18 21:56 ../
-rw-r–r– 1 chris staff 262K Feb 5 15:20 Bom
-rw-r–r– 1 chris staff 795B Feb 18 21:56 PackageInfo
-rw-r–r– 1 chris staff 16M Feb 5 15:20 Payload
-rw-r–r– 1 chris staff 542B Feb 5 15:20 Scripts
chris@chris-rmbp:~/Downloads/_tmp/TeamViewerApp.pkg$
[/bash]

Simply judging by the size you can tell that Payload must contain the application we are interested in. The Payload itself is a cpio archive compressed with gzip. The file can be extracted using the cpio archiver after piping it through gzip:

[bash]
chris@chris-rmbp:~/Downloads/_tmp/TeamViewerApp.pkg$ cat Payload | gzip -d – | cpio -id
93074 blocks
chris@chris-rmbp:~/Downloads/_tmp/TeamViewerApp.pkg$ ls -la
total 32632
drwxr-xr-x 7 chris staff 238B Feb 18 22:08 ./
drwxr-xr-x 11 chris staff 374B Feb 18 21:56 ../
-rw-r–r– 1 chris staff 262K Feb 5 15:20 Bom
-rw-r–r– 1 chris staff 795B Feb 18 21:56 PackageInfo
-rw-r–r– 1 chris staff 16M Feb 5 15:20 Payload
-rw-r–r– 1 chris staff 542B Feb 5 15:20 Scripts
drwxr-xr-x 3 chris staff 102B Feb 18 22:08 TeamViewer.app/
chris@chris-rmbp:~/Downloads/_tmp/TeamViewerApp.pkg$
[/bash]
In case new folders and files appear such as usr, private folders they contain the files and folders that would be installed relative to the root directory of the hard drive when the installer would be run normally.
In this case the only interesting file is the TeamViewer.app that contains the TeamViewer application which you can drag and drop this file to the /Application folder without having to install the launchagents that automatically start TeamViewer on login.

Silent Install of OSX Applications

Applications that are provided in an .pkg or .mpkg application bundle format can be installed silently in the background even while a user is currently logged into the system without him noticing. (.mpkg bundles are more customizable compared to their pkg. counterpart)

To Run the package installer in silent mode without showing the GUI-user-dialog the installer needs to be run from command line:

[bash]sudo -S installer -verbose -pkg your_installer_file.pkg -target /[/bash]

Target indicates the destination volume the package is going to be installed. The installer will run without showing the installer in the ui. You will have to enter your systems – administrator password in case the installer needs root permissions to copy various configuration files.
It’s possible (but not recommended) to fully automate the process with the command line by echoing the password

[bash]echo password | sudo -S installer -verbose -pkg your_installer_file.pkg -target /[/bash]

Attention: Your password will be stored in the bash_history in cleartext, therefore I DON’T  recommend it.