Skip to content

Feature: Reuse SVG or vector graphics across pages using PdfFormXObject #1888

@theGuen

Description

@theGuen

Is your feature request related to a problem? Please describe.

Currently, when rendering repeated vector backgrounds such as SVG business paper templates, the Dart PDF library embeds the full SVG content on each page individually.
This approach significantly increases the resulting PDF file size and slows down generation performance because the same complex vector data is reprocessed and duplicated per page.
In my use case (an invoice generation service), this led to large PDFs and slower throughput.

Describe the solution you'd like

I implemented an extension to the library that introduces a way to render SVGs into a PdfFormXObject and reuse it across multiple pages.

This involves:

Adding a new helper SvgPdfFormXObject class that creates a reusable XForm object from an SVG.

Extending PdfGraphics with a drawXObject() method that can draw a registered form XObject on any page using the Do operator.

The form XObject is registered once in the document and referenced multiple times, drastically reducing document size and improving rendering speed.

In my benchmarks, this optimization increased generation performance from 52 PDFs/sec to 63 PDFs/sec for two page invoices.

Describe alternatives you've considered

Re-rendering the SVG background on every page (current behavior): functional but highly inefficient.

Using raster images (PNG backgrounds): smaller performance impact, but significant loss of vector quality and scalability.

Additional context

This feature would enable efficient reuse of any static vector or graphic asset across pages, not just SVGs — for example, watermarks, headers, or corporate templates.

I’ve already implemented this in a local fork and verified performance and correctness.
I would be happy to open a Pull Request to contribute this functionality upstream.

Example usage:

final bgForm = SvgPdfFormXObject(doc, svgBytes, width, height);
for (final page in pages) {
  final g = page.getGraphics();
  g.drawXObject(bgForm, 0, 0, w: width, h: height);
}

This keeps the SVG vector quality intact while reusing the same object reference for all pages.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions