DEV Community

jelizaveta
jelizaveta

Posted on

Convert TXT to PDF with Python (Automatic Pagination)

In daily work we may need to convert plain text files (TXT) to the more universal and shareable PDF format. Although copying and pasting manually can accomplish this, manual pagination and layout become tedious for large files. Here I’ll show how to use the Spire.PDF for Python library to easily convert TXT to PDF with automatic pagination.

About Spire.PDF for Python

Spire.PDF for Python is a professional Python library for creating, manipulating, and converting PDF documents in Python applications. It provides a rich API that lets developers easily add text, images, tables, and other elements to PDFs, and supports advanced features such as encryption, merging, and splitting. Most importantly, it supports automatic pagination and can handle layout for large amounts of text.

Installation is simple with pip:

pip install Spire.PDF
Enter fullscreen mode Exit fullscreen mode

Implementation Code

Below is the complete Python code that reads content from a TXT file and converts it to a PDF:

from spire.pdf.common import *
from spire.pdf import *

defReadFromTxt(fname: str) -> str:
"""Read TXT file content"""
withopen(fname, 'r') as f:
        text = f.read()
return text

inputFile = "input.txt"
outputFile = "TextToPdf.pdf"

# Get text from TXT file
text = ReadFromTxt(inputFile)

# Create PdfDocument instance
pdf = PdfDocument()

# Add a page (default 40-point margins)
page = pdf.Pages.Add()

# Create PDF font and brush
font = PdfFont(PdfFontFamily.TimesRoman, 11.0)
brush = PdfBrushes.get_Black()

# Set text alignment and line spacing
strformat = PdfStringFormat()
strformat.LineSpacing = 10.0
strformat.Alignment = PdfTextAlignment.Justify

# Set text layout (key: automatic pagination)
textLayout = PdfTextLayout()
textLayout.Break = PdfLayoutBreakType.FitPage
textLayout.Layout = PdfLayoutType.Paginate

# Create a PdfTextWidget instance to hold the text content
textWidget = PdfTextWidget(text, font, brush)

# Set text format
textWidget.StringFormat = strformat

# Draw the text at a specified location on the page
bounds = RectangleF(PointF(0.0, 0.0), page.Canvas.ClientSize)
textWidget.Draw(page, bounds, textLayout)

# Save the result file
pdf.SaveToFile(outputFile, FileFormat.PDF)
pdf.Close()
Enter fullscreen mode Exit fullscreen mode

Code Explanation

1. Text reading

The ReadFromTxt function reads all content from the TXT file and returns it as a string.

2. PDF document creation

Create a PdfDocument instance and add an initial page.

3. Font and formatting

  • Use Times Roman font at size 11
  • Set line spacing to 10.0
  • Text alignment set to justify

4. Key settings for automatic pagination

This is the core part that enables automatic pagination:

  • PdfLayoutBreakType.FitPage: automatically break to a new page when content exceeds the page
  • PdfLayoutType.Paginate: enable pagination layout mode

5. Using PdfTextWidget

PdfTextWidget is a powerful text container that automatically handles pagination for long text. You only need to specify the drawing area and it will determine whether to create new pages for the remaining content.

6. Drawing and saving

After drawing the text at the specified position, save the PDF to a file.

Benefits

  1. Fully automated: no need to calculate how many characters fit on each page manually
  2. Preserves formatting: supports font, size, line spacing, and other formatting settings
  3. Flexible: you can customize page margins and start position
  4. Handles large files: can easily process very long text files

Summary

Using Spire.PDF for Python, you can implement a complete TXT-to-PDF converter in just a few dozen lines of code. It supports basic formatting and—crucially—built-in automatic pagination, greatly simplifying development. Whether you’re converting simple notes or large document collections, this solution is up to the task.

If you often need to convert text to PDF, give this approach a try — it should save you a lot of time!

Top comments (0)