Posts Tagged ‘PDF’

If you’ve installed a program for working with specific file types, you might find that Ubuntu‘s file manager Nautilus has no idea about it when you right-click a file and go to Open With, where a list of alternative programs to the default are presented. While in most cases that new app will be found when you choose Other Application… from the context menu, sometimes this isn’t the case.

Back in Gnome 2.x, if the program wasn’t listed, you could choose to add a custom application, which let you specify the command manually. However, this is no longer the case, but there should be another way to rectify this (see also the command-line interface method at the bottom).

In this example, we’ll look at getting Nautilus to recognise PDF Editor (pdfedit) as a viable program when right-clicking PDF documents, since the file manager doesn’t know it exists, and one can no longer just specify pdfedit as a custom command (at least via the GUI).

While you could be forgiven for thinking you’d need to hack a list of applications (for example, ~/.local/share/applications/mimeapps.list), the answer in fact lies in editing the .desktop file of the newly-installed program, and simply inserting three characters into it. Basically, this will allow Nautilus to add it to its context menu (actually, it specifies that the application can be passed a filename, which is what is missing).

All you need is the actual command that runs the program (e.g. pdfedit for PDF Editor), and you should be able to guess the .desktop file’s name (e.g. pdfedit.desktop), and open it for editing with the following command (replacing pdfedit with the appropriate name in your case):

gksudo gedit /usr/share/applications/pdfedit.desktop

(Note that the .desktop files should be in /usr/share/applications, but if not will be in ~/.local/share/applications, so change the path accordingly if you need to. Also, if you cannot correctly guess the .desktop file’s name, you can get the correct name by going to the folder and browsing for it).

[Desktop Entry]
Name=PDF Editor
Comment=PDF Editor

Find the Exec= line and you will see the command listed after it. Simply go to the end of the line, hit the spacebar, and add %f, so the line looks like:

Exec=pdfedit %f

(Once again, substitute your command’s name for pdfedit).

Simply save the file when exiting, and you shouldn’t even need to restart Nautilus, let alone log out or totally reboot. You should immediately see the desired program in the list of apps presented in Other Application…, and once you open a file with it, the app should be easily accessible in the list of secondary programs found in Open With.

If you want to make that program the new default for opening the particular filetype, you can now right-click one, choose Properties, go to the Open With tab, click on the app under Recommended Applications, and click the Set as default button.

CLI Method to Change Application & Set Default:

You can easily open a file with another application using the mimeopen command in the terminal. However, if the program isn’t already in the list of recommended applications, you’ll need to make it the default for that filetype first. Simply open a terminal in the folder where the file is and run a command like the following (substituting Recipes.pdf with the appropriate filename and filetype):

mimeopen -d Recipes.pdf

Please choose a default application for files of type application/pdf

1) GIMP Image Editor (gimp)
2) Adobe Reader 9 (AdobeReader)
3) Document Viewer (evince)
4) Other…

use application #4
use command: pdfedit

Simply choose the number that corresponds to Other… (in this case it’s 4), then type the command of the program after use command: (you probably won’t need to specify the path, but if it doesn’t work without it, it should be something like /usr/bin/pdfedit).

After that, you can switch default applications quite easily with the above command, or use the --ask option to just open the file in the desired app without changing the default (note there is no option to choose Other…, which is why you have to use the -d switch first):

mimeopen --ask Recipes.pdf

Please choose an application

1) pdfedit (pdfedit-usercreated-2)
2) GIMP Image Editor (gimp)
3) Adobe Reader 9 (AdobeReader)
4) Document Viewer (evince)

use application #


That’s it – hopefully with either of the methods you’re not cursing Nautilus any more, and have more control of your filetypes than your file manager currently provides.


Did this information make your day? Did it rescue you from hours of headache? Then please consider making a donation via PayPal, to buy me a donut, beer, or some fish’n’chips for my time and effort! Many thanks!

Buy Ubuntu Genius a Beer to say Thanks!


Read Full Post »

PDF (Portable Document Format) documents are a handy way to present text and images to others knowing they’ll look the same no matter what word processor or operating system they use. Basically, they’re a snapshot of a document, so saving images from them can be a hassle, even if your viewer lets you right-click them and save them as files. There are a few programs around that can do this for you, but it’s actually much easier and faster doing this from the command-line.

The pdfimages command is part of poppler-utils, which should already be installed on your system (sudo apt-get install poppler-utils in the terminal if it isn’t). To extract the images from a PDF, just open a terminal in the folder with the document, and run a command like the following:

pdfimages -j Cool-Pix-of-2011.pdf cool2011

Note that when extracting from files with spaces in the name, you will need to enclose the filename in single quotes. Eg:

pdfimages -j 'Cool Pix of 2011.pdf' cool2011

The text at the end of the command is what each extracted image will begin with, so the resulting filenames will be cool2011-000.jpg onwards (note that numbering starts at 000, not 001). Once again, if you’d prefer to have spaces in the target names, for example to mirror the name of the original PDF, then enclose that in single quotes too (eg: 'Cool Pix of 2011 ' – note the space at the end, just to provide a bit more separation between '2011' and the hyphen preceding the automatic numbering; this is of course optional, and you can pretty much do what you want). Eg:

pdfimages -j 'Cool Pix of 2011.pdf' 'Cool Pix of 2011 '

Your pictures will now be extracted into the folder with names starting with Cool Pix of 2011 -000.jpg.

Also, the -j option is to save the images in the .jpg format, otherwise they will be saved in .ppm (Portable Pixmap) format, with each file being over a megabyte. This can mean, for example, that an 18Mb document with 120 images can extract to 154Mb of files, whereas exporting them as .jpg ends up with a total of 18Mb, just like the original document. Of course, if you’d prefer to save them as .ppm images, simply leave out the -j option.

If you would like to include the page numbering in the file names, add the -p option. Eg:

pdfimages -j -p 'Cool Pix of 2011.pdf' 'Cool Pix of 2011 '

Lastly, don’t worry if you see the following in the terminal for each image being extracted:

Error (18468081): Missing ‘endstream’
Error: Unknown operator ‘endstream’
Error: Unknown operator ‘endobj’

You shouldn’t see that with every PDF you try to extract from, but even when you do you should find the target images have been created without issue.

Extra Notes:

For more options for this command, run pdfimages -?. For example, you can specify a start and end page, but personally I find it easier to just extract the whole document and delete any images I don’t want afterwards. But if you need to specify a password, you will find the option here.


Did this information make your day? Did it rescue you from hours of headache? Then please consider making a donation via PayPal, to buy me a donut, beer, or some fish’n’chips for my time and effort! Many thanks!

Buy Ubuntu Genius a Beer to say Thanks!

Read Full Post »