DEV Community

Joli aman
Joli aman

Posted on

TXT to PDF: 4 ways developers actually do it (CLI, Python, and no-install)

Need a plain .txt file as a PDF -- a README, some logs, meeting notes, a report? Here are four ways developers actually do it, from the terminal to zero installs. Real solutions first.

1. Pandoc: the fastest CLI option

If you live on the command line, Pandoc is the go-to document converter. Install it with your package manager (e.g. sudo apt install pandoc or brew install pandoc), then:

pandoc notes.txt -o notes.pdf
Enter fullscreen mode Exit fullscreen mode

That is the whole command. Pandoc reads the text and renders a clean PDF. Want nicer margins or a different font? Add a couple of flags:

pandoc notes.txt -o notes.pdf --pdf-engine=xelatex -V geometry:margin=1in
Enter fullscreen mode Exit fullscreen mode

Great for: one-off conversions, Makefiles, and quick CLI work.

2. Python + fpdf2: scripted and batch conversion

When you need to convert files in a pipeline, a tiny Python script wins. Install fpdf2 once:

pip install fpdf2
Enter fullscreen mode Exit fullscreen mode

Then:

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=11)
with open("notes.txt") as f:
    for line in f:
        pdf.multi_cell(0, 5, line.rstrip())
pdf.output("notes.pdf")
Enter fullscreen mode Exit fullscreen mode

Loop it over a directory and you get batch conversion, with page numbers, headers, or footers thrown in for free.

Great for: automation, CI pipelines, and custom formatting.

3. LibreOffice headless: no new tools needed

If LibreOffice is already on the machine, its headless mode converts files without opening a window:

soffice --headless --convert-to pdf notes.txt
Enter fullscreen mode Exit fullscreen mode

The PDF lands in the current directory. It works on Linux, macOS, and Windows, which makes it a reliable fallback in scripts where installing new packages is a hassle.

Great for: servers and machines that already have an office suite.

4. No install at all: convert in the browser

Sometimes you just need one file converted right now and do not want to install anything. Open a browser-based converter, upload your .txt, and download the resulting PDF. To turn a text file into a PDF, just drag the file in, wait a few seconds, and grab the download -- nothing to install, nothing to configure.

Great for: quick one-offs on a machine you do not control.

Wrapping up

For speed, reach for Pandoc; for automation, script it with Python; for machines with an office suite already installed, go headless with LibreOffice; and for the occasional one-off, the browser has you covered. Pick the option that matches where you are, and turning TXT into PDF stops being a chore.

Top comments (1)

Collapse
 
merbayerp profile image
Mustafa ERBAY •

Nice and practical overview, Joli!

I particularly appreciate that you’ve included CLI and scripting options rather than focusing only on online converters. Just two small things worth mentioning: Pandoc usually needs a separate PDF engine, such as LaTeX, and Python’s built-in PDF fonts can struggle with Unicode characters. One additional consideration is privacy. For logs, internal reports, or other sensitive files, I’d definitely prefer local conversion over uploading them to an online service. Thanks for putting these options together! Simple tools and straightforward workflows are often the most useful.