Advertisement
klimmbimm

main part of a Hikashop plugin for branding sold PDFs

Jan 16th, 2012
194
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * @package     HikaShop for Joomla!
  4.  * @version     1.5.5
  5.  */
  6. defined('_JEXEC') or die('Restricted access');
  7. ?>
  8. <?php
  9. class plgHikashopBranding extends JPlugin
  10. {
  11.     function onBeforeDownloadFile(&$file,&$do) {
  12.         try {
  13.             if (substr($file, strlen($file) - 4)  === ".pdf") {
  14.                 $user =& JFactory::getUser();
  15.                 $username = $user->username;
  16.                 $realname = $user->name;
  17.                 $fontSize = 12;
  18.                 $yTextPos = 10;
  19.                 $branding = "Licensed for: " . $realname;
  20.                
  21.                 define('DS', DIRECTORY_SEPARATOR);
  22.                 define('JPATH_BASE', dirname(__FILE__) . DS . '..' . DS . '..' . DS . '..'); // assuming we need to go up 3 steps to get to the Joomla root
  23.                 set_include_path(dirname(__FILE__)); // set include_path for external library Zend Framework
  24.                 require_once(JPATH_BASE . DS . 'plugins' .DS . 'hikashop' . DS . 'branding' . DS . 'Zend' .DS . 'Pdf.php');
  25.  
  26.                 $pdf = Zend_Pdf::load($file);
  27.                 //$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); // works without having a TTF font file put in place
  28.                 $font = Zend_Pdf_Font::fontWithPath(JPATH_BASE . DS . 'plugins' .DS . 'hikashop' . DS . 'branding' . DS . 'calibri.ttf');
  29.                
  30.                 // calculate width of branding (needed for centered placement of the branding)
  31.                 $pattern_s = array(utf8_encode('ö'),utf8_encode('ü'),utf8_encode('ä'),
  32.                                 utf8_encode('Ö'),utf8_encode('Ü'),utf8_encode('Ä'),
  33.                                 utf8_encode('ß'));
  34.                 $replace_s = array(utf8_encode('o'),utf8_encode('u'),utf8_encode('a'),
  35.                                 utf8_encode('O'),utf8_encode('U'),utf8_encode('A'),
  36.                                 utf8_encode('S'));
  37.                 $string = $branding;
  38.                 $string = str_replace($pattern_s,$replace_s,$string);
  39.                 $drawingString = iconv('', 'UTF-16BE', $string);
  40.                 $characters = array();
  41.                 for ($i = 0; $i < strlen($drawingString); $i++) {
  42.                     $characters[] = (ord($drawingString[$i++]) << 8) | ord($drawingString[$i]);
  43.                 }
  44.                 $glyphs = $font->glyphNumbersForCharacters($characters);
  45.                 $widths = $font->widthsForGlyphs($glyphs);
  46.                 $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
  47.                
  48.                 // place branding centered on every page
  49.                 foreach ($pdf->pages as &$page) {
  50.                     $page->setFont($font, $fontSize);
  51.                     $xTextPos = ($page->getWidth() - $stringWidth) / 2;
  52.                     $page->drawText($branding, $xTextPos, $yTextPos);
  53.                 }
  54.                
  55.                 // save file with the username included in the filename
  56.                 $tempfile = substr($file, 0, strlen($file) - 4) . "_" . $username . ".pdf";
  57.                 $pdf->save($tempfile);
  58.                 $file = $tempfile;
  59.             }
  60.         }
  61.         catch (Exception $e) {
  62.             $do = false;
  63.             $app =& JFactory::getApplication();
  64.             $app->enqueueMessage(JText::_('Error when branding PDF: ' . $e->getMessage()));
  65.         }
  66.     }
  67. }
Advertisement
RAW Paste Data Copied
Advertisement