Skip to content

Layers (OCG)

PDF layers — formally called Optional Content Groups (OCG) — allow you to create content that can be toggled on and off in the viewer, or selectively included when printing. The layer system is managed through Graphics\LayerManager and accessed via the Document fluent API.

All methods return static, so every call can be chained.

Quick Reference

MethodPurpose
startLayer()Begin a new layer with a given name
endLayer()Close the current layer

Basic Example

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)

    // Visible in viewer, not printed
    ->startLayer('Screen Only', print: false, view: true)
    ->cell(0, 10, 'This text appears on screen but not when printed', newLine: true)
    ->endLayer()

    // Printed only
    ->startLayer('Print Only', print: true, view: false)
    ->cell(0, 10, 'This text appears only when printed', newLine: true)
    ->endLayer()

    // Always visible
    ->cell(0, 10, 'This text is always visible', newLine: true);

startLayer()

php
$pdf->startLayer(string $name, bool $print = true, bool $view = true): static

Begins a new layer. All content drawn after this call belongs to the layer until endLayer() is called.

ParameterTypeDescription
$namestringDisplay name shown in the viewer's layer panel
$printboolWhether the layer content appears when printed
$viewboolWhether the layer content appears on screen

endLayer()

Closes the current layer. Content drawn after this call is no longer part of any layer.

Layer Visibility Modes

The combination of $print and $view gives you four useful visibility modes:

$print$viewBehaviour
truetrueAlways visible (default)
falsetrueScreen only — hidden when printed
truefalsePrint only — hidden on screen
falsefalseInitially hidden everywhere (user can toggle)

Nested Layers

Layers can be nested. A child layer inherits the visibility constraints of its parent -- if the parent is hidden, the child is hidden too.

php
$pdf->startLayer('Parent')
    ->cell(0, 10, 'Parent content', newLine: true)
    ->startLayer('Child', print: false, view: true)
    ->cell(0, 10, 'Child content — screen only', newLine: true)
    ->endLayer()
    ->endLayer();

Use Cases

Multi-Language PDFs

Place each translation on a separate layer. Readers toggle languages in the layer panel:

php
$pdf->startLayer('English')
    ->cell(0, 10, 'Hello, World!', newLine: true)
    ->endLayer()
    ->startLayer('Chinese')
    ->cell(0, 10, '你好,世界!', newLine: true)
    ->endLayer();

Watermark (Screen-Only)

php
$pdf->startLayer('Watermark', print: false, view: true)
    ->setFont('Helvetica', 'B', 48)
    ->setTextColor(200, 200, 200)
    ->text(60, 140, 'DRAFT')
    ->endLayer();
php
$pdf->startLayer('Crop Marks', print: true, view: false)
    ->cropMark(20, 20, 10, 10)
    ->cropMark(190, 20, 10, 10)
    ->endLayer();

Tips

  • Layer names should be short and descriptive -- they appear verbatim in the viewer's layer panel.
  • Adobe Acrobat and Foxit Reader have full OCG support; browser-based viewers may ignore layers.
  • Layers add minimal overhead to file size since they are metadata flags, not duplicate content.

Released under the LGPL-3.0-or-later License.