/** * Print an article as PDF and stream it to the browser * * @param object $objArticle An article object */ protected function printArticleAsPdf($objArticle) { $objArticle->headline = $objArticle->title; $objArticle->printable = false; // Generate article $objArticle = new \ModuleArticle($objArticle); $strArticle = $this->replaceInsertTags($objArticle->generate()); $strArticle = html_entity_decode($strArticle, ENT_QUOTES, $GLOBALS['TL_CONFIG']['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, $objArticle); } } // Handle line breaks in preformatted text $strArticle = preg_replace_callback('@(<pre.*</pre>)@Us', 'nl2br_callback', $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'] = $GLOBALS['TL_CONFIG']['characterSet']; $l['a_meta_language'] = $GLOBALS['TL_LANGUAGE']; $l['w_page'] = 'page'; // Include library require_once TL_ROOT . '/system/config/tcpdf.php'; require_once TL_ROOT . '/system/library/TCPDF/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($objArticle->title); $pdf->SetSubject($objArticle->title); $pdf->SetKeywords($objArticle->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(standardize(ampersand($objArticle->title, false)) . '.pdf', 'D'); // Stop script execution exit; }
/** * Get articles on pages and return as array with page id as key * @param array * @return array */ public function getArticles($arrPages, $strColumn = '') { if (!is_array($arrPages)) { $arrPages = array($arrPages); } $objDatabase = \Database::getInstance(); $time = time(); $strWhere = "published=1 AND (start='' OR start<" . $time . ") AND (stop='' OR stop>" . $time . ")" . ($strColumn ? " AND inColumn='" . $strColumn . "'" : ""); $objArticles = $objDatabase->execute("SELECT * FROM tl_article WHERE pid IN(" . implode(',', $arrPages) . ") AND " . $strWhere . " ORDER BY sorting"); if ($objArticles->numRows < 1) { return array(); } $arrReturn = array(); while ($objArticles->next()) { // fix 2: generate the whole article section. The inserttag only generates the content. #$strHtml = $this->replaceInsertTags('{{insert_article::'.$objArticles->id.'}}'); $objRow = $objDatabase->prepare("SELECT * FROM tl_article WHERE id=?")->limit(1)->execute($objArticles->id); // handle teasers if ($objRow->showTeaser) { $objRow->multiMode = 1; } // mimic module article $tmp = new \ModuleArticle($objRow); $strHtml = $tmp->generate(false); // handle empty articles if (!strlen($strHtml)) { // generate an empty article $objArticleTpl = new \FrontendTemplate('mod_article'); $objArticleTpl->class = 'mod_article'; $objArticleTpl->elements = array(); $strHtml = $objArticleTpl->parse(); } $arrReturn[] = array('id' => $objArticles->id, 'pid' => $objArticles->pid, 'col' => $objArticles->inColumn, 'html' => $strHtml); } return $arrReturn; }
/** * Generate an article and return it as string * * @param mixed $varId The article ID or a Model object * @param boolean $blnMultiMode If true, only teasers will be shown * @param boolean $blnIsInsertTag If true, there will be no page relation * @param string $strColumn The name of the column * * @return string|boolean The article HTML markup or false */ public static function getArticle($varId, $blnMultiMode = false, $blnIsInsertTag = false, $strColumn = 'main') { /** @var \PageModel $objPage */ global $objPage; if (is_object($varId)) { $objRow = $varId; } else { if (!$varId) { return ''; } $objRow = \ArticleModel::findByIdOrAliasAndPid($varId, !$blnIsInsertTag ? $objPage->id : null); if ($objRow === null) { return false; } } // Check the visibility (see #6311) if (!static::isVisibleElement($objRow)) { return ''; } // Print the article as PDF if (isset($_GET['pdf']) && \Input::get('pdf') == $objRow->id) { // Backwards compatibility if ($objRow->printable == 1) { $objArticle = new \ModuleArticle($objRow); $objArticle->generatePdf(); } elseif ($objRow->printable != '') { $options = deserialize($objRow->printable); if (is_array($options) && in_array('pdf', $options)) { $objArticle = new \ModuleArticle($objRow); $objArticle->generatePdf(); } } } $objRow->headline = $objRow->title; $objRow->multiMode = $blnMultiMode; // HOOK: add custom logic if (isset($GLOBALS['TL_HOOKS']['getArticle']) && is_array($GLOBALS['TL_HOOKS']['getArticle'])) { foreach ($GLOBALS['TL_HOOKS']['getArticle'] as $callback) { static::importStatic($callback[0])->{$callback[1]}($objRow); } } $objArticle = new \ModuleArticle($objRow, $strColumn); $strBuffer = $objArticle->generate($blnIsInsertTag); // Disable indexing if protected if ($objArticle->protected && !preg_match('/^\\s*<!-- indexer::stop/', $strBuffer)) { $strBuffer = "\n<!-- indexer::stop -->" . $strBuffer . "<!-- indexer::continue -->\n"; } return $strBuffer; }
protected function getContentFromArticle($id, $type = "") { $tmpContent = ""; $thumb = ""; $sharelink = ""; $retArr = array(); $time = time(); $base = \Environment::get('base'); $table = ""; if ($type == 'news') { $table = "tl_news"; } else { if ($table == 'event') { $table = "tl_calendar_events"; } else { $table = "tl_article"; } } $openComment = $type == 'news' ? "open" : "closed"; $commentCount = 0; $result = ""; $query = "SELECT * FROM `{$table}` WHERE `id` = '" . $id . "' "; $articleRes = \Database::getInstance()->query($query)->fetchAssoc(); if ($type == "news") { switch ($articleRes['source']) { case 'external': $sharelink = $articleRes['url']; break; case 'article': $sharelink = $base . \Controller::replaceInsertTags("{{article_url::" . $articleRes['articleId'] . "}}"); break; case 'internal': $sharelink = $base . \Controller::replaceInsertTags("{{link_url::" . $articleRes['jumpTo'] . "}}"); break; default: $sharelink = $base . \Controller::replaceInsertTags("{{news_url::" . $articleRes['id'] . "}}"); break; } $newsModel = \NewsModel::findPublishedByParentAndIdOrAlias($id, array(0 => $articleRes['pid'])); $result = $this->parseNews($newsModel); } else { if ($type == "event") { $objEvent = \CalendarEventsModel::findPublishedByParentAndIdOrAlias($id, array($this->settings['calendar'])); $objTemplate = new \FrontendTemplate("event_full"); $objTemplate->setData($objEvent->row()); $objTemplate->date = $date; $objTemplate->start = $intStartTime; $objTemplate->end = $intEndTime; $objTemplate->class = $objEvent->cssClass != '' ? ' ' . $objEvent->cssClass : ''; $objTemplate->recurring = $recurring; $objTemplate->until = $until; $objTemplate->locationLabel = $GLOBALS['TL_LANG']['MSC']['location']; $objTemplate->details = ''; $objElement = \ContentModel::findPublishedByPidAndTable($objEvent->id, 'tl_calendar_events'); if ($objElement !== null) { while ($objElement->next()) { $objTemplate->details .= $this->getContentElement($objElement->id); } } $objTemplate->addImage = false; if ($objEvent->addImage && $objEvent->singleSRC != '') { $objModel = \FilesModel::findByUuid($objEvent->singleSRC); if (is_file(TL_ROOT . '/' . $objModel->path)) { $arrEvent = $objEvent->row(); $arrEvent['singleSRC'] = $objModel->path; $this->addImageToTemplate($objTemplate, $arrEvent); } } $objTemplate->enclosure = array(); if ($objEvent->addEnclosure) { $this->addEnclosuresToTemplate($objTemplate, $objEvent->row()); } $result = $objTemplate->parse(); } else { $result = \ArticleModel::findByIdOrAliasAndPid($id, $articleRes['pid']); } } $sharelink = $sharelink == "" ? $base . \Controller::replaceInsertTags("{{link_url::" . $articleRes['pid'] . "}}") : $sharelink; if ($result != "") { if ($type != "news" && $type != "event") { $pageTitleRes = \Controller::getPageDetails($articleRes['pid']); $objArticle = new \ModuleArticle($result); $tmpContent = $objArticle->generate(true); } else { $tmpContent = $result; } $tmpContent = \Controller::replaceInsertTags($tmpContent); $tmpContent = str_replace('src="files/', 'src="' . $base . 'files/', $tmpContent); $tmpContent = str_replace('src="assets/', 'src="' . $base . 'assets/', $tmpContent); $tmpContent = str_replace('href="index.php/', 'href="' . $base . 'index.php/', $tmpContent); if ($pageTitleRes->thumb) { $thumb = $this->getFilePath($pageTitleRes->thumb); } if ($openComment == "open") { $commentCount = \CommentsModel::countPublishedBySourceAndParent("tl_news", $id); } $retArr['tstamp'] = time(); $retArr['img']['src'] = ""; $retArr['img']['thumb'] = $thumb; $retArr['pid'] = $id; $retArr['title'] = $type != "news" ? $pageTitleRes->title : $articleRes['headline']; $retArr['commentStatus'] = $openComment; $retArr['commentCount'] = $commentCount; $retArr['sharelink'] = $sharelink; $tmpContent = str_replace(']]>', ']]>', $tmpContent); $tmpContent = str_replace("\r\n", '\\n', $tmpContent); $retArr['content'] = preg_replace('/[\\x00-\\x1F\\x80-\\x9F]/u', '', $tmpContent); } else { $retArr = false; } return $retArr; }