How to reduce in size, shrink, optimize a pdf

For administrative purposes I scan all important paper documents to pdf. Sometimes those pdf files are quite big. Here are a few ways you can reduce in size, shrink and optimize a pdf.

Use pdf2ps and ps2pdf

The simplest way is to use the pdf2ps and ps2pdf (ps2pdf14) commands. They are part of the Ghostscript package so make sure you have ghostscript installed:

Next let’s see the results when using this on a 11MB pdf file called big.pdf

So the size went down from 11MB to 9,1MB. That’s not a very big improvement. As you can imagine results vary. At times these default commands can significantly reduce the size and sometimes not. If not, there are more tricks.

Use ps2pdf with options

If the reduction in size is not enough then it’s time to look at some of the ps2pdf options. man ps2pdf has a very long list of options with which you can experiment. The option that has a significant impact on the size of a pdf is -dPDFSETTINGS=…

The PDFSETTINGS option can be set to:

  1. /screen – for low-resolution output similar to the Acrobat Distiller “Screen Optimized” setting
  2. /ebook for medium-resolution output similar to the Acrobat Distiller “eBook” setting
  3. /printer for output similar to the Acrobat Distiller “Print Optimized” setting
  4. /prepress for output similar to Acrobat Distiller “Prepress Optimized” setting
  5. /default for a wide variety of uses, possibly at the expense of a larger output file

For a long list of ps2pdf options check out the ps2pdf page

Let’s try the /ebook option and see what the results are:

The size of the pdf went down from 11M to below 2M.

Other ways

Ghostscript (gs) is the de facto pdf read/write engine in the Linux world. So all other ways to process a pdf file are basically using that.

Shrinkpdf (available here) is a nice script to process pdf files with pre-determined settings.

The Ghostscript gs command
You can also directly use the Ghostscript gs command which pfd2ps, ps2pdf and shrinkpdf use. For example to convert a postscript (ps) file to a pdf with Ghostscript’s gs command:

How to make Asterisk work behind a FRITZ!Box

There are many Home DSL modems/routers that think it’s a good idea to mess with SIP and unfortunately I have one of them: the FRITZ!Box 7360.

I have an Asterisk server on a local LAN that sits behind a FRITZ!Box 7360 providing my Internet connection. The problem is that, while a remote client’s SIP registration to the Asterisk server on the local LAN succeeds, the FRITZ!Box intercepts all SIP calls, even if they are destined for the Asterisk server on the local LAN, making any call from a remote client to the Asterisk server time-out aka fail.

Yes that’s right: apparently the FRITZ!Box thinks it’s the only SIP server in the universe grabbing all SIP calls it sees, even if they are not destined for the FRITZ!Box.

If you want to see the reason with your own eyes, just backup the config of your FRITZ!Box and open it in your favourite text editor. Search for “voip” and you will find gems like this:

and this:

And this:

To make SIP calls work between remote clients and my Asterisk server, the FRITZ!Box configuration needs some serious fixing. Here are the steps to make your FRITZ!Box no longer mess with SIP.

Caveat: please be aware that the VoIP functionality of the FRITZ!Box may be impacted by this new config. As in that the VoIP/Telephony functionality of the FRITZ!Box no longer or only partially works. I do not use any of that functionality so have not tested it but given the all or nothing nature of the FRITZ!Box config it’s not an unreasonable assumption.

Step 1 – Backup the config of your FRITZ!Box

Open your browser and surf to the webinterface of the FRITZ!Box and enter your password if you have set it (good idea).

Now in the menu on the left click on “System” then in the “System” menu click on “Backup” and then click on “Save” in the lower right corner.

In my case the name of the generated config file is:
FRITZ.Box Fon WLAN 7360 124.06.04i_25.03.14_1730

Step 2 – Make a copy of the config file

Let’s create a copy of the config and work so there’s always the original config to revert back to in case anything goes wrong.

Step 3 – Make changes in the copy of the config file

Make the following changes in the copy of the config file which in my case is:
FRITZ.Box Fon WLAN 7360 124.06.04i_25.03.14_1730-CHANGED.export

– at the top of the file add “NoChecks=yes” between “Country=” and “Language=”
– change port 5060 to for example 45060
– change port 7078 range to for example 47078
– change port 7077 to for example 47077

Feel free to choose different numbers. Just make sure they don’t interfere with other applications.

Here is a diff of the changes I made:

Step 4 – Upload the new config to the FRITZ!Box


Again surf to the webinterface of the FRITZ!Box and enter your password if you have set it (still a good idea). In the menu on the left click on “System” then in the “System” menu click on “Backup” and then at the top of the screen click on the tab “Restore”.

The option “Way of Restoring Settings” should be set to the top one: “from this FRITZ!Box or another FRITZ!Box of the same model”. In my case that was the default. Next click on “Browse” and select the copy of the config file with the changes and let the FRITZ!Box restore the new config.

Once the FRITZ!Box has rebooted with the new config it should no longer mess with SIP calls destined for a SIP server on the local LAN.

How to encrypt a hard disk or USB stick

Here are the steps to encrypt a hard disk or USB stick on a Fedora or CentOS box.

Requirements: cryptsetup and pv need to be installed. So let’s do that first:

The steps to encrypt a hard disk or USB stick are exactly the same. I will describe the steps I did to encrypt a hard disk which is going to be used as a backup disk but to cryptsetup it makes no difference if it’s talking to a hard disk or USB stick. They are both disk devices and are treated similar.

Step 1 – plug in hard disk

I’m using an external hard disk and hook it up to my workstation using either eSATA or USB. It does not matter which you use. Once you have plugged it in, make sure you check /var/log/messages that the hard disk or USB stick were properly detected. You should see something like this:

The important part is the last few lines which tells you that my device is located at /dev/sdd amd that it has a partition at /dev/sdd1.

Step 2 – optionally create a partition on the hard disk

If your device does not have a partition yet create one. Just use fdisk or gparted or any partition software you like to create partitions. There is only one caveat: make sure you use the right device! So in my case it’s /dev/sdd. Using the wrong device will result in loss of data if you have data on it. Always check twice that you are using the right device.

Step 3 – fill device’s partition with random data

While you could use badblocks to randomize the data on the partition, the quality of the random data will not be as good as when using the command below. I like my encryption to be as good as can be so I’m using /dev/urandom and dd.

The command to write the entire disk partition with random data is:

Important: change /dev/sdX1 to your device’s partition!

Warning: this can take a very, very long time. A 2TB hard disk took more than 35 hours to fill with random data at a speed of 15MiB/s.

Step 4 – initialize LUKS partition, set passphrase

Use the following command to initialize the LUKS partition on the device’s partition and set the initial passphrase.

This command wil ask you to confirm that you really want to overwrite the data on your device’s partition, asks for a passphrase twice and then writes everything to your device’s partition. The output looks like this:

Step 5 – format the LUKS partition

First we need to open the LUKS partition before we can create a filesystem on it:

Again: change /dev/sdX1 to your device’s partition.

Now you can create a filesystem on it. The following command create a ext4 filesystem on the LUKS partition:

The output of the command looks like this:

When the ext4 filesystem has been created, close the LUKS partition:

Step 5 – set ownership and perms

If you are using Fedora there are only two steps left: set the ownership and permissions of the encrypted filesystem when it is mounted. Assume the encrypted filesystem on my hard disk is mounted. Let’s check the ownership and permissions:

As you can see the ownership and permissions are not right if I want to use this hard disk to backup my data on my workstation. So let’s fix that:

Now whenever I switch on the backup drive and enter the passphrase, the ownership and permissions will be: