Skip to main content

Raymii.org Raymii.org Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | RSS Feed

Viewing PDF, .docx and .odt files in mutt (as text)

Published: 03-03-2019 | Author: Remy van Elst | Text only version of this article


❗ This post is over five years old. It may no longer be up to date. Opinions may have changed.


mutt is my email client at work. I like the simple interface, the speed and the ability to customize the workflow. Email is synced with offlineimap and sent via msmtp, addresses are in abook, and calcurse is the calendar for meetings, no complicated setup there. One aspect I especially like is the ability to view attachments on the command line right from mutt itself. Some departments at work send emails with an attached PDF or .docx file that contains the actual message, instead of just putting the text in the email itself. Using pandoc and pdftotext in mutt, the text of the attachments is displayed as a regular mail, no interruptions in my workflow by opening an external program. This article explains how to set up your .muttrc and .mailcap to use pandoc and pdftotext to view attachments as text in mutt.

I do assume you have a working mutt set up as I don't cover that here. The Arch Linux Wiki on mutt is a great place to start if you haven't got mutt setup yet.

Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:

I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $200 credit for 60 days. Spend $25 after your credit expires and I'll get $25!

Installing software

On Ubuntu both packages required are in the repository and can be installed using apt:

apt-get install pandoc poppler-utils

pdf2text is in poppler-utils.

.mailcap

Your .mailcap. file contains information for a mail client how to handle non- text files.

In your .muttrc file you need to specify where this file is:

set mailcap_path = ~/.mailcap

The man page for .mailcap explains the purpose and format of the file:

Each mailcap entry consists of a content-type specification, a command to execute, and (possibly) a set of optional "flag" values. For example, a straightforward mailcap entry (which is default behavior for metamail) would look like this:

text/plain; cat %s

The optional flags can be used to specify additional information about the mail- handling command. For example:

text/plain; cat %s; copiousoutput

can be used to indicate that the output of the cat command may be voluminous, requiring either a scrolling window, a pager, or some other appropriate coping mechanism.

HTML mails

I use elinks for example to view html mails, the following line accomplishes that:

text/html; elinks -dump ; copiousoutput;

Combined with the following line in my .muttrc to auto convert HTML mails:

auto_view text/html text/calendar application/ics

You only need text/html, but I also have calendar and meeting invites that I auto view due to Exchange presenting those in some weird empty email with attachment format.

Now, back to the PDF and .docx files.

PDF & .docx

The following command will convert a .docx file to text. The to parameter states markdown, but the output will be plain text with markdown formatting.

pandoc --from docx --to markdown My_doc_file.docx

This works for OpenOffice as well:

pandoc --from odt --to markdown My_odt_file.odt

The following command will convert a .pdf file to text:

pdftotext -layout %s

Do note that in both cases non-text items might be lost, like images. Not a big issue since my files are mostly plain text but do keep it in mind. Tables work quite well, which surprised me. You still can save the attachments and view them with another program (like LibreOffice).

Putting two and two together results in the following three lines in your .mailcap file:

application/vnd.openxmlformats-officedocument.wordprocessingml.document; pandoc --from docx --to markdown %s; copiousoutput
application/vnd.oasis.opendocument.text; pandoc --from odt --to markdown %s; copiousoutput
application/pdf; pdftotext -layout %s -; copiousoutput;

Restart mutt and open an email with an attachment you want to view. Instead of using s to save the file you can now use v to view the file. Either pdftotext or pandoc is invoked, and the plain text output is shown inside mutt.

A PDF file viewed in mutt, as text

Tags: articles , bash , mail , markdown , microsoft , mutt , office , pandoc , pdf , pdf2text , word