/** * The constructor. * * @param Alpha\Model\ActiveRecord $BO * @param bool $useCache * * @since 1.0 */ public function __construct($BO, $useCache = true) { $config = ConfigProvider::getInstance(); $this->BO = $BO; if ($this->BO instanceof \Alpha\Model\Article && $this->BO->isLoadedFromFile()) { $underscoreTimeStamp = str_replace(array('-', ' ', ':'), '_', $this->BO->getContentFileDate()); $this->filename = $config->get('app.file.store.dir') . 'cache/html/' . get_class($this->BO) . '_' . $this->BO->get('title') . '_' . $underscoreTimeStamp . '.html'; } else { $this->filename = $config->get('app.file.store.dir') . 'cache/html/' . get_class($this->BO) . '_' . $this->BO->getID() . '_' . $this->BO->getVersion() . '.html'; } if (!$useCache) { $this->content = $this->markdown($this->BO->get('content', true)); } else { if ($this->checkCache()) { $this->loadCache(); } else { if ($this->BO->get('content', true) == '') { // the content may not be loaded from the DB at this stage due to a previous soft-load $this->BO->reload(); } $this->content = $this->markdown($this->BO->get('content', true)); $this->cache(); } } // Replace all instances of $attachURL in link tags to links to the ViewAttachment controller $attachments = array(); preg_match_all('/href\\=\\"\\$attachURL\\/.*\\"/', $this->content, $attachments); foreach ($attachments[0] as $attachmentURL) { $start = mb_strpos($attachmentURL, '/'); $end = mb_strrpos($attachmentURL, '"'); $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1)); if (method_exists($this->BO, 'getAttachmentSecureURL')) { $this->content = str_replace($attachmentURL, 'href="' . $this->BO->getAttachmentSecureURL($fileName) . '" rel="nofollow"', $this->content); } } // Handle image attachments $attachments = array(); preg_match_all('/\\<img\\ src\\=\\"\\$attachURL\\/.*\\.[a-zA-Z]{3}\\"[^<]*/', $this->content, $attachments); foreach ($attachments[0] as $attachmentURL) { preg_match('/\\/.*\\.[a-zA-Z]{3}/', $attachmentURL, $matches); $fileName = $matches[0]; if ($config->get('cms.images.widget')) { // get the details of the source image $path = $this->BO->getAttachmentsLocation() . $fileName; $image_details = getimagesize($path); $imgType = $image_details[2]; if ($imgType == 1) { $type = 'gif'; } elseif ($imgType == 2) { $type = 'jpg'; } elseif ($imgType == 3) { $type = 'png'; } $img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (bool) $config->get('cms.images.widget.secure')); $this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content); } else { // render a normal image link to the ViewAttachment controller if (method_exists($this->BO, 'getAttachmentSecureURL')) { $this->content = str_replace($attachmentURL, '<img src="' . $this->BO->getAttachmentSecureURL($fileName) . '">', $this->content); } } } }
/** * The constructor. * * @param Alpha\Model\ActiveRecord $BO the business object that stores the content will be rendered to Markdown * * @since 1.0 */ public function __construct($BO) { self::$logger = new Logger('TCPDFFacade'); self::$logger->debug('>>__construct()'); $config = ConfigProvider::getInstance(); $this->BO = $BO; $reflect = new \ReflectionClass($this->BO); $classname = $reflect->getShortName(); $this->PDFFilename = $config->get('app.file.store.dir') . 'cache/pdf/' . $classname . '_' . $this->BO->getID() . '_' . $this->BO->getVersion() . '.pdf'; $PDFDownloadName = str_replace(' ', '_', $this->BO->get('title') . '.pdf'); $this->HTMLFilename = $config->get('app.file.store.dir') . 'cache/html/' . $classname . '_' . $this->BO->getID() . '_' . $this->BO->getVersion() . '.html'; // first check the PDF cache if ($this->checkPDFCache()) { return; } if (method_exists($this->BO, 'getAttachmentsURL')) { $attachURL = $this->BO->getAttachmentsURL(); } else { $attachURL = ''; } if ($this->checkHTMLCache()) { $this->loadHTMLCache(); } else { $this->content = $this->markdown($this->BO->get('content', true), $attachURL); $this->HTMLCache(); } // Replace all instances of $attachURL in link tags to links to the ViewAttachment controller $attachments = array(); preg_match_all('/href\\=\\"\\$attachURL\\/.*\\"/', $this->content, $attachments); foreach ($attachments[0] as $attachmentURL) { $start = mb_strpos($attachmentURL, '/'); $end = mb_strrpos($attachmentURL, '"'); $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1)); if (method_exists($this->BO, 'getAttachmentSecureURL')) { $this->content = str_replace($attachmentURL, 'href=' . $this->BO->getAttachmentSecureURL($fileName), $this->content); } } // Handle image attachments $attachments = array(); preg_match_all('/\\<img\\ src\\=\\"\\$attachURL\\/.*\\".*\\>/', $this->content, $attachments); foreach ($attachments[0] as $attachmentURL) { $start = mb_strpos($attachmentURL, '/'); $end = mb_strrpos($attachmentURL, '" alt'); $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1)); if ($config->get('cms.images.widget')) { // get the details of the source image $path = $this->BO->getAttachmentsLocation() . '/' . $fileName; $image_details = getimagesize($path); $imgType = $image_details[2]; if ($imgType == 1) { $type = 'gif'; } elseif ($imgType == 2) { $type = 'jpg'; } elseif ($imgType == 3) { $type = 'png'; } $img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (bool) $config->get('cms.images.widget.secure')); $this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content); } else { // render a normal image link to the ViewAttachment controller if (method_exists($this->BO, 'getAttachmentSecureURL')) { $this->content = str_replace($attachmentURL, '<img src="' . $this->BO->getAttachmentSecureURL($fileName) . '">', $this->content); } } } $this->pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $this->pdf->SetCreator(PDF_CREATOR); $this->pdf->SetAuthor($this->BO->get('author')); $this->pdf->SetTitle($this->BO->get('title')); $this->pdf->SetSubject($this->BO->get('description')); //set margins $this->pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $this->pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $this->pdf->SetFooterMargin(PDF_MARGIN_FOOTER); //set auto page breaks $this->pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM); //set image scale factor $this->pdf->setImageScale(2.5); // add a page $this->pdf->AddPage(); // add the title $title = '<h1>' . $this->BO->get('title') . '</h1>'; // add some custom footer info about the article $footer = '<br><p>Article URL: <a href="' . $this->BO->get('URL') . '">' . $this->BO->get('URL') . '</a><br>Title: ' . $this->BO->get('title') . '<br>Author: ' . $this->BO->get('author') . '</p>'; // write the title self::$logger->debug('Writing the title [' . $title . '] to the PDF'); $this->pdf->writeHTML(utf8_encode($title), true, false, true, false, ''); // output the HTML content self::$logger->debug('Writing the content [' . $this->content . '] to the PDF'); $this->pdf->writeHTML(utf8_encode($this->content), true, false, true, false, ''); // write the article footer $this->pdf->writeHTML(utf8_encode($footer), true, false, true, false, ''); self::$logger->debug('Writing the footer [' . $footer . '] to the PDF'); // save this PDF to the cache $this->pdf->Output($this->PDFFilename, 'F'); self::$logger->debug('<<__construct()'); }