Skip to main content

A tool for font replacement in PDFs.

Project description

swapfont

A PDF font replacer

PyPI CI codecov Documentation Status PyPI - Python Version

swapfont is a unified toolkit for analyzing and replacing legacy fonts (such as bitmapped Type 3 fonts) in PDF files with modern TrueType or OpenType fonts.

It performs a direct vector replacement by rewriting PDF content streams. It automatically adjusts horizontal character scaling (Tz) to ensure the original visual layout—including text reflow and spacing—is preserved, even when the new font has significantly different metrics.

Features

  • Interactive Wizard: A "dashboard" style CLI to quickly identify and replace fonts without writing config files.
  • Content Stream Rewriting: Replaces font names and character codes in text-showing operators (Tj, ', ").
  • Layout Preservation: Calculates Tz scaling to squash or stretch new text to fit the original bounding box (defaulting to a liberal 50%–200% range to ensure fit).
  • Kerning Preservation: Converts complex spacing arrays ([ (A) 50 (V) ] TJ) into explicit positioning (Td) if necessary to maintain exact spacing.
  • TrueType Font Embedding: Embeds the target font file into the resulting PDF.

Installation

pip install swapfont

Quick Start (The Wizard)

The easiest way to use swapfont is the interactive wizard. It scans your PDF and lets you pick replacements font-by-font.

swapfont wizard input.pdf

Note: the interactive wizard is a work in progress, and the user interface is not yet particularly pleasant.

CLI Reference

swapfont is a single binary with multiple subcommands.

1. Run (The Core Replacer)

Executes a replacement using a configuration file. Ideally used for automation pipelines.

swapfont run input.pdf config.json [-o output.pdf]

2. Inspect

Analyzes a PDF to identify unembedded or Type 3 fonts and generates a "skeleton" configuration file for you to edit.

swapfont inspect input.pdf

3. Glue

An intermediate tool to generate a swapfont compatible JSON from the inspector's output, filtering only for the fonts you provide mappings for.

swapfont glue inspector_output.json output_config.json --mapping mapping.json

Configuration (config.json)

For automated pipelines (using swapfont run), you define replacements in a JSON file.

Example Configuration

{
  "description": "Standard replacement of Type 3 fonts with local TrueType files.",
  "rules": [
    {
      "source_font_name": "/R136",
      "target_font_file": "fonts/Roboto-Regular.ttf",
      "target_font_name": "Roboto-Regular"
    },
    {
      "source_font_name": "/R20",
      "target_font_file": "fonts/Arial.ttf",
      "target_font_name": "ArialMT",
      "strategy_options": {
          "min_scale": 50.0,
          "max_scale": 150.0
      }
    }
  ]
}

Replacement Rule Details

Field Type Description
source_font_name String The internal name of the font in the PDF's resources (e.g., /F1, /R136).
target_font_file String The path to the replacement TrueType/OpenType font file.
target_font_name String The new internal name to use in the PDF resources (e.g., /F_NEW_ROB).
strategy Literal scale_to_fit (Default): Calculates Tz scaling to match the original width.
strategy_options Object Defines the limits for horizontal scaling.
min_scale: Minimum allowed scaling % (Default: 50.0)
max_scale: Maximum allowed scaling % (Default: 200.0)
encoding_map Dictionary Advanced: Maps source character codes (e.g., "0x41") to target characters (e.g., "A"). Essential for Type 3 fonts with non-standard encodings.

Advanced Usage: The Inspection Workflow

If you have a complex PDF and don't know the font names:

  1. Inspect: Run swapfont inspect document.pdf. This generates a report and a font_rules.json template.
  2. Edit: Open font_rules.json. Fill in target_font_file for the fonts you want to replace. Remove the blocks for fonts you want to keep.
  3. Run: Execute swapfont run document.pdf font_rules.json.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swapfont-0.1.1.tar.gz (284.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swapfont-0.1.1-py3-none-any.whl (42.9 kB view details)

Uploaded Python 3

File details

Details for the file swapfont-0.1.1.tar.gz.

File metadata

  • Download URL: swapfont-0.1.1.tar.gz
  • Upload date:
  • Size: 284.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for swapfont-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3427c9400fb0c1bf017fa96eef509acde4c0b3e51fd7196ac05c230b19af0cbb
MD5 2a324b0b9ce3fea4a454259f7bfc5207
BLAKE2b-256 0d6789edf822bb0701064287fc7b21b99886b5b382345784d2c997bbf8b95f31

See more details on using hashes here.

File details

Details for the file swapfont-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: swapfont-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 42.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for swapfont-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 219a5df90b8b764af2f92f7dbf46c4dbcb7bfa1b939f13dcb942fe26d063ec3f
MD5 48d4907fc25236807d6e0c1bf2a8fe2c
BLAKE2b-256 6f6b66daf33c124e450035c6f9ab0bfe1bc8aeb4426e54463674d0dbfb0babad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page