pdf_tcpdflabel.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  7. * Copyright (C) 2015 Francis Appels <francis.appels@yahoo.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/core/modules/printsheet/doc/pdf_standardlabel.class.php
  24. * \ingroup core
  25. * \brief Fichier de la classe permettant d'editer au format PDF des etiquettes au format Avery ou personnalise
  26. */
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/commonstickergenerator.class.php';
  28. /**
  29. * Class to generate stick sheet with format Avery or other personalised
  30. */
  31. class pdf_tcpdflabel extends CommonStickerGenerator
  32. {
  33. // define 1d barcode style
  34. private $_style1d = array(
  35. 'position' => '',
  36. 'align' => 'C',
  37. 'stretch' => false,
  38. 'fitwidth' => true,
  39. 'cellfitalign' => '',
  40. 'border' => false,
  41. 'hpadding' => 'auto',
  42. 'vpadding' => 'auto',
  43. 'fgcolor' => array(0, 0, 0),
  44. 'bgcolor' => false,
  45. 'text' => true,
  46. 'font' => 'helvetica',
  47. 'fontsize' => 8,
  48. 'stretchtext' => 4
  49. );
  50. // set style for 2d barcode
  51. private $_style2d = array(
  52. 'border' => false,
  53. 'vpadding' => 'auto',
  54. 'hpadding' => 'auto',
  55. 'fgcolor' => array(0, 0, 0),
  56. 'bgcolor' => false,
  57. 'module_width' => 1, // width of a single module in points
  58. 'module_height' => 1 // height of a single module in points
  59. );
  60. private $_align2d = 'N';
  61. private $_xres = 0.4;
  62. /**
  63. * write barcode to pdf
  64. *
  65. * @param TCPDF $pdf PDF reference
  66. * @param string $code code to print
  67. * @param string $encoding type of barcode
  68. * @param boolean $is2d true if 2d barcode
  69. * @param int $x x position in user units
  70. * @param int $y y position in user units
  71. * @param int $w width in user units
  72. * @param int $h height in user units
  73. * @return void
  74. */
  75. private function writeBarcode(&$pdf, $code, $encoding, $is2d, $x, $y, $w, $h)
  76. {
  77. if ($is2d) {
  78. $pdf->write2DBarcode($code, $encoding, $x, $y, $w, $h, $this->_style2d, $this->_align2d);
  79. } else {
  80. $pdf->write1DBarcode($code, $encoding, $x, $y, $w, $h, $this->_xres, $this->_style1d);
  81. }
  82. }
  83. /**
  84. * Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
  85. *
  86. * @param TCPDF $pdf PDF reference
  87. * @param Translate $outputlangs Output langs
  88. * @param array $param Associative array containing label content and optional parameters
  89. * @return void
  90. */
  91. public function addSticker(&$pdf, $outputlangs, $param)
  92. {
  93. global $mysoc, $conf;
  94. $textleft = $param['textleft'];
  95. $header = $param['textheader'];
  96. $footer = $param['textfooter'];
  97. $textright = $param['textright'];
  98. $code = $param['code'];
  99. $encoding = $param['encoding'];
  100. $is2d = $param['is2d'];
  101. // We are in a new page, then we must add a page
  102. if (($this->_COUNTX == 0) && ($this->_COUNTY == 0) and (!$this->_First == 1)) {
  103. $pdf->AddPage();
  104. }
  105. $this->_First = 0;
  106. $_PosX = $this->_Margin_Left + ($this->_COUNTX * ($this->_Width + $this->_X_Space));
  107. $_PosY = $this->_Margin_Top + ($this->_COUNTY * ($this->_Height + $this->_Y_Space));
  108. // Define logo
  109. $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  110. if (!is_readable($logo)) {
  111. $logo = '';
  112. if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
  113. $logo = $conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
  114. } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
  115. $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  116. }
  117. }
  118. $xleft = 2;
  119. $ytop = 2;
  120. // Top
  121. if ($header != '') {
  122. $pdf->SetXY($_PosX + $xleft, $_PosY + 1); // Only 1 mm and not ytop for top text
  123. $pdf->Cell($this->_Width - 2 * $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
  124. }
  125. $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
  126. // Define widthtouse and heighttouse
  127. $pageMargins = $pdf->getMargins();
  128. $maxwidthtouse = round($this->_Width - 2 * $xleft);
  129. $maxheighttouse = round($this->_Height - 2 * $ytop);
  130. $maxheighttouse -= (empty($footer) ? 0 : (1 + $this->_Line_Height));
  131. $defaultratio = ($maxwidthtouse / $maxheighttouse);
  132. $widthtouse = $maxwidthtouse;
  133. $heighttouse = $maxheighttouse;
  134. $logoHeight = $heighttouse;
  135. $logoWidth = $widthtouse;
  136. //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
  137. // Center
  138. if ($textright == '') { // Only a left part
  139. // Output left area
  140. if ($textleft == '%LOGO%' && $logo) {
  141. $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, 0, $logoHeight);
  142. } elseif ($code && !empty($encoding)) {
  143. $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  144. } else {
  145. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  146. $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  147. }
  148. } elseif ($textleft != '' && $textright != '') { // left and right part
  149. $logoHeight = $heighttouse / 2;
  150. $logoWidth = $widthtouse / 2;
  151. if (($textleft == '%LOGO%' || $textleft == '%PHOTO%' || $textleft == '%BARCODE%') && !strstr($textright, '%')) { // left part logo/barcode right part text
  152. if ($textleft == '%LOGO%' && $logo) {
  153. $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $logoWidth, 0);
  154. } elseif ($code && !empty($encoding)) {
  155. $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse / 2, $heighttouse);
  156. }
  157. $pdf->SetXY($_PosX + ($widthtouse / 2), $_PosY + $ytop);
  158. $pdf->MultiCell($widthtouse / 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
  159. } elseif (($textright == '%LOGO%' || $textright == '%PHOTO%' || $textright == '%BARCODE%') && !strstr($textleft, '%')) { // right part logo/barcode left part text
  160. if ($textright == '%LOGO%' && $logo) {
  161. $pdf->Image($logo, $_PosX + ($widthtouse / 2), $_PosY + $ytop, $logoWidth, 0);
  162. } elseif ($code && !empty($encoding)) {
  163. $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + ($widthtouse / 2), $_PosY + $ytop, $widthtouse / 2, $heighttouse);
  164. }
  165. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  166. $pdf->MultiCell($widthtouse / 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  167. } elseif ($textleft == '%LOGO%') { // left part logo right part text/barcode
  168. if ($logo) {
  169. $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, 0, $logoHeight);
  170. }
  171. if ($code && !empty($encoding)) {
  172. $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft + $logoWidth + 1, $_PosY + $ytop, $widthtouse - $logoWidth - 1, $heighttouse);
  173. } else {
  174. $pdf->SetXY($_PosX + $xleft + $logoWidth + 1, $_PosY + $ytop);
  175. $pdf->MultiCell($widthtouse - $logoWidth - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
  176. }
  177. } elseif ($textright == '%LOGO%') { // right part logo left part text/barcode
  178. if ($logo) {
  179. $pdf->Image($logo, $_PosX + $xleft + $widthtouse - $logoWidth + 1, $_PosY + $ytop, 0, $logoHeight);
  180. }
  181. if ($code && !empty($encoding)) {
  182. $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $xleft, $_PosY + $ytop, $widthtouse - $logoWidth - 1, $heighttouse);
  183. } else {
  184. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  185. $pdf->MultiCell($widthtouse - $logoWidth - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  186. }
  187. } else // text on halft left and text on half right
  188. {
  189. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  190. $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  191. $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
  192. $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
  193. }
  194. } else // Only a right part
  195. {
  196. // Output right area
  197. if ($textright == '%LOGO%' && $logo) {
  198. $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, 0, $logoHeight);
  199. } elseif ($code && !empty($encoding)) {
  200. $this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  201. } else {
  202. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  203. $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
  204. }
  205. }
  206. // Bottom
  207. if ($footer != '') {
  208. $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
  209. $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
  210. }
  211. //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
  212. $this->_COUNTY++;
  213. if ($this->_COUNTY == $this->_Y_Number) {
  214. // Si on est en bas de page, on remonte le 'curseur' de position
  215. $this->_COUNTX++;
  216. $this->_COUNTY = 0;
  217. }
  218. if ($this->_COUNTX == $this->_X_Number) {
  219. // Si on est en bout de page, alors on repart sur une nouvelle page
  220. $this->_COUNTX = 0;
  221. $this->_COUNTY = 0;
  222. }
  223. }
  224. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  225. /**
  226. * Function to build PDF on disk, then output on HTTP strem.
  227. *
  228. * @param array $arrayofrecords Array of record informations (array('textleft'=>,'textheader'=>, ..., 'id'=>,'photo'=>)
  229. * @param Translate $outputlangs Lang object for output language
  230. * @param string $srctemplatepath Full path of source filename for generator using a template file
  231. * @param string $outputdir Output directory for pdf file
  232. * @param string $filename Short file name of PDF output file
  233. * @return int 1=OK, 0=KO
  234. */
  235. public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
  236. {
  237. // phpcs:enable
  238. global $user, $conf, $langs, $mysoc, $_Avery_Labels;
  239. $this->code = $srctemplatepath;
  240. $this->Tformat = $_Avery_Labels[$this->code];
  241. if (empty($this->Tformat)) {
  242. dol_print_error('', 'ErrorBadTypeForCard'.$this->code);
  243. exit;
  244. }
  245. $this->type = 'pdf';
  246. // standard format or custom
  247. if ($this->Tformat['paper-size'] != 'custom') {
  248. $this->format = $this->Tformat['paper-size'];
  249. } else {
  250. //custom
  251. $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
  252. $this->format = $resolution;
  253. }
  254. if (!is_object($outputlangs)) {
  255. $outputlangs = $langs;
  256. }
  257. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  258. if (!empty($conf->global->MAIN_USE_FPDF)) {
  259. $outputlangs->charset_output = 'ISO-8859-1';
  260. }
  261. // Load traductions files required by page
  262. $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
  263. $title = $outputlangs->transnoentities('Labels');
  264. $keywords = $title." ".$outputlangs->convToOutputCharset($mysoc->name);
  265. $dir = (empty($outputdir) ? $conf->adherent->dir_temp : $outputdir);
  266. $file = $dir."/".$filename;
  267. if (!file_exists($dir)) {
  268. if (dol_mkdir($dir) < 0) {
  269. $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
  270. return 0;
  271. }
  272. }
  273. $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
  274. if (class_exists('TCPDF')) {
  275. $pdf->setPrintHeader(false);
  276. $pdf->setPrintFooter(false);
  277. }
  278. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  279. $pdf->SetTitle($title);
  280. $pdf->SetSubject($title);
  281. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  282. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  283. $pdf->SetKeyWords($keywords);
  284. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  285. $pdf->SetCompression(false);
  286. }
  287. $pdf->SetMargins(0, 0);
  288. $pdf->SetAutoPageBreak(false);
  289. $this->_Metric_Doc = $this->Tformat['metric'];
  290. // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
  291. $posX = 1;
  292. $posY = 1;
  293. if ($posX > 0) {
  294. $posX--;
  295. } else {
  296. $posX = 0;
  297. }
  298. if ($posY > 0) {
  299. $posY--;
  300. } else {
  301. $posY = 0;
  302. }
  303. $this->_COUNTX = $posX;
  304. $this->_COUNTY = $posY;
  305. $this->_Set_Format($pdf, $this->Tformat);
  306. $pdf->Open();
  307. $pdf->AddPage();
  308. // Add each record
  309. foreach ($arrayofrecords as $val) {
  310. // imprime le texte specifique sur la carte
  311. $this->addSticker($pdf, $outputlangs, $val);
  312. }
  313. //$pdf->SetXY(10, 295);
  314. //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
  315. // Output to file
  316. $pdf->Output($file, 'F');
  317. if (!empty($conf->global->MAIN_UMASK)) {
  318. @chmod($file, octdec($conf->global->MAIN_UMASK));
  319. }
  320. $this->result = array('fullpath'=>$file);
  321. return 1;
  322. }
  323. }