pdf_standardlabel.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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_standardlabel extends CommonStickerGenerator
  32. {
  33. /**
  34. * Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
  35. *
  36. * @param TCPDF $pdf PDF reference
  37. * @param Translate $outputlangs Output langs
  38. * @param array $param Associative array containing label content and optional parameters
  39. * @return void
  40. */
  41. public function addSticker(&$pdf, $outputlangs, $param)
  42. {
  43. // use this method in future refactoring
  44. }
  45. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  46. /**
  47. * Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
  48. * - %LOGO% is replace with company logo
  49. * - %PHOTO% is replace with photo provided as parameter
  50. *
  51. * @param TCPDF $pdf PDF reference
  52. * @param string $textleft Text left
  53. * @param string $header Header
  54. * @param string $footer Footer
  55. * @param Translate $outputlangs Output langs
  56. * @param string $textright Text right
  57. * @param string $photo Photo (full path to image file used as replacement for key %PHOTOS% into left, right, header or footer text)
  58. * @return void
  59. */
  60. public function Add_PDF_label(&$pdf, $textleft, $header, $footer, $outputlangs, $textright = '', $photo = '')
  61. {
  62. // phpcs:enable
  63. global $mysoc, $conf, $langs;
  64. global $forceimgscalewidth, $forceimgscaleheight;
  65. $imgscalewidth = (empty($forceimgscalewidth) ? 0.3 : $forceimgscalewidth); // Scale of image for width (1=Full width of sticker)
  66. $imgscaleheight = (empty($forceimgscaleheight) ? 0.5 : $forceimgscaleheight); // Scale of image for height (1=Full height of sticker)
  67. // We are in a new page, then we must add a page
  68. if (($this->_COUNTX == 0) && ($this->_COUNTY == 0) and (!$this->_First == 1)) {
  69. $pdf->AddPage();
  70. }
  71. $this->_First = 0;
  72. $_PosX = $this->_Margin_Left + ($this->_COUNTX * ($this->_Width + $this->_X_Space));
  73. $_PosY = $this->_Margin_Top + ($this->_COUNTY * ($this->_Height + $this->_Y_Space));
  74. // Define logo
  75. $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  76. if (!is_readable($logo)) {
  77. $logo = '';
  78. if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
  79. $logo = $conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
  80. } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
  81. $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  82. }
  83. }
  84. // Define photo
  85. if (!empty($photo)) {
  86. if (!is_readable($photo)) {
  87. $photo = '';
  88. }
  89. }
  90. // Define background image
  91. $backgroundimage = '';
  92. // Print lines
  93. if ($this->code == "CARD") {
  94. $this->Tformat = $this->_Avery_Labels["CARD"];
  95. //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
  96. $this->_Croix($pdf, $_PosX, $_PosY, $_PosX + $this->_Width, $_PosY + $this->_Height, 0.1, 10);
  97. }
  98. // Background
  99. if ($backgroundimage) {
  100. $pdf->image($backgroundimage, $_PosX, $_PosY, $this->_Width, $this->_Height);
  101. }
  102. $xleft = 2;
  103. $ytop = 2;
  104. // Top
  105. if ($header != '') {
  106. if ($this->code == "CARD") {
  107. $pdf->SetDrawColor(128, 128, 128);
  108. $pdf->Line($_PosX, $_PosY + $this->_Line_Height + 1, $_PosX + $this->_Width, $_PosY + $this->_Line_Height + 1); // Only 1 mm and not ytop for top text
  109. $pdf->SetDrawColor(0, 0, 0);
  110. }
  111. $pdf->SetXY($_PosX + $xleft, $_PosY + 1); // Only 1 mm and not ytop for top text
  112. $pdf->Cell($this->_Width - 2 * $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
  113. }
  114. $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
  115. // Define widthtouse and heighttouse
  116. $maxwidthtouse = round(($this->_Width - 2 * $xleft) * $imgscalewidth);
  117. $maxheighttouse = round(($this->_Height - 2 * $ytop) * $imgscaleheight);
  118. $defaultratio = ($maxwidthtouse / $maxheighttouse);
  119. $widthtouse = $maxwidthtouse;
  120. $heighttouse = 0; // old value for image
  121. $tmp = dol_getImageSize($photo, false);
  122. if (!empty($tmp['height'])) {
  123. $imgratio = $tmp['width'] / $tmp['height'];
  124. if ($imgratio >= $defaultratio) {
  125. $widthtouse = $maxwidthtouse;
  126. $heighttouse = round($widthtouse / $imgratio);
  127. } else {
  128. $heighttouse = $maxheighttouse;
  129. $widthtouse = round($heighttouse * $imgratio);
  130. }
  131. }
  132. //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
  133. // Center
  134. if ($textright == '') { // Only a left part
  135. // Output left area
  136. if ($textleft == '%LOGO%' && $logo) {
  137. $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  138. } elseif ($textleft == '%PHOTO%' && $photo) {
  139. $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  140. } else {
  141. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  142. $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  143. }
  144. } elseif ($textleft != '' && $textright != '') { //
  145. if ($textleft == '%LOGO%' || $textleft == '%PHOTO%') {
  146. if ($textleft == '%LOGO%' && $logo) {
  147. $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  148. } elseif ($textleft == '%PHOTO%' && $photo) {
  149. $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  150. }
  151. $pdf->SetXY($_PosX + $xleft + $widthtouse + 1, $_PosY + $ytop);
  152. $pdf->MultiCell($this->_Width - $xleft - $xleft - $widthtouse - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
  153. } elseif ($textright == '%LOGO%' || $textright == '%PHOTO%') {
  154. if ($textright == '%LOGO%' && $logo) {
  155. $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  156. } elseif ($textright == '%PHOTO%' && $photo) {
  157. $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  158. }
  159. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  160. $pdf->MultiCell($this->_Width - $widthtouse - $xleft - $xleft - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  161. } else // text on halft left and text on half right
  162. {
  163. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  164. $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  165. $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
  166. $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
  167. }
  168. } else // Only a right part
  169. {
  170. // Output right area
  171. if ($textright == '%LOGO%' && $logo) {
  172. $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  173. } elseif ($textright == '%PHOTO%' && $photo) {
  174. $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  175. } else {
  176. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  177. $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
  178. }
  179. }
  180. // Bottom
  181. if ($footer != '') {
  182. if ($this->code == "CARD") {
  183. $pdf->SetDrawColor(128, 128, 128);
  184. $pdf->Line($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 2, $_PosX + $this->_Width, $_PosY + $this->_Height - $this->_Line_Height - 2);
  185. $pdf->SetDrawColor(0, 0, 0);
  186. }
  187. $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
  188. $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
  189. }
  190. //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
  191. $this->_COUNTY++;
  192. if ($this->_COUNTY == $this->_Y_Number) {
  193. // Si on est en bas de page, on remonte le 'curseur' de position
  194. $this->_COUNTX++;
  195. $this->_COUNTY = 0;
  196. }
  197. if ($this->_COUNTX == $this->_X_Number) {
  198. // Si on est en bout de page, alors on repart sur une nouvelle page
  199. $this->_COUNTX = 0;
  200. $this->_COUNTY = 0;
  201. }
  202. }
  203. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  204. /**
  205. * Function to build PDF on disk, then output on HTTP strem.
  206. *
  207. * @param array $arrayofrecords Array of record informations (array('textleft'=>,'textheader'=>, ..., 'id'=>,'photo'=>)
  208. * @param Translate $outputlangs Lang object for output language
  209. * @param string $srctemplatepath Full path of source filename for generator using a template file
  210. * @param string $outputdir Output directory for pdf file
  211. * @param string $filename Short file name of PDF output file
  212. * @return int 1=OK, 0=KO
  213. */
  214. public function write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir = '', $filename = 'tmp_address_sheet.pdf')
  215. {
  216. // phpcs:enable
  217. global $user, $conf, $langs, $mysoc, $_Avery_Labels;
  218. $this->code = $srctemplatepath;
  219. $this->Tformat = $_Avery_Labels[$this->code];
  220. if (empty($this->Tformat)) {
  221. dol_print_error('', 'ErrorBadTypeForCard'.$this->code);
  222. exit;
  223. }
  224. $this->type = 'pdf';
  225. // standard format or custom
  226. if ($this->Tformat['paper-size'] != 'custom') {
  227. $this->format = $this->Tformat['paper-size'];
  228. } else {
  229. //custom
  230. $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
  231. $this->format = $resolution;
  232. }
  233. if (!is_object($outputlangs)) {
  234. $outputlangs = $langs;
  235. }
  236. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  237. if (!empty($conf->global->MAIN_USE_FPDF)) {
  238. $outputlangs->charset_output = 'ISO-8859-1';
  239. }
  240. // Load traductions files required by page
  241. $outputlangs->loadLangs(array("main", "dict", "companies", "admin"));
  242. $title = $outputlangs->transnoentities('Labels');
  243. $keywords = $title." ".$outputlangs->convToOutputCharset($mysoc->name);
  244. $dir = (empty($outputdir) ? $conf->adherent->dir_temp : $outputdir);
  245. $file = $dir."/".$filename;
  246. if (!file_exists($dir)) {
  247. if (dol_mkdir($dir) < 0) {
  248. $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
  249. return 0;
  250. }
  251. }
  252. $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
  253. if (class_exists('TCPDF')) {
  254. $pdf->setPrintHeader(false);
  255. $pdf->setPrintFooter(false);
  256. }
  257. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  258. $pdf->SetTitle($title);
  259. $pdf->SetSubject($title);
  260. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  261. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  262. $pdf->SetKeyWords($keywords);
  263. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  264. $pdf->SetCompression(false);
  265. }
  266. $pdf->SetMargins(0, 0);
  267. $pdf->SetAutoPageBreak(false);
  268. $this->_Metric_Doc = $this->Tformat['metric'];
  269. // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
  270. $posX = 1;
  271. $posY = 1;
  272. if ($posX > 0) {
  273. $posX--;
  274. } else {
  275. $posX = 0;
  276. }
  277. if ($posY > 0) {
  278. $posY--;
  279. } else {
  280. $posY = 0;
  281. }
  282. $this->_COUNTX = $posX;
  283. $this->_COUNTY = $posY;
  284. $this->_Set_Format($pdf, $this->Tformat);
  285. $pdf->Open();
  286. $pdf->AddPage();
  287. // Add each record
  288. foreach ($arrayofrecords as $val) {
  289. // imprime le texte specifique sur la carte
  290. $this->Add_PDF_label($pdf, $val['textleft'], $val['textheader'], $val['textfooter'], $langs, $val['textright'], $val['photo']);
  291. }
  292. //$pdf->SetXY(10, 295);
  293. //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
  294. // Output to file
  295. $pdf->Output($file, 'F');
  296. if (!empty($conf->global->MAIN_UMASK)) {
  297. @chmod($file, octdec($conf->global->MAIN_UMASK));
  298. }
  299. $this->result = array('fullpath'=>$file);
  300. return 1;
  301. }
  302. }