How to Convert a PDF File to Text in the Ubuntu Terminal Using pdftotext

Extracting text from a large PDF document can be frustrating. If you open the PDF in a graphical viewer and try to highlight and copy the text manually, you often accidentally select headers, page numbers, or hidden formatting characters that ruin the text structure when pasted.

If you are using an Ubuntu Linux machine, the fastest and cleanest way to strip all the formatting and extract pure, raw text from a PDF is by using a command-line tool called pdftotext.

How to Install the Poppler Utilities Package

The pdftotext command is not installed by default on a fresh Ubuntu server. It is part of the widely used poppler-utils open-source library, which provides numerous PDF manipulation tools.

  1. Open your Ubuntu terminal.
  2. Update your package list first: sudo apt update
  3. Install the poppler utilities package: sudo apt install poppler-utils

How to Convert a PDF to a Text File

Once installed, using the tool is incredibly straightforward. You simply need to provide the name of the input PDF file and the desired name for the output text file.

  1. Navigate to the directory containing your PDF file using the cd command (e.g., cd ~/Downloads).
  2. Run the conversion command in this exact format: pdftotext input_file.pdf output_file.txt

The tool will silently process the document and create a new file called output_file.txt in the same directory. This new file will contain all the readable text from the PDF, completely stripped of fonts, images, and page layouts.

How to Maintain the Original Layout

By default, pdftotext simply dumps all the text sequentially. If your PDF contains a complex table or multi-column layout, the standard conversion might jumble the words together.

You can force the tool to use spatial analysis to preserve the physical layout of the text on the page using the -layout flag:

pdftotext -layout input_file.pdf output_file.txt

This command instructs the tool to insert spaces and line breaks intelligently to ensure the output text file visually resembles the structure of the original PDF table or column.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.