Search

Ubuntu - Repair corrupted PDF

Contents[Hide]

dropcap-ubuntu

If you are a day-to-day Linux user, you may have faced some web sites using some broken PDF generation software, where PDF files they generate can't be properly displayed with open-source viewers like Evince.

Latest site where I've faced this problem was Easyjet. I was supposed to print my e-ticket, but all important data were totally unreadable. Here is what Evince was displaying :

ubuntu-pdf-broken

While googling to find a reader able to handle these broken PDF files, I realised that this problem is quite common and more interesting, that gs tool provided by Ghostscript was able to repair these files.

This article explains how to prepare your Linux desktop to be able to repair corrupted PDF files (like Easyjet e-tickets). It also explains how to integrate this tool as a custom action available from your favorite file manager (Nautilus & PcManFM) with a simple right click on the PDF file.

It has been tested on Ubuntu Gnome 14.04 LTS and Lubuntu 14.04 LTS. But, it should be applicable to any distribution using a Nautilus or PCmanFM.

Thanks to this setup, you'll be able to repair your corrupted PDF files which should be displayed properly in Evince.

ubuntu-pdf-repaired

1. Main Principles

A PDF repair tool should be used in 2 ways :

  • as a classic standalone application where you select the file thru a dialog box
  • from a file manager custom action with a right click on the PDF file

Evince, ePDFView, Xpdf and KPDF are sharing the same PDF rendering engine.

So the main idea in a PDF correction tool is to find a robust PDF converter using a different rendering engine.

This is where gs comes into the light as it is a command line tool and uses Ghostscript rendering engine. This engine is well known to be able to handle files which are giving trouble to other renderers. So gs will be used to repair the original broken PDF file.

As the PDF reparation job is not guaranteed, the tool in charge of the repair should not replace the original file. Instead, it should keep the original myfile.pdf and generate a myfile-repaired.pdf in the same folder.

Most modern file managers allow to handle files directly on a remote share (thru ftp, smb, ssh, ...). So the repairing script should be able to handle these remote files in a transparent manner using either URI or local path. To do so, we will use gvfs tools to pull the file locally and to push it back to the remote share after it has been repaired.

Finally, as repair job may take some time on big PDF files, a notification should inform you that it is over and that your newly repaired file is available.

2. Install packages

First step is to install all the tool that will be used by the script in charge of the PDF files reparation :

  • gvfs-copy to handle remote files copy
  • gs to do the repair work
  • notify-send to display desktop notifications
  • zenity to display file selection dialog

All these tools should be installed by default under Ubuntu Gnome, but all are not available under Lubuntu.

To be on the safe side, let's (re-)declare the packages :

# sudo apt-get install gvfs-bin libnotify-bin ghostscript zenity

3. Application Installation

It's now time to install the main script in charge of the reparation job and to declare it as a desktop application.

/usr/local/bin/pdf-repair

#!/bin/bash
# ---------------------------------------------------
# Repair broken PDF file using gs
#
# Depends on :
#   * ghostscript
#   * gvfs-bin
#   * libnotify-bin
#   * zenity
#
# Parameter :
#   $1 - full path of original PDF
#
# Revision history :
#   08/11/2014, V1.0 - Creation by N. Bernaerts
#   20/11/2014, V1.1 - Add file selection dialog box
#   24/01/2015, V1.2 - Check tools availability
# ---------------------------------------------------

# check tools availability
command -v gvfs-copy >/dev/null 2>&1 || { zenity --error --text="Please install gvfs-copy [gvfs-bin]"; exit 1; }
command -v gs >/dev/null 2>&1 || { zenity --error --text="Please install gs [ghostscript]"; exit 1; }
command -v notify-send >/dev/null 2>&1 || { zenity --error --text="Please install notify-send [libnotify-bin]"; exit 1; }

# check if parameter is given, otherwise open dialog box selection
[ "$1" != "" ] && ORG_URI="$1" || ORG_URI=$(zenity --file-selection --title="Select PDF file to repair")

