Mastering Text to PDF Conversion on Your Linux Machine
So, you've got a plain text file – maybe notes, a script, or a configuration log – and you need it in PDF format. Perhaps you need to share it with someone who doesn't use Linux, or maybe you just want a more polished, universally readable document. Fortunately, Linux offers a surprising number of ways to achieve this, ranging from simple command-line tricks to user-friendly graphical applications. This guide will walk you through the most common and effective methods, ensuring you can confidently convert your text to PDF, no matter your technical background.
The Command Line: Your Powerful Ally
For many Linux users, the command line is where the real magic happens. It's efficient, scriptable, and often the quickest way to get things done. Don't let the blinking cursor intimidate you; these commands are straightforward and incredibly useful.
Using `enscript` and `ps2pdf`
This is a classic and very robust two-step process. `enscript` converts your text file into a PostScript file, which is a page description language. Then, `ps2pdf` (part of the Ghostscript suite) converts that PostScript file into a PDF. This method offers a good deal of control over formatting.
-
Install `enscript` and Ghostscript (if not already installed):
Open your terminal and run the appropriate command for your Linux distribution:
- Debian/Ubuntu:
sudo apt update && sudo apt install enscript ghostscript - Fedora:
sudo dnf install enscript ghostscript - Arch Linux:
sudo pacman -S enscript ghostscript
- Debian/Ubuntu:
-
Convert text to PostScript:
Navigate to the directory containing your text file (e.g.,
my_document.txt) in the terminal. Then, use the `enscript` command:enscript -p output.ps my_document.txtThis will create a file named
output.psin the same directory. -
Convert PostScript to PDF:
Now, use `ps2pdf` to convert the PostScript file into a PDF:
ps2pdf output.ps output.pdfThis will generate your final PDF file,
output.pdf.
Customization with `enscript`: `enscript` has numerous options. For example, to add line numbers:
enscript -N -p output.ps my_document.txt
Or to specify a particular paper size (like US Letter):
enscript --paper=letter -p output.ps my_document.txt
Using `a2ps` and `ps2pdf`
`a2ps` (Anything to PostScript) is another excellent tool that works similarly to `enscript`. It's particularly good at pretty-printing code.
-
Install `a2ps` (if not already installed):
- Debian/Ubuntu:
sudo apt update && sudo apt install a2ps ghostscript - Fedora:
sudo dnf install a2ps ghostscript - Arch Linux:
sudo pacman -S a2ps ghostscript
- Debian/Ubuntu:
-
Convert text to PostScript:
a2ps -o output.ps my_document.txt -
Convert PostScript to PDF:
ps2pdf output.ps output.pdf
Directly with `cupsfilter`
`cupsfilter` is part of the CUPS (Common Unix Printing System) printing system, which is standard on most Linux desktops. It can often perform direct text-to-PDF conversion.
-
Ensure CUPS is running:
On most modern desktop Linux distributions, CUPS is running by default. You usually don't need to do anything.
-
Convert text to PDF:
Use the `cupsfilter` command:
cupsfilter my_document.txt > output.pdfThis is often the simplest command-line method for straightforward text files.
Graphical Applications: For the Visually Inclined
If you prefer not to dive into the terminal, or if you need more sophisticated formatting and previewing capabilities, several graphical applications can handle text-to-PDF conversion with ease.
LibreOffice Writer / OpenOffice Writer
These free and open-source office suites are powerful word processors that can open plain text files and export them as PDFs.
-
Open your text file:
Launch LibreOffice Writer (or OpenOffice Writer). Go to File > Open... and select your
.txtfile. -
Export as PDF:
Once the file is open, you can treat it like any other document. To export it as a PDF, go to File > Export As > Export as PDF....
A dialog box will appear, allowing you to set various PDF options, such as image compression, security settings, and page ranges. Click Export, choose a location and filename, and save your PDF.
Text Editors with Print-to-PDF Functionality
Many modern text editors, like Visual Studio Code, Atom, gedit (GNOME's default editor), and Kate (KDE's default editor), have a built-in "Print" function that often includes a "Save as PDF" or "Print to PDF" option in the print dialog. This leverages your system's printing framework, which typically uses CUPS.
- Open your text file in your preferred editor.
-
Go to File > Print... (or use the keyboard shortcut, often
Ctrl+PorCmd+P). -
In the print dialog, look for a printer selection dropdown.
You should see options like "Print to PDF," "Save to PDF," or a virtual PDF printer. Select this option.
-
Click "Print" or "Save."
You'll then be prompted to choose a location and filename for your PDF document.
Dedicated PDF Tools (Less Common for Direct Text Conversion)
While tools like PDF Arranger, PDFtk, or GIMP are fantastic for manipulating existing PDFs, they aren't typically the first choice for converting plain text directly. They are more suited for combining, editing, or converting other document formats into PDF.
Choosing the Right Method for You
The best method depends on your needs:
- For quick, unattended conversions on the command line: `cupsfilter` is often the simplest.
- For more control over formatting, fonts, and page layout via the command line: `enscript` or `a2ps` followed by `ps2pdf` are your best bets.
- For a visual workflow with extensive formatting options and a familiar interface: LibreOffice Writer or OpenOffice Writer are excellent choices.
- For a quick conversion without leaving your favorite editor: Use the "Print to PDF" feature of your text editor.
With these methods, converting text to PDF in Linux becomes a breeze. Experiment with the options available for each tool to find the workflow that best suits your tasks and preferences.
Frequently Asked Questions (FAQ)
How do I make my PDF look more professional when converting from text?
For a more professional look, consider using LibreOffice Writer or OpenOffice Writer. These applications allow you to choose fonts, adjust margins, add headers and footers, and control line spacing, all of which contribute to a polished final PDF. The command-line tools like `enscript` also offer options for font selection and layout, but they require more specific knowledge.
Why is my PDF's text blurry or low quality?
This usually happens if you're converting an image of text or if the conversion process doesn't correctly interpret the font information. Ensure you're starting with a plain text file (.txt) and not an image file containing text. When using command-line tools, ensure you're using `ps2pdf` which is designed for high-quality PostScript to PDF conversion. In graphical applications, check the export settings to ensure you're not applying excessive image compression if your text file contains any embedded graphics (though this is rare for pure text files).
Can I convert multiple text files to PDF at once?
Yes, you can. The most straightforward way is by using shell scripting. For example, you could write a simple bash script that loops through all .txt files in a directory and applies one of the command-line methods to each. You can also often select multiple files in file managers and right-click to find an "Open With" or "Print" option that might allow batch processing, though this is less common for direct text-to-PDF batch conversion.
Why would I use the command line over a graphical application for text to PDF conversion?
The command line is incredibly efficient for repetitive tasks. If you need to convert dozens or hundreds of text files, you can write a script to automate the process, saving significant time. Command-line tools also offer finer-grained control over output formatting that might be difficult or impossible to achieve through a graphical interface. They are also essential for server environments where graphical interfaces are often not available.

