Outlines Define How Bookmark Outlines Should Be Handled

Introduction

The Core component already offers a great feature to access bookmark outlines. The Merger component allows you to automatically create a document outline by the $outlinesConfig parameter in the  addFile() and addDocument() methods.

It allows you to create a flat oulines tree but also allows you to create parent/child structures and will allow you to import existing bookmark outlines. 

Create an Outline Item Per File/Document

The easiest way to define an outline item for an added file/document is to simply pass the name you want to see in the outline: 

PHP
<?php
require_once('library/SetaPDF/Autoload.php');

$merger = new \SetaPDF_Merger();
// add a file from the file system
$merger->addFile(array(
    'filename' => 'files/pdfs/camtown/products/Boombastic-Box.pdf',
    'outlinesConfig' => 'Boombastic Box'
));

// merge them together
$merger->merge();

// get the resulting document instance
$document = $merger->getDocument();
// open the outlines panel by default
$document->getCatalog()->setPageMode(\SetaPDF_Core_Document_PageMode::USE_OUTLINES);
// add a writer
$document->setWriter(new \SetaPDF_Core_Writer_Http('outline-1.pdf', true));
// save and finish
$document->save()->finish();

By default the destination that is created will use the fit to page zoom. This can be adjusted since revision 1330 by a fitMode key in the $outlinesConfig array. You can pass an additional array structure which will be forwarded to the SetaPDF_Core_Document_Destination::createDestinationArray() method internally:

PHP
$merger->addFile([
    'filename' => 'files/pdfs/tektown/products/Fantastic-Speaker.pdf',
    'outlinesConfig' => [
        \SetaPDF_Merger::OUTLINES_TITLE => 'Link to Fantastic-Speaker.pdf',
        \SetaPDF_Merger::OUTLINES_FIT_MODE => [\SetaPDF_Core_Document_Destination::FIT_MODE_XYZ, 100, 841.890, 4]
    ]
]);

Structure Outline Items

It is also possible to append the outline items as childs of an existing outline item:

PHP
<?php
require_once('library/SetaPDF/Autoload.php');

$document = new \SetaPDF_Core_Document(
    new \SetaPDF_Core_Writer_Http('outline-2.pdf', true)
);

// create a root outline item
$outlines = $document->getCatalog()->getOutlines();
$root = \SetaPDF_Core_Document_OutlinesItem::create($document, 'Products');
$outlines->appendChild($root);

// initiate the merger with the prepared document instance
$merger = new \SetaPDF_Merger($document);

// add files from the file system
$merger->addFile(array(
    'filename' => 'files/pdfs/camtown/products/Boombastic-Box.pdf',
    'outlinesConfig' => array(
        \SetaPDF_Merger::OUTLINES_TITLE => 'Boombastic Box',
        \SetaPDF_Merger::OUTLINES_PARENT => $root
    )
));

// let's remember the item (id)
$parent = $merger->addFile(array(
    'filename' => 'files/pdfs/camtown/products/Fantastic-Speaker.pdf',
    'outlinesConfig' => array(
        \SetaPDF_Merger::OUTLINES_TITLE => 'Fantastic Speaker',
        \SetaPDF_Merger::OUTLINES_PARENT => $root
    )
));

// now add this item below the previous one
$merger->addFile(array(
    'filename' => 'files/pdfs/camtown/products/Noisy-Tube.pdf',
    'outlinesConfig' => array(
        \SetaPDF_Merger::OUTLINES_TITLE => 'Noisy Tube',
        \SetaPDF_Merger::OUTLINES_PARENT => $parent
    )
));

// merge them together
$merger->merge();

// open the outlines panel by default
$document->getCatalog()->setPageMode(\SetaPDF_Core_Document_PageMode::USE_OUTLINES);
// save and finish
$document->save()->finish();

Import an Existing Outline

The SetaPDF-Merger component allows you to import an existing bookmark outline from a document/file that was added to the instance. The bookmark outline can be appended to the root of the outline or below an individual outline item: 

PHP
<?php
require_once('library/SetaPDF/Autoload.php');

$merger = new \SetaPDF_Merger();

// copy the outline below a new item
$merger->addFile(array(
    'filename' => 'files/pdfs/Brand-Guide.pdf',
    'outlinesConfig' => array(
        \SetaPDF_Merger::OUTLINES_COPY => \SetaPDF_Merger::COPY_OUTLINES_AS_CHILDS,
        \SetaPDF_Merger::OUTLINES_TITLE => 'Brand Guide'
    )
));

// copy the outline to the root
$merger->addFile(array(
    'filename' => 'files/pdfs/tektown/Terms-and-Conditions.pdf',
    'outlinesConfig' => array(
        \SetaPDF_Merger::OUTLINES_COPY => \SetaPDF_Merger::COPY_OUTLINES_TO_ROOT
    )
));

// merge them together
$merger->merge();

// get the resulting document instance
$document = $merger->getDocument();
// open the outlines panel by default
$document->getCatalog()->setPageMode(\SetaPDF_Core_Document_PageMode::USE_OUTLINES);
// add a writer
$document->setWriter(new \SetaPDF_Core_Writer_Http('outline-3.pdf', true));
// save and finish
$document->save()->finish();