mutate_content

Mutate page content streams using a user-supplied Python script

Usage

pdftl <input> mutate_content <script.py[::func]> [<script_args>...] output <output>

Details

The mutate_content operation allows you to run custom Python logic against the raw instruction streams of a PDF. You can specify your own Python script, and the function from that script to run. The default function search for if no function is specified is mutate.

The Context Dictionary

Your function receives a context dictionary with the following keys:

  • pdf: The current pikepdf.Pdf object.

  • page_num: The current page number (1-indexed) or None for XObjects.

  • is_xobject: Boolean, True if mutating a Form XObject.

  • object: the pikepdf.Object (either a page or an XObject) containing the content stream.

  • args: A list of extra arguments passed via CLI after the script name.

Passing Arguments

You can pass custom parameters to your script: pdftl in.pdf mutate_content script.py 1.5 2.0 output out.pdf In this case, context['args'] would be ['1.5', '2.0'].

Examples

Mutate content streams in all pages using the mutate function in mutate_grayscale.py

pdftl in.pdf mutate_content mutate_grayscale.py output out.pdf

Fix hairlines using mutate_hairline_fixer.py, passing the script the argument ‘0.5’

pdftl in.pdf mutate_content mutate_hairline_fixer.py 0.5 output out.pdf

Tags: content_stream, custom, advanced

Source: pdftl.operations.mutate_content

Read online: https://pdftl.readthedocs.io/en/stable/operations/mutate_content.html

Type: Operation