From a668cc28b1aa77df5d103761a6901ebdb2faab82 Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Wed, 3 Apr 2019 16:19:49 +0200 Subject: [PATCH] Add table title auto height --- .../core/class/commondocgenerator.class.php | 50 ++++++ .../commande/doc/pdf_eratosthene.modules.php | 152 ++++++++++++++---- .../facture/doc/pdf_sponge.modules.php | 54 +++---- .../modules/propale/doc/pdf_cyan.modules.php | 50 +++--- 4 files changed, 215 insertions(+), 91 deletions(-) diff --git a/htdocs/core/class/commondocgenerator.class.php b/htdocs/core/class/commondocgenerator.class.php index 7b21dbea5b9..7c87f1e4e36 100644 --- a/htdocs/core/class/commondocgenerator.class.php +++ b/htdocs/core/class/commondocgenerator.class.php @@ -1069,4 +1069,54 @@ abstract class CommonDocGenerator } else return false; } + + /** + * print standard column content + * + * @param PDF $pdf pdf object + * @param float $tab_top tab top position + * @param float $tab_height default tab height + * @param $outputlangs + * @param int $hidetop + * @return float height of col tab titles + */ + function pdfTabTitles(&$pdf, $tab_top, $tab_height, $outputlangs, $hidetop = 0) + { + global $hookmanager; + + foreach ($this->cols as $colKey => $colDef) { + + $parameters = array( + 'colKey' => $colKey, + 'pdf' => $pdf, + 'outputlangs' => $outputlangs, + 'tab_top' => $tab_top, + 'tab_height' => $tab_height, + 'hidetop' => $hidetop + ); + + $reshook = $hookmanager->executeHooks('pdfTabTitles', $parameters, $this); // Note that $object may have been modified by hook + if ($reshook < 0) { + setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); + } elseif (empty($reshook)) { + if (!$this->getColumnStatus($colKey)) continue; + + // get title label + $colDef['title']['label'] = !empty($colDef['title']['label']) ? $colDef['title']['label'] : $outputlangs->transnoentities($colDef['title']['textkey']); + + // Add column separator + if (!empty($colDef['border-left'])) { + $pdf->line($colDef['xStartPos'], $tab_top, $colDef['xStartPos'], $tab_top + $tab_height); + } + + if (empty($hidetop)) { + $pdf->SetXY($colDef['xStartPos'] + $colDef['title']['padding'][3], $tab_top + $colDef['title']['padding'][0]); + $textWidth = $colDef['width'] - $colDef['title']['padding'][3] - $colDef['title']['padding'][1]; + $pdf->MultiCell($textWidth, 2, $colDef['title']['label'], '', $colDef['title']['align']); + $this->tabTitleHeight = max($pdf->GetY() - $tab_top + $colDef['title']['padding'][2], $this->tabTitleHeight); + } + } + } + return $this->tabTitleHeight; + } } diff --git a/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php b/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php index a9b82751ab3..16c4bf52d1c 100644 --- a/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php +++ b/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php @@ -170,6 +170,8 @@ class pdf_eratosthene extends ModelePDFCommandes $this->posxdesc=$this->marge_gauche+1; + $this->tabTitleHeight = 5; // default height + $this->tva=array(); $this->localtax1=array(); $this->localtax2=array(); @@ -203,6 +205,73 @@ class pdf_eratosthene extends ModelePDFCommandes $nblignes = count($object->lines); + $hidetop=0; + if(!empty($conf->global->MAIN_PDF_DISABLE_COL_HEAD_TITLE)){ + $hidetop=$conf->global->MAIN_PDF_DISABLE_COL_HEAD_TITLE; + } + + // Loop on each lines to detect if there is at least one image to show + $realpatharray=array(); + $this->atleastonephoto = false; + if (! empty($conf->global->MAIN_GENERATE_ORDERS_WITH_PICTURE)) + { + $objphoto = new Product($this->db); + + for ($i = 0 ; $i < $nblignes ; $i++) + { + if (empty($object->lines[$i]->fk_product)) continue; + + $objphoto->fetch($object->lines[$i]->fk_product); + //var_dump($objphoto->ref);exit; + if (! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) + { + $pdir[0] = get_exdir($objphoto->id,2,0,0,$objphoto,'product') . $objphoto->id ."/photos/"; + $pdir[1] = get_exdir(0,0,0,0,$objphoto,'product') . dol_sanitizeFileName($objphoto->ref).'/'; + } + else + { + $pdir[0] = get_exdir(0,0,0,0,$objphoto,'product') . dol_sanitizeFileName($objphoto->ref).'/'; // default + $pdir[1] = get_exdir($objphoto->id,2,0,0,$objphoto,'product') . $objphoto->id ."/photos/"; // alternative + } + + $arephoto = false; + foreach ($pdir as $midir) + { + if (! $arephoto) + { + $dir = $conf->product->dir_output.'/'.$midir; + + foreach ($objphoto->liste_photos($dir,1) as $key => $obj) + { + if (empty($conf->global->CAT_HIGH_QUALITY_IMAGES)) // If CAT_HIGH_QUALITY_IMAGES not defined, we use thumb if defined and then original photo + { + if ($obj['photo_vignette']) + { + $filename= $obj['photo_vignette']; + } + else + { + $filename=$obj['photo']; + } + } + else + { + $filename=$obj['photo']; + } + + $realpath = $dir.$filename; + $arephoto = true; + $this->atleastonephoto = true; + } + } + } + + if ($realpath && $arephoto) $realpatharray[$i]=$realpath; + } + } + + + if ($conf->commande->dir_output) { $object->fetch_thirdparty(); @@ -464,13 +533,19 @@ class pdf_eratosthene extends ModelePDFCommandes $height_note=0; } - $iniY = $tab_top + 7; - $curY = $tab_top + 7; - $nexY = $tab_top + 7; // Use new auto collum system $this->prepareArrayColumnField($object, $outputlangs, $hidedetails, $hidedesc, $hideref); + // tab simulation to know line height + $pdf->startTransaction(); + $this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop); + $pdf->rollbackTransaction(true); + + $iniY = $tab_top + $this->tabTitleHeight + 2; + $curY = $tab_top + $this->tabTitleHeight + 2; + $nexY = $tab_top + $this->tabTitleHeight + 2; + // Loop on each lines $pageposbeforeprintlines=$pdf->getPage(); $pagenb = $pageposbeforeprintlines; @@ -479,6 +554,10 @@ class pdf_eratosthene extends ModelePDFCommandes $curY = $nexY; $pdf->SetFont('', '', $default_font_size - 1); // Into loop to work with multipage $pdf->SetTextColor(0, 0, 0); + + // Define size of image if we need it + $imglinesize=array(); + if (! empty($realpatharray[$i])) $imglinesize=pdf_getSizeForImage($realpatharray[$i]); $pdf->setTopMargin($tab_top_newpage); $pdf->setPageOrientation('', 1, $heightforfooter+$heightforfreetext+$heightforinfotot); // The only function to edit the bottom margin of current page to set it. @@ -488,6 +567,29 @@ class pdf_eratosthene extends ModelePDFCommandes $curX = $this->posxdesc-1; $showpricebeforepagebreak=1; + $posYAfterImage=0; + $posYAfterDescription=0; + + if($this->getColumnStatus('photo')) + { + // We start with Photo of product line + if (isset($imglinesize['width']) && isset($imglinesize['height']) && ($curY + $imglinesize['height']) > ($this->page_hauteur-($heightforfooter+$heightforfreetext+$heightforinfotot))) // If photo too high, we moved completely on new page + { + $pdf->AddPage('','',true); + if (! empty($tplidx)) $pdf->useTemplate($tplidx); + $pdf->setPage($pageposbefore+1); + + $curY = $tab_top_newpage; + $showpricebeforepagebreak=0; + } + + if (!empty($this->cols['photo']) && isset($imglinesize['width']) && isset($imglinesize['height'])) + { + $pdf->Image($realpatharray[$i], $this->getColumnContentXStart('photo'), $curY, $imglinesize['width'], $imglinesize['height'], '', '', '', 2, 300); // Use 300 dpi + // $pdf->Image does not increase value return by getY, so we save it manually + $posYAfterImage=$curY+$imglinesize['height']; + } + } if($this->getColumnStatus('desc')) { @@ -526,7 +628,11 @@ class pdf_eratosthene extends ModelePDFCommandes $posYAfterDescription=$pdf->GetY(); } - $nexY = $pdf->GetY(); + + + $nexY = max($pdf->GetY(),$posYAfterImage); + + $pageposafter=$pdf->getPage(); $pdf->setPage($pageposbefore); @@ -657,7 +763,7 @@ class pdf_eratosthene extends ModelePDFCommandes $pdf->setPage($pagenb); if ($pagenb == $pageposbeforeprintlines) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code); } else { @@ -673,7 +779,7 @@ class pdf_eratosthene extends ModelePDFCommandes { if ($pagenb == $pageposafter) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code); } else { @@ -690,7 +796,7 @@ class pdf_eratosthene extends ModelePDFCommandes // Show square if ($pagenb == $pageposbeforeprintlines) - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, $hidetop, 0, $object->multicurrency_code); else $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0, $object->multicurrency_code); $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1; @@ -973,19 +1079,16 @@ class pdf_eratosthene extends ModelePDFCommandes $col2x-=20; } $largcol2 = ($this->page_largeur - $this->marge_droite - $col2x); - $useborder=0; $index = 0; - // Total HT $pdf->SetFillColor(255, 255, 255); $pdf->SetXY($col1x, $tab2_top + 0); $pdf->MultiCell($col2x-$col1x, $tab2_hl, $outputlangs->transnoentities("TotalHT"), 0, 'L', 1); - $total_ht = ($conf->multicurrency->enabled && $object->mylticurrency_tx != 1 ? $object->multicurrency_total_ht : $object->total_ht); $pdf->SetXY($col2x, $tab2_top + 0); $pdf->MultiCell($largcol2, $tab2_hl, price($total_ht + (! empty($object->remise)?$object->remise:0), 0, $outputlangs), 0, 'R', 1); - + // Show VAT by rates and total $pdf->SetFillColor(248, 248, 248); @@ -1250,30 +1353,13 @@ class pdf_eratosthene extends ModelePDFCommandes $this->printRect($pdf, $this->marge_gauche, $tab_top, $this->page_largeur-$this->marge_gauche-$this->marge_droite, $tab_height, $hidetop, $hidebottom); // Rect prend une longueur en 3eme param et 4eme param - foreach ($this->cols as $colKey => $colDef) - { - if(!$this->getColumnStatus($colKey)) continue; - - // get title label - $colDef['title']['label'] = !empty($colDef['title']['label'])?$colDef['title']['label']:$outputlangs->transnoentities($colDef['title']['textkey']); - - // Add column separator - if(!empty($colDef['border-left'])){ - $pdf->line($colDef['xStartPos'], $tab_top, $colDef['xStartPos'], $tab_top + $tab_height); - } - - if (empty($hidetop)) - { - $pdf->SetXY($colDef['xStartPos'] + $colDef['title']['padding'][3], $tab_top + $colDef['title']['padding'][0]); - - $textWidth = $colDef['width'] - $colDef['title']['padding'][3] -$colDef['title']['padding'][1]; - $pdf->MultiCell($textWidth, 2, $colDef['title']['label'], '', $colDef['title']['align']); - } - } - + $this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop); + if (empty($hidetop)){ - $pdf->line($this->marge_gauche, $tab_top+5, $this->page_largeur-$this->marge_droite, $tab_top+5); // line prend une position y en 2eme param et 4eme param + $pdf->line($this->marge_gauche, $tab_top+$this->tabTitleHeight, $this->page_largeur-$this->marge_droite, $tab_top+$this->tabTitleHeight); // line prend une position y en 2eme param et 4eme param } + + } /** diff --git a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php index 6cf6e39dc3f..4cdc4df2623 100644 --- a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php +++ b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php @@ -180,6 +180,9 @@ class pdf_sponge extends ModelePDFFactures // Define position of columns $this->posxdesc=$this->marge_gauche+1; // used for notes ans other stuff + + $this->tabTitleHeight = 5; // default height + // Use new system for position of columns, view $this->defineColumnField() $this->tva=array(); @@ -217,6 +220,11 @@ class pdf_sponge extends ModelePDFFactures $nblignes = count($object->lines); + $hidetop=0; + if(!empty($conf->global->MAIN_PDF_DISABLE_COL_HEAD_TITLE)){ + $hidetop=$conf->global->MAIN_PDF_DISABLE_COL_HEAD_TITLE; + } + // Loop on each lines to detect if there is at least one image to show $realpatharray=array(); $this->atleastonephoto = false; @@ -549,13 +557,18 @@ class pdf_sponge extends ModelePDFFactures $height_note=0; } - $iniY = $tab_top + 7; - $curY = $tab_top + 7; - $nexY = $tab_top + 7; - // Use new auto collum system $this->prepareArrayColumnField($object, $outputlangs, $hidedetails, $hidedesc, $hideref); + // Simulation de tableau pour connaitre la hauteur de la ligne de titre + $pdf->startTransaction(); + $this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop); + $pdf->rollbackTransaction(true); + + $iniY = $tab_top + $this->tabTitleHeight + 2; + $curY = $tab_top + $this->tabTitleHeight + 2; + $nexY = $tab_top + $this->tabTitleHeight + 2; + // Loop on each lines $pageposbeforeprintlines=$pdf->getPage(); $pagenb = $pageposbeforeprintlines; @@ -786,7 +799,7 @@ class pdf_sponge extends ModelePDFFactures $pdf->setPage($pagenb); if ($pagenb == $pageposbeforeprintlines) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code); } else { @@ -803,7 +816,7 @@ class pdf_sponge extends ModelePDFFactures { if ($pagenb == $pageposafter) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code); } else { @@ -821,7 +834,7 @@ class pdf_sponge extends ModelePDFFactures // Show square if ($pagenb == $pageposbeforeprintlines) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, $hidetop, 0, $object->multicurrency_code); $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1; } else @@ -1541,30 +1554,13 @@ class pdf_sponge extends ModelePDFFactures $this->printRect($pdf, $this->marge_gauche, $tab_top, $this->page_largeur-$this->marge_gauche-$this->marge_droite, $tab_height, $hidetop, $hidebottom); // Rect prend une longueur en 3eme param et 4eme param - foreach ($this->cols as $colKey => $colDef) - { - if(!$this->getColumnStatus($colKey)) continue; - - // get title label - $colDef['title']['label'] = !empty($colDef['title']['label'])?$colDef['title']['label']:$outputlangs->transnoentities($colDef['title']['textkey']); - - // Add column separator - if(!empty($colDef['border-left'])){ - $pdf->line($colDef['xStartPos'], $tab_top, $colDef['xStartPos'], $tab_top + $tab_height); - } - - if (empty($hidetop)) - { - $pdf->SetXY($colDef['xStartPos'] + $colDef['title']['padding'][3], $tab_top + $colDef['title']['padding'][0]); - - $textWidth = $colDef['width'] - $colDef['title']['padding'][3] -$colDef['title']['padding'][1]; - $pdf->MultiCell($textWidth, 2, $colDef['title']['label'], '', $colDef['title']['align']); - } - } - + $this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop); + if (empty($hidetop)){ - $pdf->line($this->marge_gauche, $tab_top+5, $this->page_largeur-$this->marge_droite, $tab_top+5); // line prend une position y en 2eme param et 4eme param + $pdf->line($this->marge_gauche, $tab_top+$this->tabTitleHeight, $this->page_largeur-$this->marge_droite, $tab_top+$this->tabTitleHeight); // line prend une position y en 2eme param et 4eme param } + + } /** diff --git a/htdocs/core/modules/propale/doc/pdf_cyan.modules.php b/htdocs/core/modules/propale/doc/pdf_cyan.modules.php index c25d1d9e3a1..58872a25b1e 100644 --- a/htdocs/core/modules/propale/doc/pdf_cyan.modules.php +++ b/htdocs/core/modules/propale/doc/pdf_cyan.modules.php @@ -169,6 +169,7 @@ class pdf_cyan extends ModelePDFPropales $this->posxdesc=$this->marge_gauche+1; + $this->tabTitleHeight = 5; // default height $this->tva=array(); $this->localtax1=array(); @@ -207,6 +208,11 @@ class pdf_cyan extends ModelePDFPropales $nblignes = count($object->lines); + $hidetop=0; + if(!empty($conf->global->MAIN_PDF_DISABLE_COL_HEAD_TITLE)){ + $hidetop=$conf->global->MAIN_PDF_DISABLE_COL_HEAD_TITLE; + } + // Loop on each lines to detect if there is at least one image to show $realpatharray=array(); $this->atleastonephoto = false; @@ -536,13 +542,18 @@ class pdf_cyan extends ModelePDFPropales $height_note=0; } - $iniY = $tab_top + 7; - $curY = $tab_top + 7; - $nexY = $tab_top + 7; - // Use new auto collum system $this->prepareArrayColumnField($object, $outputlangs, $hidedetails, $hidedesc, $hideref); + // Simulation de tableau pour connaitre la hauteur de la ligne de titre + $pdf->startTransaction(); + $this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop); + $pdf->rollbackTransaction(true); + + $iniY = $tab_top + $this->tabTitleHeight + 2; + $curY = $tab_top + $this->tabTitleHeight + 2; + $nexY = $tab_top + $this->tabTitleHeight + 2; + // Loop on each lines $pageposbeforeprintlines=$pdf->getPage(); $pagenb = $pageposbeforeprintlines; @@ -761,7 +772,7 @@ class pdf_cyan extends ModelePDFPropales $pdf->setPage($pagenb); if ($pagenb == $pageposbeforeprintlines) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code); } else { @@ -777,7 +788,7 @@ class pdf_cyan extends ModelePDFPropales { if ($pagenb == $pageposafter) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code); } else { @@ -795,7 +806,7 @@ class pdf_cyan extends ModelePDFPropales // Show square if ($pagenb == $pageposbeforeprintlines) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforsignature - $heightforfooter, 0, $outputlangs, 0, 0, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforsignature - $heightforfooter, 0, $outputlangs, $hidetop, 0, $object->multicurrency_code); $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforsignature - $heightforfooter + 1; } else @@ -1448,29 +1459,10 @@ class pdf_cyan extends ModelePDFPropales $this->printRect($pdf, $this->marge_gauche, $tab_top, $this->page_largeur-$this->marge_gauche-$this->marge_droite, $tab_height, $hidetop, $hidebottom); // Rect prend une longueur en 3eme param et 4eme param - foreach ($this->cols as $colKey => $colDef) - { - if(!$this->getColumnStatus($colKey)) continue; - - // get title label - $colDef['title']['label'] = !empty($colDef['title']['label'])?$colDef['title']['label']:$outputlangs->transnoentities($colDef['title']['textkey']); - - // Add column separator - if(!empty($colDef['border-left'])){ - $pdf->line($colDef['xStartPos'], $tab_top, $colDef['xStartPos'], $tab_top + $tab_height); - } - - if (empty($hidetop)) - { - $pdf->SetXY($colDef['xStartPos'] + $colDef['title']['padding'][3], $tab_top + $colDef['title']['padding'][0]); - - $textWidth = $colDef['width'] - $colDef['title']['padding'][3] -$colDef['title']['padding'][1]; - $pdf->MultiCell($textWidth, 2, $colDef['title']['label'], '', $colDef['title']['align']); - } - } - + $this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop); + if (empty($hidetop)){ - $pdf->line($this->marge_gauche, $tab_top+5, $this->page_largeur-$this->marge_droite, $tab_top+5); // line prend une position y en 2eme param et 4eme param + $pdf->line($this->marge_gauche, $tab_top+$this->tabTitleHeight, $this->page_largeur-$this->marge_droite, $tab_top+$this->tabTitleHeight); // line prend une position y en 2eme param et 4eme param } }