Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @package HikaShop for Joomla!
- * @version 1.5.5
- */
- defined('_JEXEC') or die('Restricted access');
- ?>
- <?php
- class plgHikashopBranding extends JPlugin
- {
- function onBeforeDownloadFile(&$file,&$do) {
- try {
- if (substr($file, strlen($file) - 4) === ".pdf") {
- $user =& JFactory::getUser();
- $username = $user->username;
- $realname = $user->name;
- $fontSize = 12;
- $yTextPos = 10;
- $branding = "Licensed for: " . $realname;
- define('DS', DIRECTORY_SEPARATOR);
- define('JPATH_BASE', dirname(__FILE__) . DS . '..' . DS . '..' . DS . '..'); // assuming we need to go up 3 steps to get to the Joomla root
- set_include_path(dirname(__FILE__)); // set include_path for external library Zend Framework
- require_once(JPATH_BASE . DS . 'plugins' .DS . 'hikashop' . DS . 'branding' . DS . 'Zend' .DS . 'Pdf.php');
- $pdf = Zend_Pdf::load($file);
- //$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); // works without having a TTF font file put in place
- $font = Zend_Pdf_Font::fontWithPath(JPATH_BASE . DS . 'plugins' .DS . 'hikashop' . DS . 'branding' . DS . 'calibri.ttf');
- // calculate width of branding (needed for centered placement of the branding)
- $pattern_s = array(utf8_encode('ö'),utf8_encode('ü'),utf8_encode('ä'),
- utf8_encode('Ö'),utf8_encode('Ü'),utf8_encode('Ä'),
- utf8_encode('ß'));
- $replace_s = array(utf8_encode('o'),utf8_encode('u'),utf8_encode('a'),
- utf8_encode('O'),utf8_encode('U'),utf8_encode('A'),
- utf8_encode('S'));
- $string = $branding;
- $string = str_replace($pattern_s,$replace_s,$string);
- $drawingString = iconv('', 'UTF-16BE', $string);
- $characters = array();
- for ($i = 0; $i < strlen($drawingString); $i++) {
- $characters[] = (ord($drawingString[$i++]) << 8) | ord($drawingString[$i]);
- }
- $glyphs = $font->glyphNumbersForCharacters($characters);
- $widths = $font->widthsForGlyphs($glyphs);
- $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
- // place branding centered on every page
- foreach ($pdf->pages as &$page) {
- $page->setFont($font, $fontSize);
- $xTextPos = ($page->getWidth() - $stringWidth) / 2;
- $page->drawText($branding, $xTextPos, $yTextPos);
- }
- // save file with the username included in the filename
- $tempfile = substr($file, 0, strlen($file) - 4) . "_" . $username . ".pdf";
- $pdf->save($tempfile);
- $file = $tempfile;
- }
- }
- catch (Exception $e) {
- $do = false;
- $app =& JFactory::getApplication();
- $app->enqueueMessage(JText::_('Error when branding PDF: ' . $e->getMessage()));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement