PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/core/modules/printsheet/doc/pdf_standardlabel.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 510 lines | 291 code | 61 blank | 158 comment | 89 complexity | 3ceb4495ae2969bdc6cb3fed10133ced MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2003 Steve Dillon
  3. * Copyright (C) 2003 Laurent Passebecq
  4. * Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  5. * Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
  6. * Copyright (C) 2006-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /* Inspire de PDF_Label
  22. * PDF_Label - PDF label editing
  23. * @package PDF_Label
  24. * @author Laurent PASSEBECQ <lpasseb@numericable.fr>
  25. * @copyright 2003 Laurent PASSEBECQ
  26. * disponible ici : http://www.fpdf.org/fr/script/script29.php
  27. */
  28. //-------------------------------------------------------------------
  29. // VERSIONS :
  30. // 1.0 : Initial release
  31. // 1.1 : + : Added unit in the constructor
  32. // + : Now Positions start @ (1,1).. then the first image @top-left of a page is (1,1)
  33. // + : Added in the description of a label :
  34. // font-size : defaut char size (can be changed by calling Set_Char_Size(xx);
  35. // paper-size : Size of the paper for this sheet (thanx to Al Canton)
  36. // metric : type of unit used in this description
  37. // You can define your label properties in inches by setting metric to 'in'
  38. // and printing in millimiter by setting unit to 'mm' in constructor.
  39. // Added some labels :
  40. // 5160, 5161, 5162, 5163,5164 : thanx to Al Canton : acanton@adams-blake.com
  41. // 8600 : thanx to Kunal Walia : kunal@u.washington.edu
  42. // + : Added 3mm to the position of labels to avoid errors
  43. ////////////////////////////////////////////////////
  44. /**
  45. * \file htdocs/core/modules/printsheet/doc/pdf_standardlabel.class.php
  46. * \ingroup core
  47. * \brief Fichier de la classe permettant d'editer au format PDF des etiquettes au format Avery ou personnalise
  48. */
  49. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  50. require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
  51. /**
  52. * Classe afin d'editer au format PDF des pages d'etiquette adresse au format Avery ou personnalise
  53. */
  54. class pdf_standardlabel
  55. {
  56. var $code; // Code of format
  57. var $format; // Array with informations
  58. // Proprietes privees
  59. var $_Avery_Name = ''; // Nom du format de l'etiquette
  60. var $_Margin_Left = 0; // Marge de gauche de l'etiquette
  61. var $_Margin_Top = 0; // marge en haut de la page avant la premiere etiquette
  62. var $_X_Space = 0; // Espace horizontal entre 2 bandes d'etiquettes
  63. var $_Y_Space = 0; // Espace vertical entre 2 bandes d'etiquettes
  64. var $_X_Number = 0; // NX Nombre d'etiquettes sur la largeur de la page
  65. var $_Y_Number = 0; // NY Nombre d'etiquettes sur la hauteur de la page
  66. var $_Width = 0; // Largeur de chaque etiquette
  67. var $_Height = 0; // Hauteur de chaque etiquette
  68. var $_Char_Size = 10; // Hauteur des caracteres
  69. var $_Line_Height = 10; // Hauteur par defaut d'une ligne
  70. var $_Metric = 'mm'; // Type of metric.. Will help to calculate good values
  71. var $_Metric_Doc = 'mm'; // Type of metric for the doc..
  72. var $_COUNTX = 1;
  73. var $_COUNTY = 1;
  74. var $_First = 1;
  75. /**
  76. * Constructor
  77. *
  78. * @param DoliDB $db Database handler
  79. */
  80. function __construct($db = '')
  81. {
  82. $this->db = $db;
  83. }
  84. /**
  85. * Methode qui permet de modifier la taille des caracteres
  86. * Cela modiera aussi l'espace entre chaque ligne
  87. *
  88. * @param PDF &$pdf PDF
  89. * @param int $pt point
  90. * @return void
  91. */
  92. function Set_Char_Size(&$pdf,$pt)
  93. {
  94. if ($pt > 3) {
  95. $this->_Char_Size = $pt;
  96. $this->_Line_Height = $this->_Get_Height_Chars($pt);
  97. $pdf->SetFont('','',$pt);
  98. }
  99. }
  100. /**
  101. * On imprime une etiquette
  102. *
  103. * @param PDF &$pdf PDF
  104. * @param string $textleft Textleft
  105. * @param string $header Header
  106. * @param string $footer Footer
  107. * @param Translate $outputlangs Output langs
  108. * @param string $textright Text right
  109. * @return void
  110. */
  111. function Add_PDF_card(&$pdf,$textleft,$header,$footer,$outputlangs,$textright='')
  112. {
  113. global $mysoc,$conf,$langs;
  114. // We are in a new page, then we must add a page
  115. if (($this->_COUNTX ==0) and ($this->_COUNTY==0) and (!$this->_First==1)) {
  116. $pdf->AddPage();
  117. }
  118. $this->_First=0;
  119. $_PosX = $this->_Margin_Left+($this->_COUNTX*($this->_Width+$this->_X_Space));
  120. $_PosY = $this->_Margin_Top+($this->_COUNTY*($this->_Height+$this->_Y_Space));
  121. // Define logo
  122. $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  123. if (! is_readable($logo))
  124. {
  125. $logo='';
  126. if (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small))
  127. {
  128. $logo=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
  129. }
  130. elseif (! empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
  131. {
  132. $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  133. }
  134. }
  135. // Define photo
  136. $photo='';
  137. // Define background image
  138. $backgroundimage='';
  139. // Print lines
  140. if ($this->code == "CARD")
  141. {
  142. $this->Tformat=$this->_Avery_Labels["CARD"];
  143. //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
  144. $this->_Croix($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.1,10);
  145. }
  146. // Background
  147. if ($backgroundimage)
  148. {
  149. $pdf->image($backgroundimage,$_PosX,$_PosY,$this->_Width,$this->_Height);
  150. }
  151. // Top
  152. if ($header!='')
  153. {
  154. if ($this->code == "CARD")
  155. {
  156. $pdf->SetDrawColor(128,128,128);
  157. $pdf->Line($_PosX, $_PosY+$this->_Line_Height+1, $_PosX+$this->_Width, $_PosY+$this->_Line_Height+1);
  158. $pdf->SetDrawColor(0,0,0);
  159. }
  160. $pdf->SetXY($_PosX, $_PosY+1);
  161. $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($header),0,1,'C');
  162. }
  163. // Middle
  164. if ($textright=='') // Only a left part
  165. {
  166. if ($textleft == '%LOGO%' && $logo) $this->Image($logo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  167. else if ($textleft == '%PHOTO%' && $photo) $this->Image($photo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  168. else
  169. {
  170. $pdf->SetXY($_PosX+3, $_PosY+3+$this->_Line_Height);
  171. $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
  172. }
  173. }
  174. else if ($textleft!='' && $textright!='') //
  175. {
  176. if ($textleft == '%LOGO%' || $textleft == '%PHOTO%')
  177. {
  178. if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  179. else if ($textleft == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  180. $pdf->SetXY($_PosX+21, $_PosY+3+$this->_Line_Height);
  181. $pdf->MultiCell($this->_Width-22, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
  182. }
  183. else if ($textright == '%LOGO%' || $textright == '%PHOTO%')
  184. {
  185. if ($textright == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$this->_Width-21,$_PosY+3+$this->_Line_Height,20);
  186. else if ($textright == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$this->_Width-21,$_PosY+3+$this->_Line_Height,20);
  187. $pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
  188. $pdf->MultiCell($this->_Width-22, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
  189. }
  190. else
  191. {
  192. $pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
  193. $pdf->MultiCell(round($this->_Width/2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
  194. $pdf->SetXY($_PosX+round($this->_Width/2), $_PosY+3+$this->_Line_Height);
  195. $pdf->MultiCell(round($this->_Width/2)-2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
  196. }
  197. }
  198. else // Only a right part
  199. {
  200. if ($textright == '%LOGO%' && $logo) $this->Image($logo,$_PosX+$this->_Width-21,$_PosY+1,20);
  201. else if ($textright == '%PHOTO%' && $photo) $this->Image($photo,$_PosX+$this->_Width-21,$_PosY+1,20);
  202. else
  203. {
  204. $pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
  205. $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
  206. }
  207. }
  208. // Bottom
  209. if ($footer!='')
  210. {
  211. if ($this->code == "CARD")
  212. {
  213. $pdf->SetDrawColor(128,128,128);
  214. $pdf->Line($_PosX, $_PosY+$this->_Height-$this->_Line_Height-2, $_PosX+$this->_Width, $_PosY+$this->_Height-$this->_Line_Height-2);
  215. $pdf->SetDrawColor(0,0,0);
  216. }
  217. $pdf->SetXY($_PosX, $_PosY+$this->_Height-$this->_Line_Height-1);
  218. $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer),0,1,'C');
  219. }
  220. //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
  221. $this->_COUNTY++;
  222. if ($this->_COUNTY == $this->_Y_Number) {
  223. // Si on est en bas de page, on remonte le 'curseur' de position
  224. $this->_COUNTX++;
  225. $this->_COUNTY=0;
  226. }
  227. if ($this->_COUNTX == $this->_X_Number) {
  228. // Si on est en bout de page, alors on repart sur une nouvelle page
  229. $this->_COUNTX=0;
  230. $this->_COUNTY=0;
  231. }
  232. }
  233. /**
  234. * Print dot line
  235. *
  236. * @param PDF &$pdf PDF
  237. * @param int $x1 X1
  238. * @param int $y1 Y1
  239. * @param int $x2 X2
  240. * @param int $y2 Y2
  241. * @param int $epaisseur Epaisseur
  242. * @param int $nbPointilles Nb pointilles
  243. * @return void
  244. */
  245. function _Pointille(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$nbPointilles=15)
  246. {
  247. $pdf->SetLineWidth($epaisseur);
  248. $length=abs($x1-$x2);
  249. $hauteur=abs($y1-$y2);
  250. if($length>$hauteur) {
  251. $Pointilles=($length/$nbPointilles)/2; // taille des pointilles
  252. }
  253. else {
  254. $Pointilles=($hauteur/$nbPointilles)/2;
  255. }
  256. for($i=$x1;$i<=$x2;$i+=$Pointilles+$Pointilles) {
  257. for($j=$i;$j<=($i+$Pointilles);$j++) {
  258. if($j<=($x2-1)) {
  259. $pdf->Line($j,$y1,$j+1,$y1); // on trace le pointill? du haut, point par point
  260. $pdf->Line($j,$y2,$j+1,$y2); // on trace le pointill? du bas, point par point
  261. }
  262. }
  263. }
  264. for($i=$y1;$i<=$y2;$i+=$Pointilles+$Pointilles) {
  265. for($j=$i;$j<=($i+$Pointilles);$j++) {
  266. if($j<=($y2-1)) {
  267. $pdf->Line($x1,$j,$x1,$j+1); // on trace le pointill? du haut, point par point
  268. $pdf->Line($x2,$j,$x2,$j+1); // on trace le pointill? du bas, point par point
  269. }
  270. }
  271. }
  272. }
  273. /**
  274. * Fonction realisant une croix aux 4 coins des cartes
  275. *
  276. * @param PDF &$pdf PDF
  277. * @param int $x1 X1
  278. * @param int $y1 Y1
  279. * @param int $x2 X2
  280. * @param int $y2 Y2
  281. * @param int $epaisseur Epaisseur
  282. * @param int $taille Size
  283. * @return void
  284. */
  285. function _Croix(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$taille=4)
  286. {
  287. $pdf->SetDrawColor(192,192,192);
  288. $pdf->SetLineWidth($epaisseur);
  289. $lg=$taille/2;
  290. // croix haut gauche
  291. $pdf->Line($x1,$y1-$lg,$x1,$y1+$lg);
  292. $pdf->Line($x1-$lg,$y1,$x1+$lg,$y1);
  293. // croix bas gauche
  294. $pdf->Line($x1,$y2-$lg,$x1,$y2+$lg);
  295. $pdf->Line($x1-$lg,$y2,$x1+$lg,$y2);
  296. // croix haut droit
  297. $pdf->Line($x2,$y1-$lg,$x2,$y1+$lg);
  298. $pdf->Line($x2-$lg,$y1,$x2+$lg,$y1);
  299. // croix bas droit
  300. $pdf->Line($x2,$y2-$lg,$x2,$y2+$lg);
  301. $pdf->Line($x2-$lg,$y2,$x2+$lg,$y2);
  302. $pdf->SetDrawColor(0,0,0);
  303. }
  304. /**
  305. * Convert units (in to mm, mm to in)
  306. * $src and $dest must be 'in' or 'mm'
  307. *
  308. * @param int $value value
  309. * @param string $src from
  310. * @param string $dest to
  311. * @return float value value after conversion
  312. */
  313. function _Convert_Metric ($value, $src, $dest) {
  314. if ($src != $dest) {
  315. $tab['in'] = 39.37008;
  316. $tab['mm'] = 1000;
  317. return $value * $tab[$dest] / $tab[$src];
  318. } else {
  319. return $value;
  320. }
  321. }
  322. /**
  323. * Give the height for a char size given.
  324. *
  325. * @param int $pt Point
  326. * @return int Height chars
  327. */
  328. function _Get_Height_Chars($pt) {
  329. // Tableau de concordance entre la hauteur des caracteres et de l'espacement entre les lignes
  330. $_Table_Hauteur_Chars = array(6=>2, 7=>2.5, 8=>3, 9=>3.5, 10=>4, 11=>6, 12=>7, 13=>8, 14=>9, 15=>10);
  331. if (in_array($pt, array_keys($_Table_Hauteur_Chars))) {
  332. return $_Table_Hauteur_Chars[$pt];
  333. } else {
  334. return 100; // There is a prob..
  335. }
  336. }
  337. /**
  338. * Set format
  339. *
  340. * @param PDF &$pdf PDF
  341. * @param string $format Format
  342. * @return void
  343. */
  344. function _Set_Format(&$pdf, $format)
  345. {
  346. $this->_Metric = $format['metric'];
  347. $this->_Avery_Name = $format['name'];
  348. $this->_Avery_Code = $format['code'];
  349. $this->_Margin_Left = $this->_Convert_Metric($format['marginLeft'], $this->_Metric, $this->_Metric_Doc);
  350. $this->_Margin_Top = $this->_Convert_Metric($format['marginTop'], $this->_Metric, $this->_Metric_Doc);
  351. $this->_X_Space = $this->_Convert_Metric($format['SpaceX'], $this->_Metric, $this->_Metric_Doc);
  352. $this->_Y_Space = $this->_Convert_Metric($format['SpaceY'], $this->_Metric, $this->_Metric_Doc);
  353. $this->_X_Number = $format['NX'];
  354. $this->_Y_Number = $format['NY'];
  355. $this->_Width = $this->_Convert_Metric($format['width'], $this->_Metric, $this->_Metric_Doc);
  356. $this->_Height = $this->_Convert_Metric($format['height'], $this->_Metric, $this->_Metric_Doc);
  357. $this->Set_Char_Size($pdf, $format['font-size']);
  358. }
  359. /**
  360. * Function to build PDF on disk, then output on HTTP strem.
  361. *
  362. * @param array $arrayofmembers Array of members informations
  363. * @param Translate $outputlangs Lang object for output language
  364. * @param string $srctemplatepath Full path of source filename for generator using a template file
  365. * @param string $outputdir Output directory
  366. * @return int 1=OK, 0=KO
  367. */
  368. function write_file($arrayofmembers,$outputlangs,$srctemplatepath,$outputdir='')
  369. {
  370. global $user,$conf,$langs,$mysoc,$_Avery_Labels;
  371. $this->code=$srctemplatepath;
  372. $this->Tformat = $_Avery_Labels[$this->code];
  373. if (empty($this->Tformat)) { dol_print_error('','ErrorBadTypeForCard'.$this->code); exit; }
  374. $this->type = 'pdf';
  375. $this->format = $this->Tformat['paper-size'];
  376. if (! is_object($outputlangs)) $outputlangs=$langs;
  377. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  378. if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
  379. $outputlangs->load("main");
  380. $outputlangs->load("dict");
  381. $outputlangs->load("companies");
  382. $outputlangs->load("members");
  383. $outputlangs->load("admin");
  384. $dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir);
  385. $filename='tmp_address_sheet.pdf';
  386. $file = $dir."/".$filename;
  387. if (! file_exists($dir))
  388. {
  389. if (dol_mkdir($dir) < 0)
  390. {
  391. $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
  392. return 0;
  393. }
  394. }
  395. $pdf=pdf_getInstance($this->format,$this->Tformat['metric']);
  396. if (class_exists('TCPDF'))
  397. {
  398. $pdf->setPrintHeader(false);
  399. $pdf->setPrintFooter(false);
  400. }
  401. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  402. $pdf->SetTitle($outputlangs->transnoentities('MembersLabels'));
  403. $pdf->SetSubject($outputlangs->transnoentities("MembersLabels"));
  404. $pdf->SetCreator("Speedealing ".DOL_VERSION);
  405. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  406. $pdf->SetKeyWords($outputlangs->transnoentities('MembersLabels')." ".$outputlangs->transnoentities("Foundation")." ".$outputlangs->convToOutputCharset($mysoc->name));
  407. if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
  408. $pdf->SetMargins(0,0);
  409. $pdf->SetAutoPageBreak(false);
  410. $this->_Metric_Doc = $this->Tformat['metric'];
  411. // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
  412. $posX=1;
  413. $posY=1;
  414. if ($posX > 0) $posX--; else $posX=0;
  415. if ($posY > 0) $posY--; else $posY=0;
  416. $this->_COUNTX = $posX;
  417. $this->_COUNTY = $posY;
  418. $this->_Set_Format($pdf, $this->Tformat);
  419. $pdf->Open();
  420. $pdf->AddPage();
  421. // Add each record
  422. foreach($arrayofmembers as $val)
  423. {
  424. // imprime le texte specifique sur la carte
  425. $this->Add_PDF_card($pdf,$val['textleft'],$val['textheader'],$val['textfooter'],$langs,$val['textright'],$val['id'],$val['photo']);
  426. }
  427. //$pdf->SetXY(10, 295);
  428. //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
  429. // Output to file
  430. $pdf->Output($file,'F');
  431. if (! empty($conf->global->MAIN_UMASK))
  432. @chmod($file, octdec($conf->global->MAIN_UMASK));
  433. // Output to http stream
  434. clearstatcache();
  435. $attachment=true;
  436. if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
  437. $type=dol_mimetype($filename);
  438. //if ($encoding) header('Content-Encoding: '.$encoding);
  439. if ($type) header('Content-Type: '.$type);
  440. if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
  441. else header('Content-Disposition: inline; filename="'.$filename.'"');
  442. // Ajout directives pour resoudre bug IE
  443. header('Cache-Control: Public, must-revalidate');
  444. header('Pragma: public');
  445. readfile($file);
  446. return 1;
  447. }
  448. }
  449. ?>