# if a PDF file is selected
if [ "$ORG_URI" != "" ]
then
  # generate corrected document name
  ORG_BASE=$(echo "$ORG_URI" | sed 's/^\(.*\)\..*$/\1/')
  ORG_EXT=$(echo "$ORG_URI" | sed 's/^.*\.\(.*\)$/\1/')
  NEW_URI="$ORG_BASE-repaired.$ORG_EXT"

  # generate temporary local filename
  ORG_TMP=$(mktemp -t XXXXXXXX.${ORG_EXT})
  NEW_TMP=$(mktemp -t XXXXXXXX.${ORG_EXT})

  # copy input file to temporary local file
  gvfs-copy "${ORG_URI}" "${ORG_TMP}"

  # generate corrected PDF
  gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -sOutputFile="${NEW_TMP}" "${ORG_TMP}"

  # copy back corrected file
  gvfs-copy "${NEW_TMP}" "${NEW_URI}"

  # remove temporary local files
  rm ${ORG_TMP}
  rm ${NEW_TMP}

  # send desktop notification
  NEW_NAME=$(echo "$NEW_URI" | sed 's/^.*\/\(.*\)$/\1/')
  notify-send "Repaired PDF is $NEW_NAME"
fi

/usr/share/applications/pdf-repair.desktop

[Desktop Entry]
Type=Application
Exec=pdf-repair
Hidden=false
NoDisplay=false
Icon=evince
Keywords=pdf;repair;broken;corrupted;easyjet;
X-GNOME-Autostart-enabled=true
Name[en_US]=Repair corrupted PDF
Name[en]=Repair corrupted PDF
Name[C]=Repair corrupted PDF
Name[fr_FR]=Réparer PDF corrompu
Comment=Tool to repair corrupted PDF files with Ghostcsript. Is works well on boarding passes issued by EasyJet site.
Comment[en_US]=Tool to repair corrupted PDF files with Ghostcsript. Is works well on boarding passes issued by EasyJet site.
Comment[fr_FR]=Outil de reparation de fichiers PDF corrompus. Fonctionne sur les cartes d'enregistrement du site EasyJet.
MimeType=application/pdf;application/x-pdf;application/x-bzpdf;application/x-gzpdf;
Categories=GNOME;GTK;Viewer;Graphics;Utility;

Both files can be downloaded and installed from my GitHub account.

# sudo wget -O /usr/local/bin/pdf-repair https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/pdf/pdf-repair
# sudo chmod +x /usr/local/bin/pdf-repair
# sudo wget -O /usr/share/applications/pdf-repair.desktop https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/pdf/pdf-repair.desktop

After a reboot or a new session login, you should get a new Repair corrupted PDF application.

4. Desktop Integration

To get a full desktop integration, this repair tool should be available from a custom action in your file manager context menu.

This context menu should be displayed for any file having a PDF mimetype.

With latest Extension for Menus and Actions of the freedesktop.org Desktop Entry Specification (DES-EMA) this integration has become quite easy.

You just need to declare the new custom action in a .desktop file placed under $HOME/.local/share/file-manager/actions.

~/.local/share/file-manager/actions/pdf-repair-action.desktop

[Desktop Entry]
Type=Action
Icon=evince
Name[en_US]=Repair broken PDF
Name[en]=Repair broken PDF
Name[C]=Repair broken PDF
Name[fr_FR]=Réparer PDF corrompu
Tooltip[en_US]=Use Ghostscript to rebuild a faulty PDF file
Tooltip[en]=Use Ghostscript to rebuild a faulty PDF file
Tooltip[C]=Use Ghostscript to rebuild a faulty PDF file
Tooltip[fr_FR]=Utilise ghostscript pour réparer un fichier PDF mal construit
Profiles=repair_pdf;

[X-Action-Profile repair_pdf]
Exec=pdf-repair %u
MimeTypes=application/pdf;application/x-pdf;application/x-bzpdf;application/x-gzpdf;
Name[en_US]=Default profile
Name[en]=Default profile
Name[C]=Default profile

This can be done with these commands :

# mkdir --parents $HOME/.local/share/file-manager/actions
# wget -O $HOME/.local/share/file-manager/actions/pdf-repair-action.desktop https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/pdf/pdf-repair-action.desktop

After next login, all you PDF files should provide this new menu :

ubuntu-pdf-repair-menu

4.1. Nautilus

If you are using Nautilus file manager, you need one extra step to get this right click menu.

In fact, Nautilus is implementing DES-EMA specifications thru an extra nautilus-actions package which need to be installed :

# sudo apt-get install nautilus-actions

Once installed, launch Nautilus Actions application and configure the settings to get :

ubuntu-nautilus-action-preferences

As Nautilus does not display menu icons by default, you also need to enable this feature.

# gsettings set org.gnome.desktop.interface menus-have-icons true

After this configuration you should get the new Repair broken PDF right click menu on PDF files.

 

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus