Skip to content

Support linear-gradient() and radial-gradient() as background images (CPDF) - #3822

Open
Niksac wants to merge 3 commits into
dompdf:masterfrom
Niksac:css-linear-gradient
Open

Niksac wants to merge 3 commits into
dompdf:masterfrom
Niksac:css-linear-gradient

Conversation

@Niksac

@Niksac Niksac commented Nov 15, 2026

Copy link
Copy Markdown

Adds linear-gradient(), radial-gradient() and their repeating- variants for background-image and the background shorthand. Closes #1912.

Gradients are written as native PDF shadings, so the output stays vector. Transparent stops use a soft mask.

Changes

  • Cpdf::gradient() paints a shading over the current clipping area.
  • Adapter\CPDF::gradient() fills a rectangle with it.
  • The new class Css\Gradient parses a value and resolves it for a box. Angles, sides and corners, circle and ellipse sizes and positions, calc() stops, color hints, currentcolor and alpha are supported.
  • An invalid gradient is dropped, so an earlier valid declaration stays in effect.

Limitations

  • background-size, -position and -repeat are ignored. The gradient always covers the whole box.
  • One gradient per element, no multiple background layers.
  • No conic-gradient(), no vendor-prefixed syntax.
  • CPDF only. The renderer checks for the method with method_exists(), so the Canvas interface is unchanged. Other backends still paint the background color.
  • A gradient on a table row is painted per cell.
  • Repeating gradients store every repetition in the PDF. Above 500 repetitions the average color is used.

Tests

Unit tests for parsing, resolution and the Cpdf output, plus four output tests. The reference PDFs were generated with dompdf, like the existing ones.

Disclaimer:
Most of the code was written by AI (Claude) and reviewed / tested by me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support linear-gradient

2 participants