Forum Discussion
How to convert TXT to PDF?
fpdf2 is another free Python option. Its multi_cell() method wraps text within the page margins, which addresses your concern about lines being cut off.
Install it:
python -m pip install fpdf2
Put a Unicode TrueType font such as DejaVuSans.ttf beside your TXT file, then save this as convert.py:
from pathlib import Path
from fpdf import FPDF
text = Path("notes.txt").read_text(encoding="utf-8-sig")
pdf = FPDF()
pdf.set_margins(18, 18, 18)
pdf.set_auto_page_break(auto=True, margin=18)
pdf.add_page()
pdf.add_font("NotesFont", fname="DejaVuSans.ttf")
pdf.set_font("NotesFont", size=11)
pdf.multi_cell(w=0, h=6, text=text.expandtabs(4))
pdf.output("notes.pdf")
Run python convert.py to create notes.pdf.
Existing newline characters are retained, and additional wrapping happens when a line reaches the margin. The font is embedded in the PDF, helping it display consistently on other computers. Choose a font that contains the characters used in your notes.
This takes more setup than printing from a text editor, but it’s useful when you want the same page settings every time.