protected function getGraphicsContextFromSourceDocument(Document $document) { $fileOfSourcePage = $this->getAttribute('document-template'); if ($fileOfSourcePage) { $engine = $document->loadEngine($fileOfSourcePage, $this->getEncoding()); $graphicsContexts = $engine->getAttachedGraphicsContexts(); $count = count($graphicsContexts); if ($count == 0) { return null; } $pageContext = $this->context; $index = ($pageContext ? $pageContext->getPageNumber() - 1 : 0) % $count; return $graphicsContexts[$index]; } return null; }