/** * Builds final pdf * * @return mPDF * @throws \Exception */ private function build() { if (empty($this->documentTitle)) { throw new \Exception("Var 'documentTitle' cannot be empty."); } if ($this->ignoreStylesInHTMLDocument) { if (!class_exists('Symfony\\Component\\DomCrawler\\Crawler')) { throw new MissingServiceException("Class 'Symfony\\Component\\DomCrawler\\Crawler' not found. Try composer-require 'symfony/dom-crawler'."); } if (!class_exists('Symfony\\Component\\CssSelector\\CssSelector')) { throw new MissingServiceException("Class 'Symfony\\Component\\CssSelector\\CssSelector' not found. Try composer-require 'symfony/css-selector'."); } } if ($this->generatedFile) { // singleton return $this->generatedFile; } if ($this->source instanceof ITemplate || $this->source instanceof Template) { $html = $this->source->__toString(); } else { $html = $this->source; } // Fix: $html can't be empty (mPDF generates Fatal error) if (empty($html)) { $html = '<html><body></body></html>'; } $mpdf = $this->getMPDF(); $mpdf->biDirectional = $this->multiLanguage; $mpdf->SetAuthor($this->documentAuthor); $mpdf->SetTitle($this->documentTitle); $mpdf->SetDisplayMode($this->displayZoom, $this->displayLayout); $mpdf->showImageErrors = true; // Add styles if (!empty($this->styles)) { $mpdf->WriteHTML($this->styles, 1); } // copied from mPDF -> removes comments $html = preg_replace('/<!--mpdf/i', '', $html); $html = preg_replace('/mpdf-->/i', '', $html); $html = preg_replace('/<\\!\\-\\-.*?\\-\\->/s', '', $html); // @see: http://mpdf1.com/manual/index.php?tid=121&searchstring=writeHTML if ($this->ignoreStylesInHTMLDocument) { // deletes all <style> tags $crawler = new Crawler($html); foreach ($crawler->filter('style') as $child) { $child->parentNode->removeChild($child); } $html = $crawler->html(); $mode = 2; // If <body> tags are found, all html outside these tags are discarded, and the rest is parsed as content for the document. If no <body> tags are found, all html is parsed as content. Prior to mPDF 4.2 the default CSS was not parsed when using mode #2 } else { $mode = 0; // Parse all: HTML + CSS } // Add content $mpdf->WriteHTML($html, $mode); $mpdf->page = count($mpdf->pages); //set pointer to last page to force render of all pages $this->onBeforeComplete($mpdf); $this->generatedFile = $mpdf; return $this->generatedFile; }