oxpdf.php

Go to the documentation of this file.
00001 <?php
00002 
00003 $myConfig = oxRegistry::getConfig();
00004 
00005 $sTcPdfPath = $myConfig->getConfigParam( 'sCoreDir' ) . "tcpdf/";
00006 $sTcPdfUrl  = $myConfig->getConfigParam( 'sShopURL') . "/" . $myConfig->getConfigParam( 'sCoreDir' ) . "tcpdf/";
00007 
00011 define ('K_TCPDF_EXTERNAL_CONFIG', 1 );
00012 
00017 define ('K_PATH_MAIN', $sTcPdfPath );
00018 
00023 define ('K_PATH_URL', $sTcPdfUrl );
00024 
00029 define ('K_PATH_FONTS', K_PATH_MAIN.'fonts/');
00030 
00034 define ('K_PATH_CACHE', K_PATH_MAIN.'cache/');
00035 
00039 define ('K_PATH_URL_CACHE', K_PATH_URL.'cache/');
00040 
00044 define ('K_PATH_IMAGES', K_PATH_MAIN.'images/');
00045 
00049 define ('K_BLANK_IMAGE', K_PATH_IMAGES.'_blank.png');
00050 
00054 define ('PDF_PAGE_FORMAT', 'A4');
00055 
00059 define ('PDF_PAGE_ORIENTATION', 'P');
00060 
00064 define ('PDF_CREATOR', 'TCPDF');
00065 
00069 define ('PDF_AUTHOR', 'TCPDF');
00070 
00074 define ('PDF_HEADER_TITLE', 'TCPDF Example');
00075 
00079 define ('PDF_HEADER_STRING', "by Nicola Asuni - Tecnick.com\nwww.tcpdf.org");
00080 
00084 define ('PDF_HEADER_LOGO', 'tcpdf_logo.jpg');
00085 
00089 define ('PDF_HEADER_LOGO_WIDTH', 30);
00090 
00094 define ('PDF_UNIT', 'mm');
00095 
00099 define ('PDF_MARGIN_HEADER', 5);
00100 
00104 define ('PDF_MARGIN_FOOTER', 10);
00105 
00109 define ('PDF_MARGIN_TOP', 27);
00110 
00114 define ('PDF_MARGIN_BOTTOM', 25);
00115 
00119 define ('PDF_MARGIN_LEFT', 15);
00120 
00124 define ('PDF_MARGIN_RIGHT', 15);
00125 
00129 define ('PDF_FONT_NAME_MAIN', 'helvetica');
00130 
00134 define ('PDF_FONT_SIZE_MAIN', 10);
00135 
00139 define ('PDF_FONT_NAME_DATA', 'helvetica');
00140 
00144 define ('PDF_FONT_SIZE_DATA', 8);
00145 
00149 define ('PDF_FONT_MONOSPACED', 'courier');
00150 
00154 define ('PDF_IMAGE_SCALE_RATIO', 1);
00155 
00159 define('HEAD_MAGNIFICATION', 1.1);
00160 
00164 define('K_CELL_HEIGHT_RATIO', 1.25);
00165 
00169 define('K_TITLE_MAGNIFICATION', 1.3);
00170 
00174 define('K_SMALL_RATIO', 2/3);
00175 
00179 require_once $sTcPdfPath . "config/lang/eng.php";
00180 
00184 require_once $sTcPdfPath . "tcpdf.php";
00185 
00189 class oxPDF extends TCPDF
00190 {
00206     public function __construct( $orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false )
00207     {
00208         $myConfig = oxRegistry::getConfig();
00209         $unicode  = $myConfig->isUtf();
00210         $encoding = $unicode ? 'UTF-8' : oxRegistry::getLang()->translateString( "charset" );
00211         //#1161: Thin line and unknown characters on every pdf page
00212         //we use myorder::pdfFooter()
00213         $this->setPrintFooter(false);
00214 
00215         parent::__construct( $orientation, $unit, $format, $unicode, $encoding, $diskcache );
00216     }
00217 
00235     public function WriteHTML($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='')
00236     {
00237         //HTML parser
00238         $html = str_replace( "\n", ' ', $html );
00239         $a    = preg_split('/<(.*)>/U', $html, -1, PREG_SPLIT_DELIM_CAPTURE );
00240         foreach ($a as $i=>$e) {
00241             if ($i%2==0) {
00242                 //Text
00243                 if ($this->HREF) {
00244                     $this->PutLink( $this->HREF, $e );
00245                 } else {
00246                     $this->Write( 5, $e );
00247                 }
00248             } else {
00249                 //Tag
00250                 if ($e{0}=='/') {
00251                     $this->CloseTag(strtoupper(substr($e, 1)));
00252                 } else {
00253                     //Extract attributes
00254                     $a2=explode(' ', $e);
00255                     $tag=strtoupper(array_shift($a2));
00256                     $attr=array();
00257                     foreach ($a2 as $v) {
00258                         if (preg_match('/^([^=]*)=["\']?([^"\']*)["\']?$/', $v, $a3)) {
00259                             $attr[strtoupper($a3[1])]=$a3[2];
00260                         }
00261                     }
00262                     $this->OpenTag($tag, $attr);
00263                 }
00264             }
00265         }
00266     }
00267 
00276     public function OpenTag($tag,$attr)
00277     {
00278         if ( $tag=='B' or $tag=='I' or $tag=='U' ) {
00279             $this->SetStyle($tag, true);
00280         }
00281 
00282         if ( $tag=='A' ) {
00283             $this->HREF = (is_array($attr) && isset($attr['HREF'])) ? $attr['HREF'] : '';
00284         }
00285 
00286         if ( $tag=='BR' ) {
00287             $this->Ln(5);
00288         }
00289     }
00290 
00298     public function CloseTag($tag)
00299     {
00300         if ( $tag=='B' or $tag=='I' or $tag=='U' ) {
00301             $this->SetStyle($tag, false);
00302         }
00303 
00304         if ( $tag=='A' ) {
00305             $this->HREF = '';
00306         }
00307     }
00308 
00317     public function SetStyle($tag,$enable)
00318     {
00319         $this->$tag+=($enable ? 1 : -1);
00320         $style='';
00321         foreach (array('B', 'I', 'U') as $s) {
00322             if ($this->$s>0) {
00323                 $style.=$s;
00324             }
00325         }
00326         $this->SetFont('', $style);
00327     }
00328 
00337     public function PutLink($sURL, $sText)
00338     {
00339         $this->SetTextColor( 0, 0, 255 );
00340         $this->SetStyle( 'U', true );
00341         $this->Write( 5, $sText, $sURL );
00342         $this->SetStyle( 'U', false );
00343         $this->SetTextColor( 0 );
00344     }
00345 
00358     public function SetFont($family, $style='', $size=0, $fontfile='')
00359     {
00360         if ( $family == 'Arial' ) {
00361             // overriding standard ..
00362             $family = oxRegistry::getConfig()->isUtf() ? 'freesans' : '';
00363         }
00364 
00365         parent::SetFont($family, $style, $size, $fontfile);
00366     }
00367 
00368 }