public function generatePdf()
{
$this->headline = $this->title;
$this->printable = false;
// Generate article
$strArticle = $this->replaceInsertTags($this->generate(), false);
$strArticle = html_entity_decode($strArticle, ENT_QUOTES, \Config::get('characterSet'));
$strArticle = $this->convertRelativeUrls($strArticle, '', true);
// Remove form elements and JavaScript links
$arrSearch = array('@<form.*</form>@Us', '@<a [^>]*href="[^"]*javascript:[^>]+>.*</a>@Us');
$strArticle = preg_replace($arrSearch, '', $strArticle);
// HOOK: allow individual PDF routines
if (isset($GLOBALS['TL_HOOKS']['printArticleAsPdf']) && is_array($GLOBALS['TL_HOOKS']['printArticleAsPdf'])) {
foreach ($GLOBALS['TL_HOOKS']['printArticleAsPdf'] as $callback) {
$this->import($callback[0]);
$this->{$callback[0]}->{$callback[1]}($strArticle, $this);
}
}
// URL decode image paths (see #6411)
$strArticle = preg_replace_callback('@(src="[^"]+")@', function ($arg) {
return rawurldecode($arg[0]);
}, $strArticle);
// Handle line breaks in preformatted text
$strArticle = preg_replace_callback('@(<pre.*</pre>)@Us', function ($arg) {
return str_replace("\n", '<br>', $arg[0]);
}, $strArticle);
// Default PDF export using TCPDF
$arrSearch = array('@<span style="text-decoration: ?underline;?">(.*)</span>@Us', '@(<img[^>]+>)@', '@(<div[^>]+block[^>]+>)@', '@[\\n\\r\\t]+@', '@<br( /)?><div class="mod_article@', '@href="([^"]+)(pdf=[0-9]*(&|&)?)([^"]*)"@');
$arrReplace = array('<u>$1</u>', '<br>$1', '<br>$1', ' ', '<div class="mod_article', 'href="$1$4"');
$strArticle = preg_replace($arrSearch, $arrReplace, $strArticle);
// TCPDF configuration
$l['a_meta_dir'] = 'ltr';
$l['a_meta_charset'] = \Config::get('characterSet');
$l['a_meta_language'] = substr($GLOBALS['TL_LANGUAGE'], 0, 2);
$l['w_page'] = 'page';
// Include library
require_once __DIR__ . '/../config/tcpdf.php';
// Create new PDF document
$pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true);
// Set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor(PDF_AUTHOR);
$pdf->SetTitle($this->title);
$pdf->SetSubject($this->title);
$pdf->SetKeywords($this->keywords);
// Prevent font subsetting (huge speed improvement)
$pdf->setFontSubsetting(false);
// Remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// Set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
// Set auto page breaks
$pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM);
// Set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// Set some language-dependent strings
$pdf->setLanguageArray($l);
// Initialize document and add a page
$pdf->AddPage();
// Set font
$pdf->SetFont(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN);
// Write the HTML content
$pdf->writeHTML($strArticle, true, 0, true, 0);
// Close and output PDF document
$pdf->lastPage();
$pdf->Output(\StringUtil::standardize(ampersand($this->title, false)) . '.pdf', 'D');
// Stop script execution
exit;
}