PDF Function

A PDF Function is a mathematical mapping object that converts one or more input values to one or more output values. Functions appear throughout the PDF specification as the building blocks for gradients (ShadingType), transfer functions, colour space conversions, and soft mask tone curves. PDF defines five function types: sampled, exponential, stitching, PostScript calculator, and identity.

A PDF Function is a mathematical object that maps one or more numeric input values to one or more numeric output values. Functions are not a PDF-specific invention — they are built from standard mathematical concepts like sampled lookup tables, exponential curves, and PostScript arithmetic. But within PDF, they serve as the universal building blocks for a remarkably wide range of visual effects: colour gradients, transparency curves, halftone dot gain compensation, and soft mask opacity shaping.

Understanding PDF functions is essential for anyone working with advanced PDF features — particularly shading patterns (gradients), <a href="https://pdftheguide.com/glossary/pdf-transfer-function" target="_blank" rel="noopener">transfer functions</a>, and <a href="https://pdftheguide.com/glossary/pdf-soft-mask" target="_blank" rel="noopener">soft mask tone mapping</a>.

The five PDF function types

The PDF specification defines five function types, each suited to different use cases:

**Type 0 — Sampled (<code>/FunctionType 0</code>)**: A lookup table (LUT) of evenly-spaced input-output sample pairs. The PDF reader interpolates between samples for values that fall between the table entries. Type 0 is the most general function type — any mapping can be approximated with sufficient samples. It is the function type used most often for complex colour gradients where the relationship between input and output is not easily expressed analytically. Sample values are stored in the function's stream data, making Type 0 functions compact and efficient.

**Type 2 — Exponential (<code>/FunctionType 2</code>)**: Computes an exponential curve: <code>f(x) = C0 + x^N × (C1 − C0)</code>, where C0 is the output at x=0, C1 is the output at x=1, and N is the exponent. When N=1, this is a simple linear interpolation between C0 and C1. Type 2 functions are widely used for smooth two-colour gradients — the vast majority of simple gradient fills in PDF documents use Type 2 functions.

**Type 3 — Stitching (<code>/FunctionType 3</code>)**: A composite function that partitions the input domain into sub-intervals and delegates each to a different sub-function. Used to construct multi-colour gradients (e.g., a rainbow gradient) by stitching together multiple Type 2 exponential functions, one for each colour transition.

**Type 4 — PostScript Calculator (<code>/FunctionType 4</code>)**: A function expressed as a PostScript-language program in RPN (Reverse Polish Notation) arithmetic. Type 4 provides arbitrary mathematical expressibility — any function computable in PostScript can be encoded. Used for complex colour space conversions and custom transfer functions. Less common in modern PDFs due to security concerns about embedded executable code.

**Type 1 — Identity (name <code>/Identity</code>)**: Not a function type number but a name — used in some contexts (like ToUnicode CMaps and some shading patterns) to indicate an identity mapping where output equals input.

Functions in shading patterns (gradients)

<a href="https://pdftheguide.com/glossary/pdf-content-stream" target="_blank" rel="noopener">PDF content streams</a> support gradient fills via shading patterns (ShadingType 2 for axial gradients, ShadingType 3 for radial gradients, ShadingType 4–7 for mesh gradients). All shading types use a PDF function to map position (a value from 0 to 1 along the gradient axis) to colour values.

For a simple two-colour linear gradient from blue (0,0,1) to red (1,0,0): - A Type 2 exponential function with C0=[0,0,1], C1=[1,0,0], N=1 interpolates linearly between the two colours - The axial shading (ShadingType 2) references this function and provides the geometric axis of the gradient

For a multi-stop gradient (e.g., blue → white → red): - A Type 3 stitching function divides the [0,1] domain into [0,0.5] and [0.5,1] - Each sub-interval delegates to a Type 2 function (blue→white and white→red respectively)

Design tools like Illustrator and InDesign construct these function hierarchies automatically when generating gradient PDF output. Understanding the structure helps when debugging gradient rendering differences across PDF viewers.

Functions in colour spaces

Several PDF colour spaces use functions for colour conversion:

**DeviceN and Separation colour spaces**: Custom ink channels (spot colours like Pantone or metallic inks) use an alternate space and a tintTransform function that converts spot ink coverage (0–1) to equivalent values in the alternate colour space (typically CMYK or sRGB). This allows devices that cannot print the spot colour to render an approximate equivalent.

**CalRGB and Lab colour spaces**: These CIE-based colour spaces use predefined mathematical relationships (not PDF function objects) for conversion, but the principle is similar — precise mathematical mappings rather than device-dependent assumptions.

**ICCBased colour spaces**: Use an embedded ICC profile for conversion — ICC profiles are themselves lookup-table-based functions with multiple dimensions, analogous to very high-dimensional Type 0 PDF functions.

For the <a href="https://pdftheguide.com/news/pdf-color-management" target="_blank" rel="noopener">colour management workflow</a>, understanding that all colour conversions are ultimately function evaluations helps explain why seemingly identical colours can render differently across different PDF rendering pipelines that evaluate these functions with different precision or interpolation strategies.

Functions and performance

PDF function evaluation happens at render time — the PDF reader evaluates the function for each pixel or sample point during rendering. The performance implications vary by function type:

- **Type 2 (exponential)**: Extremely fast — simple arithmetic, no table lookup. - **Type 3 (stitching)**: Fast — a few comparisons to find the right sub-interval, then a Type 2 evaluation. - **Type 0 (sampled)**: Fast if the sample table is reasonably sized (256–1024 samples per dimension). Very large multi-dimensional tables can be slow to load and interpolate. - **Type 4 (PostScript calculator)**: Slow in some implementations — PostScript interpretation is more complex than arithmetic. Security-conscious readers may also sandbox or reject Type 4 functions due to the embedded code concern.

For PDF documents intended to render quickly in browser-based viewers (PDF.js, Chrome PDFium), prefer Type 2 and Type 3 functions for gradients. Type 4 functions should be avoided for performance and security reasons.

Frequently asked questions

Where do PDF functions appear in a PDF file?

PDF functions appear as objects referenced from specific dictionary entries. Common locations: the /Function key in a shading dictionary (for gradient fills), the /TR or /TR2 keys in a graphics state parameter dictionary (for transfer functions), the /TintTransform key in a Separation or DeviceN colour space array (for spot colour conversion), and the /TR key in a soft mask dictionary (for mask tone curves). Functions are typically indirect objects to allow referencing from multiple contexts.

What is the difference between a Type 2 and a Type 0 PDF function?

A Type 2 function computes an exponential curve analytically — no table storage needed, evaluation is a single formula. It can only represent relationships where the output varies according to an exponential curve from C0 to C1. A Type 0 function stores a discrete lookup table of input-output pairs and interpolates between them — it can represent any mapping, at the cost of storing the sample data. Type 2 is compact and precise for smooth gradient transitions; Type 0 is more flexible for complex non-exponential relationships.

Are PDF Type 4 (PostScript calculator) functions supported in all viewers?

Not universally. Type 4 functions contain executable PostScript code, which raises security and implementation complexity concerns. Adobe Acrobat fully supports them. PDF.js (Firefox) supports a subset of the PostScript operators used in Type 4 functions. Some security-focused PDF viewers sandbox or reject Type 4 functions. For maximum compatibility, avoid Type 4 functions in documents intended for broad distribution. Transfer functions that require Type 4 can often be approximated with a Type 0 sampled function instead.

How do PDF functions relate to gradients in design tools?

When you create a gradient fill in Illustrator, InDesign, or Affinity Publisher and export to PDF, the software converts your gradient stops into PDF function objects. A two-stop gradient becomes a Type 2 exponential function. A multi-stop gradient becomes a Type 3 stitching function with one Type 2 sub-function per adjacent stop pair. The gradient shape (linear, radial, mesh) determines the ShadingType; the colour interpolation is encoded in the function(s). This conversion happens automatically — designers rarely need to interact with PDF functions directly.