SetaPDF-MetaManager Implement identical functionality as the SetaPDF-MetaManager offered

Introduction

As of SetaPDF 2 the so called SetaPDF-MetaManager API is obsolete, because all features are available in the Core component.

This article will show replacement code for specific methods of the old API.

Error Handling

While in version 1 error objects were returned, version 2 uses Exceptions throughout. More details can be found here.  

Loading and Saving PDF Documents

The SetaPDF-MetaManager offered only the possibility to load a PDF file from a file. The new system allows a PDF document to be read from various sources through so called Reader objects.

Nearly same things exists for the resulting document: SetaPDF_Core_Writer_WriterInterface

PHP
$filename = 'a/path/to/a/Pdf/file.pdf';
$target = 'a/path/to/the/resulting/file.pdf';
     
// old version
$metaManager = SetaPDF_MetaManager::factory($filename);
// ...
$metaManager->output($target);
     
// will become:
$reader = new SetaPDF_Core_Reader_File($filename);
$writer = new SetaPDF_Core_Writer_File($target);
$document = SetaPDF_Core_Document::load($reader, $writer);
// ...
$document->save()->finish();

In the new version it is possible to define separate readers or writers, which was impossible for the old version.

Equivalent Code of Methods

The old methods can be refactored to the new version as follows: 

setUseUpdate()

The behavior of the setUseUpdate() method is identical to the $update parameter of the SetaPDF_Core_Document::save() method.

PHP
$update = false;
// old
$metaManager->setUseUpdate($update);
// new
$document->save($update);

getEncryption()

This method is not fully compatible but the main information, if a document is encrypted, can be get like this:

PHP
// old:
$encrypted = $metaManager->getEncryption();
// new:
$encrypted = $document->hasSecHandler();

getPageCount()

PHP
// old:
$pageCount = $metaManager->getPageCount();
// new:
$pageCount = $document->getCatalog()->getPages()->count();

getPageData()

In the new version this data can be received through page objects and specific methods. All boxes returned by SetaPDF_Core_Document_Page::getBoundary() are of a special object type SetaPDF_Core_DataStructure_Rectangle

PHP
// old:
$mediaBox = $metaManager->getPageData(1, 'MediaBox');
// new:
$mediaBoxBoundary = $document->getCatalog()->getPages()->getPage(1)->getBoundary(SetaPDF_Core_PageBoundaries::MEDIA_BOX);
$width = $mediaBoxBoundary->getWidth();
$height = $mediaBoxBoundary->getHeight();
$llx = $mediaBoxBoundary->getLlx();

The rotation value is also available from a pages object: SetaPDF_Core_Document_Page::getRotation().

PHP
// old:
$rotation = $metaManager->getPageData(1, 'Rotation');
// new:
$rotation = $document->getCatalog()->getPages()->getPage(1)->getRotation();

The LastModified key is available through a page objects method, too: SetaPDF_Core_Document_Page::getLastModified().

PHP
// old:
$lastModified = $metaManager->getPageData(1, 'LastModified');
// new:
$lastModified = $document->getCatalog()->getPages()->getPage(1)->getLastModified();

setPageData()

For all get-methods equivalent set-methods are available:

setBoundary()

Set a boundary box.

setLastModified()

Set the date and time the page was edited.

setRotation()

Set the page rotation.

getInfoData()

The handling of the information dictionary is encapsulated by helper class in the new version: SetaPDF_Core_Document_Info

PHP
// old:
$allInfoData = $metaManager->getInfoData();
$author = $metaManager->getInfoData('Author');
// new:
$allInfoData = $document->getInfo()->getAll();
$author = $document->getInfo()->getAuthor();

setInfoData()

The helper class SetaPDF_Core_Document_Info offers methods for setting specific information values. 

PHP
// old:
$metaManager->setInfoData('Author', 'My Name');
// new:
$document->getInfo()->setAuthor('My Name');

getDocumentMetadata()

The document metadata can be accessed with the SetaPDF_Core_Document_Catalog class. 

PHP
// old:
$metadata = $metaManager->getDocumentMetadata();
// new:
$metadata = $document->getCatalog()->getMetadata();

setDocumentMetadata()

PHP
$xmp = ...
// old:
$metaManager->setDocumentMetadata($xmp);
// new:
$document->getCatalog()->setMetadata($xmp);

Unsetting the metadata stream differs: The new version requires the parameter to be null instead of false (old version).

getDocumentIds()

A simliar method is now available in the document instance, too: SetaPDF_Core_Document::getFileIdentifier(). This method only returns plain text values in the new version.

PHP
// old
$ids = $metaManager->getDocumentIds(true);
// new
$permanentIdentifier = $document->getFileIdentifier(true);
$nonPermanentIdentifier =  $document->getFileIdentifier(false);

Setting the new file identifier was possible in the output() method of the old version. The new version offers a SetaPDF_Core_Document::setNewFileIdentifier() method for it.

PHP
$ownNewIdentifier = ...;
// old
$metaManagher->output('doc.pdf', 'F', false, $ownNewIdentifier);
// new
$document->setNewFileIdentifier($ownNewIdentifier);