pdf_standard.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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/member/doc/pdf_standard.class.php
  24. * \ingroup member
  25. * \brief File of class to generate PDF document of labels
  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_standard extends CommonStickerGenerator
  32. {
  33. /**
  34. * Dolibarr version of the loaded document
  35. * @var string
  36. */
  37. public $version = 'dolibarr';
  38. /**
  39. * Constructor
  40. *
  41. * @param DoliDB $db Database handler
  42. */
  43. public function __construct($db)
  44. {
  45. global $conf, $langs, $mysoc;
  46. // Translations
  47. $langs->loadLangs(array("main", "admin"));
  48. $this->db = $db;
  49. $this->name = "standard";
  50. $this->description = $langs->trans('TemplateforBusinessCards');
  51. //$this->update_main_doc_field = 1; // Save the name of generated file as the main doc when generating a doc with this template
  52. $this->type = 'pdf-various-sizes';
  53. }
  54. /**
  55. * Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
  56. *
  57. * @param TCPDF $pdf PDF reference
  58. * @param Translate $outputlangs Output langs
  59. * @param array $param Associative array containing label content and optional parameters
  60. * @return void
  61. */
  62. public function addSticker(&$pdf, $outputlangs, $param)
  63. {
  64. // use this method in future refactoring
  65. }
  66. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  67. /**
  68. * Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
  69. * - __LOGO__ is replace with company logo
  70. * - __PHOTO__ is replace with photo provided as parameter
  71. *
  72. * @param TCPDF $pdf PDF
  73. * @param string $textleft Text left
  74. * @param string $header Header
  75. * @param string $footer Footer
  76. * @param Translate $outputlangs Output langs
  77. * @param string $textright Text right
  78. * @param int $idmember Id member
  79. * @param string $photo Photo (full path to image file used as replacement for key __PHOTOS__ into left, right, header or footer text)
  80. * @return void
  81. */
  82. public function Add_PDF_card(&$pdf, $textleft, $header, $footer, $outputlangs, $textright = '', $idmember = 0, $photo = '')
  83. {
  84. // phpcs:enable
  85. global $db, $mysoc, $conf, $langs;
  86. global $forceimgscalewidth, $forceimgscaleheight;
  87. $imgscalewidth = (empty($forceimgscalewidth) ? 0.3 : $forceimgscalewidth); // Scale of image for width (1=Full width of sticker)
  88. $imgscaleheight = (empty($forceimgscalewidth) ? 0.5 : $forceimgscalewidth); // Scale of image for height (1=Full height of sticker)
  89. // We are in a new page, then we must add a page
  90. if (($this->_COUNTX == 0) && ($this->_COUNTY == 0) and (!$this->_First == 1)) {
  91. $pdf->AddPage();
  92. }
  93. $this->_First = 0;
  94. $_PosX = $this->_Margin_Left + ($this->_COUNTX * ($this->_Width + $this->_X_Space));
  95. $_PosY = $this->_Margin_Top + ($this->_COUNTY * ($this->_Height + $this->_Y_Space));
  96. // Define logo
  97. $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  98. if (!is_readable($logo)) {
  99. $logo = '';
  100. if (!empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small)) {
  101. $logo = $conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
  102. } elseif (!empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo)) {
  103. $logo = $conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  104. }
  105. }
  106. $member = new Adherent($db);
  107. $member->id = $idmember;
  108. $member->ref = $idmember;
  109. // Define photo
  110. $dir = $conf->adherent->dir_output;
  111. if (!empty($photo)) {
  112. $file = get_exdir(0, 0, 0, 0, $member, 'member').'photos/'.$photo;
  113. $photo = $dir.'/'.$file;
  114. if (!is_readable($photo)) {
  115. $photo = '';
  116. }
  117. }
  118. // Define background image
  119. $backgroundimage = '';
  120. if (!empty($conf->global->ADHERENT_CARD_BACKGROUND) && file_exists($conf->adherent->dir_output.'/'.$conf->global->ADHERENT_CARD_BACKGROUND)) {
  121. $backgroundimage = $conf->adherent->dir_output.'/'.$conf->global->ADHERENT_CARD_BACKGROUND;
  122. }
  123. // Print lines
  124. if ($this->code == "CARD") {
  125. $this->Tformat = $this->_Avery_Labels["CARD"];
  126. //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
  127. $this->_Croix($pdf, $_PosX, $_PosY, $_PosX + $this->_Width, $_PosY + $this->_Height, 0.1, 10);
  128. }
  129. // Background
  130. if ($backgroundimage) {
  131. $pdf->image($backgroundimage, $_PosX, $_PosY, $this->_Width, $this->_Height);
  132. }
  133. $xleft = 2;
  134. $ytop = 2;
  135. // Top
  136. if ($header != '') {
  137. if ($this->code == "CARD") {
  138. $pdf->SetDrawColor(128, 128, 128);
  139. $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
  140. $pdf->SetDrawColor(0, 0, 0);
  141. }
  142. $pdf->SetXY($_PosX + $xleft, $_PosY + 1); // Only 1 mm and not ytop for top text
  143. $pdf->Cell($this->_Width - 2 * $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header), 0, 1, 'C');
  144. }
  145. $ytop += (empty($header) ? 0 : (1 + $this->_Line_Height));
  146. // Define widthtouse and heighttouse
  147. $maxwidthtouse = round(($this->_Width - 2 * $xleft) * $imgscalewidth);
  148. $maxheighttouse = round(($this->_Height - 2 * $ytop) * $imgscaleheight);
  149. $defaultratio = ($maxwidthtouse / $maxheighttouse);
  150. $widthtouse = $maxwidthtouse;
  151. $heighttouse = 0; // old value for image
  152. $tmp = dol_getImageSize($photo, false);
  153. if (isset($tmp['height'])) {
  154. $imgratio = $tmp['width'] / $tmp['height'];
  155. if ($imgratio >= $defaultratio) {
  156. $widthtouse = $maxwidthtouse;
  157. $heighttouse = round($widthtouse / $imgratio);
  158. } else {
  159. $heighttouse = $maxheighttouse;
  160. $widthtouse = round($heighttouse * $imgratio);
  161. }
  162. }
  163. //var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
  164. // Center
  165. if ($textright == '') { // Only a left part
  166. // Output left area
  167. if ($textleft == '__LOGO__' && $logo) {
  168. $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  169. } elseif ($textleft == '__PHOTO__' && $photo) {
  170. $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  171. } else {
  172. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  173. $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  174. }
  175. } elseif ($textleft != '' && $textright != '') { //
  176. if ($textleft == '__LOGO__' || $textleft == '__PHOTO__') {
  177. if ($textleft == '__LOGO__' && $logo) {
  178. $pdf->Image($logo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  179. } elseif ($textleft == '__PHOTO__' && $photo) {
  180. $pdf->Image($photo, $_PosX + $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  181. }
  182. $pdf->SetXY($_PosX + $xleft + $widthtouse + 1, $_PosY + $ytop);
  183. $pdf->MultiCell($this->_Width - $xleft - $xleft - $widthtouse - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
  184. } elseif ($textright == '__LOGO__' || $textright == '__PHOTO__') {
  185. if ($textright == '__LOGO__' && $logo) {
  186. $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  187. } elseif ($textright == '__PHOTO__' && $photo) {
  188. $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  189. }
  190. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  191. $pdf->MultiCell($this->_Width - $widthtouse - $xleft - $xleft - 1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  192. } else // text on halft left and text on half right
  193. {
  194. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  195. $pdf->MultiCell(round($this->_Width / 2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  196. $pdf->SetXY($_PosX + round($this->_Width / 2), $_PosY + $ytop);
  197. $pdf->MultiCell(round($this->_Width / 2) - 2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
  198. }
  199. } else // Only a right part
  200. {
  201. // Output right area
  202. if ($textright == '__LOGO__' && $logo) {
  203. $pdf->Image($logo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  204. } elseif ($textright == '__PHOTO__' && $photo) {
  205. $pdf->Image($photo, $_PosX + $this->_Width - $widthtouse - $xleft, $_PosY + $ytop, $widthtouse, $heighttouse);
  206. } else {
  207. $pdf->SetXY($_PosX + $xleft, $_PosY + $ytop);
  208. $pdf->MultiCell($this->_Width - $xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
  209. }
  210. }
  211. // Bottom
  212. if ($footer != '') {
  213. if ($this->code == "CARD") {
  214. $pdf->SetDrawColor(128, 128, 128);
  215. $pdf->Line($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 2, $_PosX + $this->_Width, $_PosY + $this->_Height - $this->_Line_Height - 2);
  216. $pdf->SetDrawColor(0, 0, 0);
  217. }
  218. $pdf->SetXY($_PosX, $_PosY + $this->_Height - $this->_Line_Height - 1);
  219. $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer), 0, 1, 'C');
  220. }
  221. //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
  222. $this->_COUNTY++;
  223. if ($this->_COUNTY == $this->_Y_Number) {
  224. // Si on est en bas de page, on remonte le 'curseur' de position
  225. $this->_COUNTX++;
  226. $this->_COUNTY = 0;
  227. }
  228. if ($this->_COUNTX == $this->_X_Number) {
  229. // Si on est en bout de page, alors on repart sur une nouvelle page
  230. $this->_COUNTX = 0;
  231. $this->_COUNTY = 0;
  232. }
  233. }
  234. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  235. /**
  236. * Function to build PDF on disk, then output on HTTP stream.
  237. *
  238. * @param Adherent $object Member object. Old usage: Array of record informations (array('textleft'=>,'textheader'=>, ...'id'=>,'photo'=>)
  239. * @param Translate $outputlangs Lang object for output language
  240. * @param string $srctemplatepath Full path of source filename for generator using a template file. Example: '5161', 'AVERYC32010', 'CARD', ...
  241. * @param string $mode Tell if doc module is called for 'member', ...
  242. * @param int $nooutput 1=Generate only file on disk and do not return it on response
  243. * @param string $filename Name of output file (without extension)
  244. * @return int 1=OK, 0=KO
  245. */
  246. public function write_file($object, $outputlangs, $srctemplatepath, $mode = 'member', $nooutput = 0, $filename = 'tmp_cards')
  247. {
  248. // phpcs:enable
  249. global $user, $conf, $langs, $mysoc, $_Avery_Labels;
  250. $this->code = $srctemplatepath;
  251. if (is_object($object)) {
  252. if ($object->country == '-') {
  253. $object->country = '';
  254. }
  255. $now = dol_now();
  256. $year = dol_print_date($now, '%Y');
  257. $month = dol_print_date($now, '%m');
  258. $day = dol_print_date($now, '%d');
  259. // List of values to scan for a replacement
  260. $substitutionarray = array(
  261. '__ID__' => $object->id,
  262. '__LOGIN__'=>$object->login,
  263. '__FIRSTNAME__'=>$object->firstname,
  264. '__LASTNAME__'=>$object->lastname,
  265. '__FULLNAME__'=>$object->getFullName($langs),
  266. '__COMPANY__'=>$object->company,
  267. '__ADDRESS__'=>$object->address,
  268. '__ZIP__'=>$object->zip,
  269. '__TOWN__'=>$object->town,
  270. '__COUNTRY__'=>$object->country,
  271. '__COUNTRY_CODE__'=>$object->country_code,
  272. '__EMAIL__'=>$object->email,
  273. '__BIRTH__'=>dol_print_date($object->birth, 'day'),
  274. '__TYPE__'=>$object->type,
  275. '__YEAR__'=>$year,
  276. '__MONTH__'=>$month,
  277. '__DAY__'=>$day,
  278. '__DOL_MAIN_URL_ROOT__'=>DOL_MAIN_URL_ROOT,
  279. '__SERVER__'=>"https://".$_SERVER["SERVER_NAME"]."/"
  280. );
  281. complete_substitutions_array($substitutionarray, $langs);
  282. // For business cards
  283. $textleft = make_substitutions(getDolGlobalString("ADHERENT_CARD_TEXT"), $substitutionarray);
  284. $textheader = make_substitutions(getDolGlobalString("ADHERENT_CARD_HEADER_TEXT"), $substitutionarray);
  285. $textfooter = make_substitutions(getDolGlobalString("ADHERENT_CARD_FOOTER_TEXT"), $substitutionarray);
  286. $textright = make_substitutions(getDolGlobalString("ADHERENT_CARD_TEXT_RIGHT"), $substitutionarray);
  287. $nb = $_Avery_Labels[$this->code]['NX'] * $_Avery_Labels[$this->code]['NY'];
  288. if ($nb <= 0) {
  289. $nb = 1; // Protection to avoid empty page
  290. }
  291. for ($j = 0; $j < $nb; $j++) {
  292. $arrayofmembers[] = array(
  293. 'textleft'=>$textleft,
  294. 'textheader'=>$textheader,
  295. 'textfooter'=>$textfooter,
  296. 'textright'=>$textright,
  297. 'id'=>(isset($object->rowid) ? $object->rowid : ""),
  298. 'photo'=>(isset($object->photo) ? $object->photo : "")
  299. );
  300. }
  301. $arrayofrecords = $arrayofmembers;
  302. } else {
  303. $arrayofrecords = $object;
  304. }
  305. //var_dump($arrayofrecords);exit;
  306. $this->Tformat = $_Avery_Labels[$this->code];
  307. if (empty($this->Tformat)) {
  308. dol_print_error('', 'ErrorBadTypeForCard'.$this->code);
  309. exit;
  310. }
  311. $this->type = 'pdf';
  312. $filename .= '.pdf';
  313. // standard format or custom
  314. if ($this->Tformat['paper-size'] != 'custom') {
  315. $this->format = $this->Tformat['paper-size'];
  316. } else {
  317. //custom
  318. $resolution = array($this->Tformat['custom_x'], $this->Tformat['custom_y']);
  319. $this->format = $resolution;
  320. }
  321. if (!is_object($outputlangs)) {
  322. $outputlangs = $langs;
  323. }
  324. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  325. if (!empty($conf->global->MAIN_USE_FPDF)) {
  326. $outputlangs->charset_output = 'ISO-8859-1';
  327. }
  328. // Load traductions files required by page
  329. $outputlangs->loadLangs(array("main", "dict", "companies", "admin", "members"));
  330. if (empty($mode) || $mode == 'member') {
  331. $title = $outputlangs->transnoentities('MembersCards');
  332. $keywords = $outputlangs->transnoentities('MembersCards')." ".$outputlangs->transnoentities("Foundation")." ".$outputlangs->convToOutputCharset($mysoc->name);
  333. } else {
  334. dol_print_error('', 'Bad value for $mode');
  335. return -1;
  336. }
  337. if (is_object($object)) {
  338. $outputdir = $conf->adherent->dir_output;
  339. $dir = $outputdir."/".get_exdir(0, 0, 0, 0, $object, 'member');
  340. $file = $dir.'/'.$filename;
  341. } else {
  342. $outputdir = $conf->adherent->dir_temp;
  343. $dir = $outputdir;
  344. $file = $dir.'/'.$filename;
  345. }
  346. //var_dump($file);exit;
  347. if (!file_exists($dir)) {
  348. if (dol_mkdir($dir) < 0) {
  349. $this->error = $langs->trans("ErrorCanNotCreateDir", $dir);
  350. return 0;
  351. }
  352. }
  353. $pdf = pdf_getInstance($this->format, $this->Tformat['metric'], $this->Tformat['orientation']);
  354. if (class_exists('TCPDF')) {
  355. $pdf->setPrintHeader(false);
  356. $pdf->setPrintFooter(false);
  357. }
  358. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  359. $pdf->SetTitle($title);
  360. $pdf->SetSubject($title);
  361. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  362. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  363. $pdf->SetKeyWords($keywords);
  364. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  365. $pdf->SetCompression(false);
  366. }
  367. $pdf->SetMargins(0, 0);
  368. $pdf->SetAutoPageBreak(false);
  369. $this->_Metric_Doc = $this->Tformat['metric'];
  370. // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
  371. $posX = 1;
  372. $posY = 1;
  373. if ($posX > 0) {
  374. $posX--;
  375. } else {
  376. $posX = 0;
  377. }
  378. if ($posY > 0) {
  379. $posY--;
  380. } else {
  381. $posY = 0;
  382. }
  383. $this->_COUNTX = $posX;
  384. $this->_COUNTY = $posY;
  385. $this->_Set_Format($pdf, $this->Tformat);
  386. $pdf->Open();
  387. $pdf->AddPage();
  388. // Add each record
  389. foreach ($arrayofrecords as $val) {
  390. // imprime le texte specifique sur la carte
  391. $this->Add_PDF_card($pdf, $val['textleft'], $val['textheader'], $val['textfooter'], $langs, $val['textright'], $val['id'], $val['photo']);
  392. }
  393. //$pdf->SetXY(10, 295);
  394. //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
  395. // Output to file
  396. $pdf->Output($file, 'F');
  397. if (!empty($conf->global->MAIN_UMASK)) {
  398. @chmod($file, octdec($conf->global->MAIN_UMASK));
  399. }
  400. $this->result = array('fullpath'=>$file);
  401. // Output to http stream
  402. if (empty($nooutput)) {
  403. clearstatcache();
  404. $attachment = true;
  405. if (!empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) {
  406. $attachment = false;
  407. }
  408. $type = dol_mimetype($filename);
  409. //if ($encoding) header('Content-Encoding: '.$encoding);
  410. if ($type) {
  411. header('Content-Type: '.$type);
  412. }
  413. if ($attachment) {
  414. header('Content-Disposition: attachment; filename="'.$filename.'"');
  415. } else {
  416. header('Content-Disposition: inline; filename="'.$filename.'"');
  417. }
  418. // Ajout directives pour resoudre bug IE
  419. header('Cache-Control: Public, must-revalidate');
  420. header('Pragma: public');
  421. readfile($file);
  422. }
  423. return 1;
  424. }
  425. }