PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/include/Sugarpdf/Sugarpdf.php

https://github.com/vincentamari/SuperSweetAdmin
PHP | 658 lines | 390 code | 65 blank | 203 comment | 80 complexity | c338aadbb7e308c2c5e1ca85e9d32c2a MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, AGPL-3.0, LGPL-2.1
  1. <?php
  2. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  3. /*********************************************************************************
  4. * SugarCRM is a customer relationship management program developed by
  5. * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU Affero General Public License version 3 as published by the
  9. * Free Software Foundation with the addition of the following permission added
  10. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  11. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  12. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License along with
  20. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  21. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301 USA.
  23. *
  24. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  25. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by SugarCRM".
  36. ********************************************************************************/
  37. if(file_exists('custom/include/Sugarpdf/sugarpdf_config.php')){
  38. require_once('custom/include/Sugarpdf/sugarpdf_config.php');
  39. } else {
  40. require_once('include/Sugarpdf/sugarpdf_config.php');
  41. }
  42. require_once('include/tcpdf/tcpdf.php');
  43. require_once('include/Sugarpdf/SugarpdfHelper.php');
  44. class Sugarpdf extends TCPDF
  45. {
  46. /**
  47. * This array is meant to hold an objects/data that we would like to pass between
  48. * the controller and the view. The bean will automatically be set for us, but this
  49. * is meant to hold anything else.
  50. */
  51. var $sugarpdf_object_map = array();
  52. /**
  53. * The name of the current module.
  54. */
  55. var $module = '';
  56. /**
  57. * The name of the current action.
  58. */
  59. var $action = '';
  60. /**
  61. */
  62. var $bean = null;
  63. /**
  64. * Any errors that occured this can either be set by the view or the controller or the model
  65. */
  66. var $errors = array();
  67. /**
  68. * Use to set the filename of the output pdf file.
  69. */
  70. var $fileName = PDF_FILENAME;
  71. /**
  72. * Use for the ACL access.
  73. */
  74. var $aclAction = PDF_ACL_ACCESS;
  75. /**
  76. * Constructor which will peform the setup.
  77. */
  78. function __construct($bean = null, $sugarpdf_object_map = array(),$orientation=PDF_PAGE_ORIENTATION, $unit=PDF_UNIT, $format=PDF_PAGE_FORMAT, $unicode=true, $encoding='UTF-8', $diskcache=false){
  79. global $locale;
  80. // $encoding = $locale->getExportCharset();
  81. if(empty($encoding)){
  82. $encoding = "UTF-8";
  83. }
  84. parent::__construct($orientation,$unit,$format,$unicode,$encoding,$diskcache);
  85. $this->module = $GLOBALS['module'];
  86. $this->bean = &$bean;
  87. $this->sugarpdf_object_map = $sugarpdf_object_map;
  88. if(!empty($_REQUEST["sugarpdf"])){
  89. $this->action = $_REQUEST["sugarpdf"];
  90. }
  91. }
  92. /**
  93. * This method will be called from the controller and is not meant to be overridden.
  94. */
  95. function process(){
  96. $this->preDisplay();
  97. $this->display();
  98. }
  99. /**
  100. * This method will display the errors on the page.
  101. */
  102. function displayErrors(){
  103. foreach($this->errors as $error) {
  104. echo '<span class="error">' . $error . '</span><br>';
  105. }
  106. }
  107. /**
  108. * [OVERRIDE] - This method is meant to overidden in a subclass. The purpose of this method is
  109. * to allow a view to do some preprocessing before the display method is called. This becomes
  110. * useful when you have a view defined at the application level and then within a module
  111. * have a sub-view that extends from this application level view. The application level
  112. * view can do the setup in preDisplay() that is common to itself and any subviews
  113. * and then the subview can just override display(). If it so desires, can also override
  114. * preDisplay().
  115. */
  116. function preDisplay(){
  117. // set document information
  118. $this->SetCreator(PDF_CREATOR);
  119. $this->SetAuthor(PDF_AUTHOR);
  120. $this->SetTitle(PDF_TITLE);
  121. $this->SetSubject(PDF_SUBJECT);
  122. $this->SetKeywords(PDF_KEYWORDS);
  123. // set other properties
  124. $compression=false;
  125. if(PDF_COMPRESSION == "on"){
  126. $compression=true;
  127. }
  128. $this->SetCompression($compression);
  129. $protection=array();
  130. if(PDF_PROTECTION != ""){
  131. $protection=explode(",",PDF_PROTECTION);
  132. }
  133. $this->SetProtection($protection,blowfishDecode(blowfishGetKey('sugarpdf_pdf_user_password'), PDF_USER_PASSWORD),blowfishDecode(blowfishGetKey('sugarpdf_pdf_owner_password'), PDF_OWNER_PASSWORD));
  134. $this->setCellHeightRatio(K_CELL_HEIGHT_RATIO);
  135. $this->setJPEGQuality(intval(PDF_JPEG_QUALITY));
  136. $this->setPDFVersion(PDF_PDF_VERSION);
  137. // set default header data
  138. $this->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
  139. // set header and footer fonts
  140. $this->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
  141. $this->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  142. //set margins
  143. $this->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
  144. $this->setHeaderMargin(PDF_MARGIN_HEADER);
  145. $this->setFooterMargin(PDF_MARGIN_FOOTER);
  146. //set auto page breaks
  147. $this->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  148. //set image scale factor
  149. $this->setImageScale(PDF_IMAGE_SCALE_RATIO);
  150. //set some language-dependent strings
  151. //$this->setLanguageArray($l);
  152. // ---------------------------------------------------------
  153. }
  154. /**
  155. * [OVERRIDE] - This method is meant to overidden in a subclass.
  156. */
  157. function display(){
  158. $this->AddPage();
  159. $this->SetFont(PDF_FONT_NAME_MAIN,'B',16);
  160. $this->MultiCell(0,0,'Tcpdf class for this module and action has not been implemented.',0,'C');
  161. $this->Info();
  162. }
  163. /**
  164. * [OVERRIDE]
  165. * This method override the regular Header() method to enable the custom image directory in addition to the OOB image directory.
  166. * This method is used to render the page header.
  167. * It is automatically called by AddPage().
  168. * @access public
  169. * @see include/tcpdf/TCPDF#Header()
  170. */
  171. public function Header() {
  172. $ormargins = $this->getOriginalMargins();
  173. $headerfont = $this->getHeaderFont();
  174. $headerdata = $this->getHeaderData();
  175. if (($headerdata['logo']) AND ($headerdata['logo'] != K_BLANK_IMAGE)) {
  176. // START SUGARPDF
  177. $logo = K_PATH_CUSTOM_IMAGES.$headerdata['logo'];
  178. $imsize = @getimagesize($logo);
  179. if ($imsize === FALSE) {
  180. // encode spaces on filename
  181. $logo = str_replace(' ', '%20', $logo);
  182. $imsize = @getimagesize($logo);
  183. if ($imsize === FALSE) {
  184. $logo = K_PATH_IMAGES.$headerdata['logo'];
  185. }
  186. }
  187. // END SUGARPDF
  188. $this->Image($logo, $this->GetX(), $this->getHeaderMargin(), $headerdata['logo_width']);
  189. $imgy = $this->getImageRBY();
  190. } else {
  191. $imgy = $this->GetY();
  192. }
  193. $cell_height = round(($this->getCellHeightRatio() * $headerfont[2]) / $this->getScaleFactor(), 2);
  194. // set starting margin for text data cell
  195. if ($this->getRTL()) {
  196. $header_x = $ormargins['right'] + ($headerdata['logo_width'] * 1.1);
  197. } else {
  198. $header_x = $ormargins['left'] + ($headerdata['logo_width'] * 1.1);
  199. }
  200. $this->SetTextColor(0, 0, 0);
  201. // header title
  202. $this->SetFont($headerfont[0], 'B', $headerfont[2] + 1);
  203. $this->SetX($header_x);
  204. $this->Cell(0, $cell_height, $headerdata['title'], 0, 1, '', 0, '', 0);
  205. // header string
  206. $this->SetFont($headerfont[0], $headerfont[1], $headerfont[2]);
  207. $this->SetX($header_x);
  208. $this->MultiCell(0, $cell_height, $headerdata['string'], 0, '', 0, 1, '', '', true, 0, false);
  209. // print an ending header line
  210. $this->SetLineStyle(array('width' => 0.85 / $this->getScaleFactor(), 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)));
  211. $this->SetY((2.835 / $this->getScaleFactor()) + max($imgy, $this->GetY()));
  212. if ($this->getRTL()) {
  213. $this->SetX($ormargins['right']);
  214. } else {
  215. $this->SetX($ormargins['left']);
  216. }
  217. $this->Cell(0, 0, '', 'T', 0, 'C');
  218. }
  219. /**
  220. * [OVERRIDE] SetFont method in TCPDF Library
  221. * This method override the regular SetFont() method to enable the custom font directory in addition to the OOB font directory.
  222. *
  223. * @param string $family Family font. It can be either a name defined by AddFont() or one of the standard Type1 families (case insensitive):<ul><li>times (Times-Roman)</li><li>timesb (Times-Bold)</li><li>timesi (Times-Italic)</li><li>timesbi (Times-BoldItalic)</li><li>helvetica (Helvetica)</li><li>helveticab (Helvetica-Bold)</li><li>helveticai (Helvetica-Oblique)</li><li>helveticabi (Helvetica-BoldOblique)</li><li>courier (Courier)</li><li>courierb (Courier-Bold)</li><li>courieri (Courier-Oblique)</li><li>courierbi (Courier-BoldOblique)</li><li>symbol (Symbol)</li><li>zapfdingbats (ZapfDingbats)</li></ul> It is also possible to pass an empty string. In that case, the current family is retained.
  224. * @param string $style Font style. Possible values are (case insensitive):<ul><li>empty string: regular</li><li>B: bold</li><li>I: italic</li><li>U: underline</li><li>D: line trough</li></ul> or any combination. The default value is regular. Bold and italic styles do not apply to Symbol and ZapfDingbats basic fonts or other fonts when not defined.
  225. * @param float $size Font size in points. The default value is the current size. If no size has been specified since the beginning of the document, the value taken is 12
  226. * @param string $fontfile The font definition file. By default, the name is built from the family and style, in lower case with no spaces.
  227. * @access public
  228. * @see include/tcpdf/TCPDF#SetFont()
  229. */
  230. public function SetFont($family, $style='', $size=0, $fontfile='') {
  231. if(empty($fontfile) && defined('K_PATH_CUSTOM_FONTS')){
  232. // This will force addFont to search the custom directory for font before the OOB directory
  233. $fontfile = K_PATH_CUSTOM_FONTS."phantomFile.phantom";
  234. }
  235. parent::SetFont($family, $style, $size, $fontfile);
  236. }
  237. function Info(){
  238. $this->SetFont(PDF_FONT_NAME_MAIN,'',12);
  239. $this->MultiCell(0,0,'---',0,'L');
  240. $this->MultiCell(0,0,'Class: '.get_class($this),0,'L');
  241. $this->MultiCell(0,0,'Extends: '.get_parent_class($this),0,'L');
  242. $this->MultiCell(0,0,'---',0,'L');
  243. $this->MultiCell(0,0,'Module: '.$this->module,0,'L');
  244. $this->MultiCell(0,0,'Tcpdf Action: '.$this->action,0,'L');
  245. $this->MultiCell(0,0,'Bean ID: '.$this->bean->getFieldValue('id'),0,'L');
  246. $this->SetFont(PDF_FONT_NAME_MAIN,'',12);
  247. $this->MultiCell(0,0,'---',0,'L');
  248. }
  249. /**
  250. * [OVERRIDE] Cell method in tcpdf library.
  251. * Handle charset conversion and HTML entity decode.
  252. * This method override the regular Cell() method to apply the prepare_string() function to
  253. * the string to print in the PDF.
  254. * The cell method is used by all the methods which print text (Write, MultiCell).
  255. * @see include/tcpdf/TCPDF#Cell()
  256. */
  257. public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=0, $link='', $stretch=0) {
  258. parent::Cell($w, $h, prepare_string($txt), $border, $ln, $align, $fill, $link, $stretch);
  259. }
  260. /**
  261. * This Ln1() method will always print a line break of one line height.
  262. * The regular Ln() method print a line break which has the height of the last printed cell.
  263. */
  264. public function Ln1() {
  265. parent::Ln($this->FontSize * $this->cell_height_ratio + 2 * $this->cMargin, false);
  266. }
  267. /**
  268. * This method allow printing a table using the MultiCell method with a formatted options array in parameter
  269. * Options :
  270. * header options override the regular options for the header's cells - $options['header']
  271. * cell options override the regular options for the specific cell - Array[line number (0 to x)][cell header]['options']
  272. * @param $item Array[line number (0 to x)][cell header] = Cell content OR
  273. * Array[line number (0 to x)][cell header]['value'] = Cell content AND
  274. * Array[line number (0 to x)][cell header]['options'] = Array[cell properties] = values
  275. * @param $options Array which can contain : width (array 'column name'=>'width value + % OR nothing'), isheader (bool), header (array), fill (string: HTML color), ishtml (bool) default: false, border (0: no border (defaul), 1: frame or all of the following characters: L ,T ,R ,B), align (L: left align, C: center, R: right align, J: justification)
  276. * @see MultiCell()
  277. */
  278. public function writeCellTable($item, $options=NULL){
  279. // Save initial font values
  280. $fontFamily = $this->getFontFamily();
  281. $fontSize = $this->getFontSizePt();
  282. $fontStyle = $this->getFontStyle();
  283. $this->SetTextColor(0, 0, 0);
  284. $options = $this->initOptionsForWriteCellTable($options, $item);
  285. // HEADER
  286. if(!isset($options['isheader']) || $options['isheader'] == true){
  287. $headerOptions = $options;
  288. if(!empty($options['header']) && is_array($options['header'])){
  289. $headerOptions = $this->initOptionsForWriteCellTable($options['header'], $item);
  290. }
  291. foreach($item[0] as $k => $v){
  292. $header[$k]=$k;
  293. }
  294. $h = $this->getLineHeightFromArray($header, $options["width"]);
  295. foreach($header as $v){
  296. $this->MultiCell($options["width"][$v],$h,$v,$headerOptions['border'],$headerOptions['align'],$headerOptions['fillstate'],0,'','',true,0,$headerOptions['ishtml']);
  297. }
  298. $this->SetFillColorArray($this->convertHTMLColorToDec($options['fill']));
  299. $this->Ln();
  300. }
  301. // MAIN
  302. // default font
  303. $this->SetFont($fontFamily,$fontStyle,$fontSize);
  304. $this->SetTextColor(0, 0, 0);
  305. $even=true;
  306. // LINES
  307. foreach($item as $k=>$line){
  308. $even=!$even;
  309. $h = $this->getLineHeightFromArray($line, $options["width"]);
  310. //CELLS
  311. foreach($line as $kk=>$cell){
  312. $cellOptions = $options;
  313. $value = $cell;
  314. if(is_array($cell)){
  315. $value = $cell['value'];
  316. if(!empty($cell['options']) && is_array($cell['options'])){
  317. $cellOptions = $this->initOptionsForWriteCellTable($cell['options'], $item);
  318. }
  319. }
  320. if($even && !empty($options['evencolor'])){
  321. $this->SetFillColorArray($this->convertHTMLColorToDec($options['evencolor']));
  322. $cellOptions['fillstate']=1;
  323. }else if(!$even && !empty($options['oddcolor'])){
  324. $this->SetFillColorArray($this->convertHTMLColorToDec($options['oddcolor']));
  325. $cellOptions['fillstate']=1;
  326. }
  327. $this->MultiCell($options["width"][$kk],$h,$value,$cellOptions['border'],$cellOptions['align'],$cellOptions['fillstate'],0,'','',true,0,$cellOptions['ishtml']);
  328. $this->SetFillColorArray($this->convertHTMLColorToDec($options['fill']));
  329. }
  330. $this->Ln();
  331. }
  332. $this->SetFont($fontFamily,$fontStyle,$fontSize);
  333. $this->SetTextColor(0, 0, 0);
  334. }
  335. /**
  336. * This method allow printing a table using the writeHTML method with a formatted array in parameter
  337. * This method can also return the table as HTML code
  338. * @param $item Array[line number (0 to x)][cell header] = Cell content OR
  339. * Array[line number (0 to x)][cell header]['value'] = Cell content AND
  340. * Array[line number (0 to x)][cell header]['options'] = Array[cell properties] = values
  341. * @param $returnHtml (bool) Return the table as HTML code instead of printing the HTML table
  342. * @param $options Array which can contain : table (array of "HTML proprty"=>"value"),td (array of "HTML proprty"=>"value"), tr (array of "HTML proprty"=>"value"), isheader(bool), header (array of "HTML proprty"=>"value"), width (array 'column name'=>'width value + unit OR nothing')
  343. * @return the HTML code if $returnHtml set to true
  344. */
  345. public function writeHTMLTable($item, $returnHtml=false, $options=NULL){
  346. //TODO ISSUE - width in % for the td have to be multiply by the number of column.
  347. // ex: for a width of 20% in a table of 6 columns the width will have to be 120% (20*6).
  348. $html="";
  349. $line="";
  350. if(!empty($options)){
  351. foreach($options as $k=>$v){
  352. $tmp[strtolower($k)]=$v;
  353. }
  354. $options=$tmp;
  355. }else{
  356. $options=array();
  357. }
  358. if(!isset($options["isheader"]) || $options["isheader"] == true){
  359. if(!empty($options["header"])){
  360. foreach($options["header"] as $k=>$v){
  361. $tmp[strtolower($k)]=$v;
  362. }
  363. $options["header"]=$tmp;
  364. }else{
  365. $options["header"]=array("tr"=>array("bgcolor"=>"#DCDCDC"),"td"=>array());
  366. }
  367. foreach($item[0] as $k => $v){
  368. if(!empty($options["width"]))$options["header"]["td"]["width"]=$options["width"][$k];
  369. $line.=$this->wrap("td", $k, $options["header"]);
  370. }
  371. $html.=$this->wrap("tr", $line, $options["header"]);
  372. }
  373. foreach ($item as $k=>$v){
  374. $line="";
  375. foreach($v as $kk => $vv){
  376. if(!empty($options["width"]) && isset($options["width"][$kk]))$options["td"]["width"]=$options["width"][$kk];
  377. $line.=$this->wrap("td", $vv, $options);
  378. }
  379. $html.=$this->wrap("tr", $line, $options);
  380. }
  381. $html=$this->wrap("table", $html, $options);
  382. if($returnHtml){
  383. return $html;
  384. }else{
  385. $this->writeHTML($html);
  386. }
  387. }
  388. /**
  389. * return the HTML code of the value wrap with the tag $tag. This method handle options (general and specific)
  390. * @param $tag
  391. * @param $value
  392. * @param $options
  393. * @return the HTML wrapped code
  394. */
  395. private function wrap($tag, $value, $options){
  396. if(empty($options[$tag])){
  397. $options[$tag] = array();
  398. }
  399. if(is_array($value)){
  400. if(isset($value["options"])){
  401. // The options of a specific entity overwrite the general options
  402. $options[$tag] = $value["options"];
  403. }
  404. if(isset($value["value"])){
  405. $value = $value["value"];
  406. }else{
  407. $value = "";
  408. }
  409. }
  410. return wrapTag($tag, $value, $options[$tag]);
  411. }
  412. /**
  413. * Return the heigth of a line depending of the width, the font and the content
  414. * @param $line Array containing the data of all the cells of the line
  415. * @param $width Array containing the width of all the cells of the line
  416. * @return The heigth of the line
  417. */
  418. private function getLineHeightFromArray($line, $width){
  419. $h=0;
  420. foreach($line as $kk=>$cell){
  421. $cellValue = $cell;
  422. if(is_array($cellValue)){
  423. $tmp = $cellValue['value'];
  424. $cellValue = $tmp;
  425. }
  426. if($h<$this->getNumLines($cellValue, $width[$kk])){
  427. $h=$this->getNumLines($cellValue, $width[$kk]);
  428. }
  429. }
  430. return $h * $this->FontSize * $this->cell_height_ratio + 2 * $this->cMargin;
  431. }
  432. /**
  433. * Private method for writeCellTable which format and initialize the options array.
  434. * @param $options array
  435. * @param $item array
  436. * @return $options array
  437. */
  438. private function initOptionsForWriteCellTable($options, $item){
  439. if(!empty($options)){
  440. foreach($options as $k=>$v){
  441. $tmp[strtolower($k)]=$v;
  442. }
  443. $options=$tmp;
  444. }else{
  445. $options=array();
  446. }
  447. // set to default if empty
  448. if(empty($options["width"]) || !is_array($options["width"])){
  449. $colNum = count($item[0]);
  450. $defaultWidth = $this->getRemainingWidth()/$colNum;
  451. foreach($item[0] as $k => $v){
  452. $options["width"][$k]=$defaultWidth;
  453. }
  454. }else{
  455. foreach($options["width"] as $k => $v){
  456. $options["width"][$k] = $this->getHTMLUnitToUnits($v, $this->getRemainingWidth());
  457. }
  458. }
  459. if(empty($options["border"])){
  460. $options["border"]=0;
  461. }
  462. if(empty($options["align"])){
  463. $options["align"]="L";
  464. }
  465. if(empty($options['ishtml'])){
  466. $options['ishtml'] = false;
  467. }
  468. if(empty($options['border'])){
  469. $options['border'] = 0;
  470. }
  471. if(!empty($options['fill'])){
  472. $this->SetFillColorArray($this->convertHTMLColorToDec($options['fill']));
  473. $options['fillstate']=1;
  474. }else{
  475. $options['fill']="#FFFFFF";//white
  476. $options['fillstate']=0;
  477. }
  478. if(!empty($options['fontfamily'])){
  479. $fontFamily = $options['fontfamily'];
  480. }else{
  481. $fontFamily = $this->getFontFamily();
  482. }
  483. if(!empty($options['fontsize'])){
  484. $fontSize = $options['fontsize'];
  485. }else{
  486. $fontSize = $this->getFontSizePt();
  487. }
  488. if(!empty($options['fontstyle'])){
  489. $fontStyle = $options['fontstyle'];
  490. }else{
  491. $fontStyle = $this->getFontStyle();
  492. }
  493. if(!empty($options['textcolor'])){
  494. $this->SetTextColorArray($this->convertHTMLColorToDec($options['textcolor']));
  495. }else{
  496. $this->SetTextColor(0, 0, 0);//black
  497. }
  498. $this->SetFont($fontFamily, $fontStyle, $fontSize);
  499. return $options;
  500. }
  501. /**
  502. * This is method is fix for a better handling of the count. This method now handle the line break
  503. * between words.
  504. * This method returns the estimated number of lines required to print the text.
  505. * @param string $txt text to print
  506. * @param float $w width of cell. If 0, they extend up to the right margin of the page.
  507. * @return int Return the estimated number of lines.
  508. * @access public
  509. * @since 4.5.011
  510. * @OVERRIDE
  511. */
  512. public function getNumLines($txt, $w=0) {
  513. $lines = 0;
  514. if (empty($w) OR ($w <= 0)) {
  515. if ($this->rtl) {
  516. $w = $this->x - $this->lMargin;
  517. } else {
  518. $w = $this->w - $this->rMargin - $this->x;
  519. }
  520. }
  521. // max column width
  522. $wmax = $w - (2 * $this->cMargin);
  523. // remove carriage returns
  524. $txt = str_replace("\r", '', $txt);
  525. // divide text in blocks
  526. $txtblocks = explode("\n", $txt);
  527. // for each block;
  528. foreach ($txtblocks as $block) {
  529. // estimate the number of lines
  530. if(empty($block)){
  531. $lines++;
  532. // If the block is in more than one line
  533. }else if(ceil($this->GetStringWidth($block) / $wmax)>1){
  534. //devide in words
  535. $words = explode(" ", $block);
  536. //TODO explode with space is not the best things to do...
  537. $wordBlock = "";
  538. $first=true;
  539. $lastNum = 0;
  540. $run = false;
  541. for($i=0; $i<count($words); $i++){
  542. if($first){
  543. $wordBlock = $words[$i];
  544. }else{
  545. $wordBlock .= " ".$words[$i];
  546. }
  547. if(ceil($this->GetStringWidth($wordBlock) / $wmax)>1){
  548. if($first){
  549. $lastNum = ceil($this->GetStringWidth($wordBlock) / $wmax);
  550. $run = true;
  551. $first = false;
  552. }else{
  553. if($run && $lastNum == ceil($this->GetStringWidth($wordBlock) / $wmax)){
  554. // save the number of line if it is the last loop
  555. if($i+1 == count($words)){
  556. $lines += ceil($this->GetStringWidth($wordBlock) / $wmax);
  557. }
  558. continue;
  559. }else{
  560. $first = true;
  561. $lines += ceil($this->GetStringWidth( substr($wordBlock, 0, (strlen($wordBlock) - strlen(" ".$words[$i]))) ) / $wmax);
  562. $i--;
  563. $lastNum = 0;
  564. $run = false;
  565. }
  566. }
  567. }else{
  568. $first = false;
  569. }
  570. // save the number of line if it is the last loop
  571. if($i+1 == count($words)){
  572. $lines += ceil($this->GetStringWidth($wordBlock) / $wmax);
  573. }
  574. }
  575. }else{
  576. $lines++;
  577. }
  578. }
  579. return $lines;
  580. }
  581. /**
  582. * Disable zlib output compression if we are downloading the PDF.
  583. *
  584. * @see TCPDF::Output()
  585. */
  586. public function Output($name='doc.pdf', $dest='I')
  587. {
  588. if ( $dest == 'I' || $dest == 'D') {
  589. ini_set('zlib.output_compression', 'Off');
  590. }
  591. return parent::Output($name,$dest);
  592. }
  593. }