linea21-core
[ class tree: linea21-core ] [ index: linea21-core ] [ all elements ]

Source for file idd_pdf.php

Documentation is available at idd_pdf.php

  1. <?php
  2.  
  3. /**
  4.  * @package linea21.core
  5.  * @subpackage system
  6.  * @see tcpdf
  7.  * @author linea21 <info@linea21.com>
  8.  * @version $id SVN
  9.  * @access public
  10.  * @license http://opensource.org/licenses/gpl-3.0.html
  11.  */
  12.  
  13. require('tcpdf.php');
  14.  
  15. class idd_pdf extends TCPDF {
  16.  
  17.   // //////////////////////////////////
  18.   // formattage titres item & info
  19.   // //////////////////////////////////
  20.   function FormatSdiItem($libelle)
  21.   {
  22.     $libelle="        ".$libelle;
  23.     $this->SetFont(DOC_POLICE, SDI_ITEM_STYLE, SDI_ITEM_SIZE);
  24.     $this->SetColor('SDI_ITEM_COLOR');
  25.     $this->MultiCell(0,DOC_HEIGHT,$libelle,0,'L',0);
  26.     $this->SetColor('DOC_COLOR');
  27.     $this->SetFont(DOC_POLICE,DOC_STYLE,DOC_SIZE);
  28.   }
  29.  
  30.   function FormatSdiInfo($libelle)
  31.   {
  32.     $this->SetColor('BCKG_INFO', 'BCKG');
  33.     $this->SetFont(DOC_POLICE, SDI_INFO_STYLE, SDI_INFO_SIZE);
  34.     $this->SetColor('SDI_INFO_COLOR');
  35.     $this->MultiCell(0,DOC_HEIGHT+1,$libelle,0,'L',1);
  36.     $this->SetColor('DOC_COLOR');
  37.     $this->SetFont(DOC_POLICE,DOC_STYLE,DOC_SIZE);
  38.     $this->Ln(DOC_INTERVAL);
  39.   }
  40.  
  41.   // Saut de paragraphe
  42.   function ParagraphBreak()
  43.   {
  44.     $this->SetColor('DOC_COLOR');
  45.     $this->SetFont(DOC_POLICE,DOC_STYLE,DOC_SIZE);
  46.     $this->Ln(PARAGRAPH_BREAK);
  47.   }
  48.  
  49.  
  50.   // //////////////////////////////////
  51.   // gestion header & footer
  52.   // //////////////////////////////////
  53.   function Footer()
  54.   {
  55.     global $report_settings;
  56.     global $lang;
  57.  
  58.     $content = $report_settings['author'];
  59.     if(!empty($report_settings['mail_author']))  {
  60.       $content .= ' ('.$report_settings['mail_author'].')';
  61.       $mailto='mailto:'.$report_settings['mail_author'];
  62.     } else $mailto='';
  63.     $content_comp= !empty($report_settings['service_author']) ? ' -- '.$report_settings['service_author'] : '';
  64.     $content_comp.= !empty($report_settings['adress_author']) ? ' -- '.$report_settings['adress_author'] : '';
  65.  
  66.     $this->SetColor('DOC_COLOR_HEADER');
  67.     $this->SetFont(DOC_POLICE, EM_STYLE, MIN_SIZE);
  68.     $this->SetY(-10);
  69.     $this->Line($this->GetX(), $this->GetY(), $this->GetX()+190, $this->GetY());
  70.     $this->Cell($this->GetStringWidth($content), 4, $content, 0, 0, 'L', 0, $mailto);
  71.     $this->Cell(0, 4, $content_comp, 0, 0, 'L');
  72.  
  73.     // Numéro et nombre de pages
  74.     $this->Cell(0, 4, $lang['divers']['page'].' ' . $this->PageNo() . '/{nb}', 0, 0, 'R');
  75.   }
  76.  
  77.   function Header()
  78.   {
  79.     global $report_settings;
  80.     global $lang;
  81.  
  82.     $today = date("d-m-Y");
  83.     $content = formatText($report_settings['title']) .' - '. SITE_NAME;
  84.  
  85.     $this->SetColor('DOC_COLOR_HEADER');
  86.     $this->SetFont(DOC_POLICE, EM_STYLE, MIN_SIZE);
  87.  
  88.     //$this->Cell(0, 4, $lang['statut']['published_on'].' '. $today, 0, 0, 'L');
  89.     $this->Cell(0, 4, $today, 0, 0, 'L');
  90.     $this->Cell(0, 4, $content, 0, 1, 'R');
  91.     $this->Line($this->GetX(), $this->GetY(), $this->GetX()+190, $this->GetY());
  92.     //$this->Image('inc_report/logo_p.png', $this->GetX(), $this->GetY());
  93.     $this->Ln(PARAGRAPH_BREAK);
  94.   }
  95.   // //////////////////////////////////
  96.   // gestion dynamique des couleurs
  97.   // //////////////////////////////////
  98.   /**
  99.   * idd_pdf::SetColor()
  100.   *
  101.   * @param string $ink 
  102.   * @param string $type 'TRACE' | 'FONT' | 'BCKG'
  103.   * @return 
  104.   */
  105.   function SetColor($ink = 'DOC_COLOR', $type = 'FONT')
  106.   {
  107.     switch ($ink) {
  108.       case 'DOC_COLOR':
  109.         $r = 0;
  110.         $g = 0;
  111.         $b = 0;
  112.         break;
  113.       case 'DOC_COLOR_HEADER':
  114.         $r = 34;
  115.         $g = 122;
  116.         $b = 155;
  117.         break;
  118.       case 'DOC_COLOR_BORDER';
  119.       $r = 34;
  120.       $g = 122;
  121.       $b = 155;
  122.       break;
  123.       case 'DOC_TITLE_COLOR';
  124.       $r = 34;
  125.       $g = 122;
  126.       $b = 155;
  127.       break;
  128.       case 'DOC_ADVISE_COLOR':
  129.         $r = 113;
  130.         $g = 114;
  131.         $b = 115;
  132.         break;
  133.       case 'SDI_TITLE_COLOR':
  134.         $r = 82;
  135.         $g = 117;
  136.         $b = 181;
  137.         break;
  138.       case 'SDI_ITEM_COLOR':
  139.         $r = 236;
  140.         $g = 158;
  141.         $b = 22;
  142.         break;
  143.       case 'SDI_INFO_COLOR':
  144.         $r = 255;
  145.         $g = 255;
  146.         $b = 255;
  147.         break;
  148.       case 'BCKG_PROVIDER':
  149.         $r = 226;
  150.         $g = 234;
  151.         $b = 249;
  152.         break;
  153.       case 'BCKG_ITEM':
  154.         $r = 226;
  155.         $g = 234;
  156.         $b = 249;
  157.         break;
  158.       case 'BCKG_INFO':
  159.         $r = 119;
  160.         $g = 152;
  161.         $b = 183;
  162.         break;
  163.     }
  164.     switch ($type) {
  165.       case 'FONT':
  166.         $this->SetTextColor($r, $g, $b);
  167.         break;
  168.       case 'TRACE':
  169.         $this->SetDrawColor($r, $g, $b);
  170.         break;
  171.       case 'BCKG':
  172.         $this->SetFillColor($r, $g, $b);
  173.         break;
  174.     }
  175.   }
  176.  
  177.  
  178.   // //////////////////////////////////
  179.   // gestion signets
  180.   // //////////////////////////////////
  181.   var $outlines = array();
  182.   var $OutlineRoot;
  183.  
  184.   function Bookmark($txt, $level = 0, $y = 0)
  185.   {
  186.     if ($y == -1)
  187.     $y = $this->GetY();
  188.     $this->outlines[] = array('t' => $txt, 'l' => $level, 'y' => $y, 'p' => $this->PageNo());
  189.   }
  190.  
  191.   function _putbookmarks()
  192.   {
  193.     $nb = count($this->outlines);
  194.     if ($nb == 0)
  195.     return;
  196.     $lru = array();
  197.     $level = 0;
  198.     foreach($this->outlines as $i => $o) {
  199.       if ($o['l'] > 0) {
  200.         $parent = $lru[$o['l']-1];
  201.         // Set parent and last pointers
  202.         $this->outlines[$i]['parent'] = $parent;
  203.         $this->outlines[$parent]['last'] = $i;
  204.         if ($o['l'] > $level) {
  205.           // Level increasing: set first pointer
  206.           $this->outlines[$parent]['first'] = $i;
  207.         }
  208.       } else
  209.       $this->outlines[$i]['parent'] = $nb;
  210.       if ($o['l'] <= $level and $i > 0) {
  211.         // Set prev and next pointers
  212.         $prev = $lru[$o['l']];
  213.         $this->outlines[$prev]['next'] = $i;
  214.         $this->outlines[$i]['prev'] = $prev;
  215.       }
  216.       $lru[$o['l']] = $i;
  217.       $level = $o['l'];
  218.     }
  219.     // Outline items
  220.     $n = $this->n + 1;
  221.     foreach($this->outlines as $i => $o) {
  222.       $this->_newobj();
  223.       $this->_out('<</Title ' . $this->_textstring($o['t']));
  224.       //$this->_out('<</Title ' . $this->_escapetext($o['t']));
  225.       $this->_out('/Parent ' . ($n + $o['parent']) . ' 0 R');
  226.       if (isset($o['prev']))
  227.       $this->_out('/Prev ' . ($n + $o['prev']) . ' 0 R');
  228.       if (isset($o['next']))
  229.       $this->_out('/Next ' . ($n + $o['next']) . ' 0 R');
  230.       if (isset($o['first']))
  231.       $this->_out('/First ' . ($n + $o['first']) . ' 0 R');
  232.       if (isset($o['last']))
  233.       $this->_out('/Last ' . ($n + $o['last']) . ' 0 R');
  234.       $this->_out(sprintf('/Dest [%d 0 R /XYZ 0 %.2f null]', 1 + 2 * $o['p'], ($this->h - $o['y']) * $this->k));
  235.       $this->_out('/Count 0>>');
  236.       $this->_out('endobj');
  237.     }
  238.     // Outline root
  239.     $this->_newobj();
  240.     $this->OutlineRoot = $this->n;
  241.     $this->_out('<</Type /Outlines /First ' . $n . ' 0 R');
  242.     $this->_out('/Last ' . ($n + $lru[0]) . ' 0 R>>');
  243.     $this->_out('endobj');
  244.   }
  245.  
  246.   function _putresources()
  247.   {
  248.     parent::_putresources();
  249.     $this->_putbookmarks();
  250.   }
  251.  
  252.   function _putcatalog()
  253.   {
  254.     parent::_putcatalog();
  255.     if (count($this->outlines) > 0) {
  256.       $this->_out('/Outlines ' . $this->OutlineRoot . ' 0 R');
  257.       $this->_out('/PageMode /UseOutlines');
  258.     }
  259.   }
  260.  
  261.  
  262.  
  263.   // //////////////////////////////////
  264.   // creation de l'index
  265.   // //////////////////////////////////
  266.   function CreateIndex($depth=100){
  267.  
  268.     global $lang;
  269.     global $link_array;
  270.  
  271.     //Titre
  272.     $this->SetFont(DOC_POLICE, DOC_TITLE_STYLE, DOC_TITLE_SIZE);
  273.     $this->SetColor('DOC_TITLE_COLOR');
  274.     $this->Cell(0, DOC_HEIGHT, $lang['dashboard']['rap_summary'], 0, 2, 'C');
  275.     $this->SetFont(DOC_POLICE, EM_STYLE, MIN_SIZE);
  276.     $this->Ln(10);
  277.  
  278.  
  279.  
  280.  
  281.     $size=sizeof($this->outlines)-1; // -1 pour supprimer le bookmark de l'index
  282.     $PageCellSize=$this->GetStringWidth('p. '.$this->outlines[$size-1]['p'])+2;
  283.  
  284.     $cnt_level1=0;
  285.     for ($i=0;$i<$size;$i++){
  286.  
  287.       //Décalage
  288.       $level=$this->outlines[$i]['l'];
  289.       if($level<$depth) {
  290.         if($level>0)
  291.         $this->Cell($level*8);
  292.             
  293.         // Titre de l'indicateur
  294.         if($level==0) {
  295.           $cnt_level1++;
  296.           $link=$cnt_level1;
  297.                    $this->SetColor('SDI_ITEM_COLOR');
  298.         } else $link='';
  299.  
  300.             
  301.             
  302.         //Libellé
  303.         $str=$this->outlines[$i]['t'];
  304.         $strsize=$this->GetStringWidth($str);
  305.         $avail_size=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-4;
  306.         while ($strsize>=$avail_size){
  307.           $str=substr($str,0,-1);
  308.           $strsize=$this->GetStringWidth($str);
  309.         }
  310.         $this->Cell($strsize+2,$this->FontSize+2,$str, 0, 0, 'L', 0, $link);
  311.         $this->SetColor('DOC_TITLE_COLOR');
  312.  
  313.         //Points
  314.         $w=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-($strsize+2);
  315.         $nb=$w/$this->GetStringWidth('.');
  316.         $dots=str_repeat('.',$nb);
  317.         $this->Cell($w,$this->FontSize+2,$dots,0,0,'R');
  318.  
  319.         //Numéro de page
  320.         $this->Cell($PageCellSize,$this->FontSize+2,'p. '.$this->outlines[$i]['p'],0,1,'R');
  321.       }
  322.     }
  323.     $this->SetFont(DOC_POLICE, DOC_TITLE_STYLE, DOC_TITLE_SIZE);
  324.   }
  325.  
  326.   // //////////////////////////////////
  327.   // Tableau avec multicells
  328.   // //////////////////////////////////
  329.   var $widths;
  330.   var $aligns;
  331.  
  332.   function SetWidths($w)
  333.   {
  334.     // Tableau des largeurs de colonnes
  335.     $this->widths = $w;
  336.   }
  337.  
  338.   function SetAligns($a)
  339.   {
  340.     // Tableau des alignements de colonnes
  341.     $this->aligns = $a;
  342.   }
  343.  
  344.   function Row($data)
  345.   {
  346.     // Calcule la hauteur de la ligne
  347.     $nb = 0;
  348.     for($i = 0;$i < count($data);$i++)
  349.     $nb = max($nb, $this->NbLines($this->widths[$i], $data[$i]));
  350.     $h = 5 * $nb;
  351.     // Effectue un saut de page si nécessaire
  352.     $this->CheckPageBreak($h);
  353.     // Dessine les cellules
  354.     for($i = 0;$i < count($data);$i++) {
  355.       $w = $this->widths[$i];
  356.       $a = isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
  357.       // Sauve la position courante
  358.       $x = $this->GetX();
  359.       $y = $this->GetY();
  360.       // Dessine le cadre
  361.       $this->Rect($x, $y, $w, $h);
  362.       // Imprime le texte
  363.       $this->MultiCell($w, 5, $data[$i], 0, $a);
  364.       // Repositionne à droite
  365.       $this->SetXY($x + $w, $y);
  366.     }
  367.     // Va à la ligne
  368.     $this->Ln($h);
  369.   }
  370.  
  371.   function CheckPageBreak($h)
  372.   {
  373.     // Si la hauteur h provoque un d�bordement, saut de page manuel
  374.     if ($this->GetY() + $h > $this->PageBreakTrigger)
  375.     $this->AddPage($this->CurOrientation);
  376.   }
  377.  
  378.   function NbLines($w, $txt)
  379.   {
  380.     // Calcule le nombre de lignes qu'occupe un MultiCell de largeur w
  381.     $cw = &$this->CurrentFont['cw'];
  382.     if ($w == 0)
  383.     $w = $this->w - $this->rMargin - $this->x;
  384.     $wmax = ($w-2 * $this->cMargin) * 1000 / $this->FontSize;
  385.     $s = str_replace("\r", '', $txt);
  386.     $nb = strlen($s);
  387.     if ($nb > 0 and $s[$nb-1] == "\n")
  388.     $nb--;
  389.     $sep = -1;
  390.     $i = 0;
  391.     $j = 0;
  392.     $l = 0;
  393.     $nl = 1;
  394.     while ($i < $nb) {
  395.       $c = $s[$i];
  396.       if ($c == "\n") {
  397.         $i++;
  398.         $sep = -1;
  399.         $j = $i;
  400.         $l = 0;
  401.         $nl++;
  402.         continue;
  403.       }
  404.       if ($c == ' ')
  405.       $sep = $i;
  406.       //$l += $cw[$c];
  407.       $l += $cw[ord($c)];
  408.       if ($l > $wmax) {
  409.         if ($sep == -1) {
  410.           if ($i == $j)
  411.           $i++;
  412.         } else
  413.         $i = $sep + 1;
  414.         $sep = -1;
  415.         $j = $i;
  416.         $l = 0;
  417.         $nl++;
  418.       } else
  419.       $i++;
  420.     }
  421.     return $nl;
  422.   }
  423.  
  424.  
  425.   // //////////////////////////////////
  426.   // import d'image en memoire
  427.   // //////////////////////////////////
  428.   // (c) Xavier Nicolay
  429.   // V1.0 : 2004-01-17
  430.   // CONSTRUCTOR
  431.   function MEM_IMAGE($orientation = 'P', $unit = 'mm', $format = 'A4')
  432.   {
  433.     $this->FPDF($orientation, $unit, $format);
  434.     // Register var stream protocol (requires PHP>=4.3.2)
  435.     if (function_exists('stream_wrapper_register'))
  436.     stream_wrapper_register('var', 'VariableStream');
  437.   }
  438.   // PRIVATE FUNCTIONS
  439.   function _readstr($var, &$pos, $n)
  440.   {
  441.     // Read some bytes from string
  442.     $string = substr($var, $pos, $n);
  443.     $pos += $n;
  444.     return $string;
  445.   }
  446.  
  447.   function _readstr_int($var, &$pos)
  448.   {
  449.     // Read a 4-byte integer from string
  450.     $i = ord($this->_readstr($var, $pos, 1)) << 24;
  451.     $i += ord($this->_readstr($var, $pos, 1)) << 16;
  452.     $i += ord($this->_readstr($var, $pos, 1)) << 8;
  453.     $i += ord($this->_readstr($var, $pos, 1));
  454.     return $i;
  455.   }
  456.  
  457.   function _parsemempng($var)
  458.   {
  459.     $pos = 0;
  460.     // Check signature
  461.     $sig = $this->_readstr($var, $pos, 8);
  462.     if ($sig != chr(137) . 'PNG' . chr(13) . chr(10) . chr(26) . chr(10))
  463.     $this->Error('Not a PNG image');
  464.     // Read header chunk
  465.     $this->_readstr($var, $pos, 4);
  466.     $ihdr = $this->_readstr($var, $pos, 4);
  467.     if ($ihdr != 'IHDR')
  468.     $this->Error('Incorrect PNG Image');
  469.     $w = $this->_readstr_int($var, $pos);
  470.     $h = $this->_readstr_int($var, $pos);
  471.     $bpc = ord($this->_readstr($var, $pos, 1));
  472.     if ($bpc > 8)
  473.     $this->Error('16-bit depth not supported: ' . $file);
  474.     $ct = ord($this->_readstr($var, $pos, 1));
  475.     if ($ct == 0)
  476.     $colspace = 'DeviceGray';
  477.     elseif ($ct == 2)
  478.     $colspace = 'DeviceRGB';
  479.     elseif ($ct == 3)
  480.     $colspace = 'Indexed';
  481.     else
  482.     $this->Error('Alpha channel not supported: ' . $file);
  483.     if (ord($this->_readstr($var, $pos, 1)) != 0)
  484.     $this->Error('Unknown compression method: ' . $file);
  485.     if (ord($this->_readstr($var, $pos, 1)) != 0)
  486.     $this->Error('Unknown filter method: ' . $file);
  487.     if (ord($this->_readstr($var, $pos, 1)) != 0)
  488.     $this->Error('Interlacing not supported: ' . $file);
  489.     $this->_readstr($var, $pos, 4);
  490.     $parms = '/DecodeParms <</Predictor 15 /Colors ' . ($ct == 2 ? 3 : 1) . ' /BitsPerComponent ' . $bpc . ' /Columns ' . $w . '>>';
  491.     // Scan chunks looking for palette, transparency and image data
  492.     $pal = '';
  493.     $trns = '';
  494.     $data = '';
  495.     do {
  496.       $n = $this->_readstr_int($var, $pos);
  497.       $type = $this->_readstr($var, $pos, 4);
  498.       if ($type == 'PLTE') {
  499.         // Read palette
  500.         $pal = $this->_readstr($var, $pos, $n);
  501.         $this->_readstr($var, $pos, 4);
  502.       } elseif ($type == 'tRNS') {
  503.         // Read transparency info
  504.         $t = $this->_readstr($var, $pos, $n);
  505.         if ($ct == 0)
  506.         $trns = array(ord(substr($t, 1, 1)));
  507.         elseif ($ct == 2)
  508.         $trns = array(ord(substr($t, 1, 1)), ord(substr($t, 3, 1)), ord(substr($t, 5, 1)));
  509.         else {
  510.           $pos = strpos($t, chr(0));
  511.           if (is_int($pos))
  512.           $trns = array($pos);
  513.         }
  514.         $this->_readstr($var, $pos, 4);
  515.       } elseif ($type == 'IDAT') {
  516.         // Read image data block
  517.         $data .= $this->_readstr($var, $pos, $n);
  518.         $this->_readstr($var, $pos, 4);
  519.       } elseif ($type == 'IEND')
  520.       break;
  521.       else
  522.       $this->_readstr($var, $pos, $n + 4);
  523.     } while ($n);
  524.     if ($colspace == 'Indexed' and empty($pal))
  525.     $this->Error('Missing palette in ' . $file);
  526.     return array('w' => $w,
  527.             'h' => $h,
  528.             'cs' => $colspace,
  529.             'bpc' => $bpc,
  530.             'f' => 'FlateDecode',
  531.             'parms' => $parms,
  532.             'pal' => $pal,
  533.             'trns' => $trns,
  534.             'data' => $data);
  535.   }
  536.  
  537.   /**
  538.    */
  539.   /* PUBLIC FUNCTIONS */
  540.   /**
  541.    */
  542.   function MemImage($data, $x, $y, $w = 0, $h = 0, $link = '')
  543.   {
  544.     // Put the PNG image stored in $data
  545.     $id = md5($data);
  546.     if (!isset($this->images[$id])) {
  547.       $info = $this->_parsemempng($data);
  548.       $info['i'] = count($this->images) + 1;
  549.       $this->images[$id] = $info;
  550.     } else
  551.     $info = $this->images[$id];
  552.     // Automatic width and height calculation if needed
  553.     if ($w == 0 and $h == 0) {
  554.       // Put image at 72 dpi
  555.       $w = $info['w'] / $this->k;
  556.       $h = $info['h'] / $this->k;
  557.     }
  558.     if ($w == 0)
  559.     $w = $h * $info['w'] / $info['h'];
  560.     if ($h == 0)
  561.     $h = $w * $info['h'] / $info['w'];
  562.  
  563.     $this->_out(sprintf('q %.2f 0 0 %.2f %.2f %.2f cm /I%d Do Q', $w * $this->k, $h * $this->k, $x * $this->k, ($this->h - ($y + $h)) * $this->k, $info['i']));
  564.     if ($link)
  565.     $this->Link($x, $y, $w, $h, $link);
  566.   }
  567.  
  568. }
  569.  
  570. ?>

Documentation generated on Fri, 01 Apr 2011 09:31:30 +0200 by phpDocumentor 1.4.1