2
0
forked from Wavyzz/dolibarr

Merge pull request #21594 from ATM-Consulting/Fix_15.0_pdf_sponge_with_too_many_situation

Fix 15.0 pdf sponge with too many situation
This commit is contained in:
Laurent Destailleur
2022-08-03 18:48:33 +02:00
committed by GitHub

View File

@@ -115,6 +115,32 @@ class pdf_sponge extends ModelePDFFactures
*/
public $marge_basse;
/**
* @var int heightforinfotot
*/
public $heightforinfotot;
/**
* @var int heightforfreetext
*/
public $heightforfreetext;
/**
* @var int heightforfooter
*/
public $heightforfooter;
/**
* @var int tab_top
*/
public $tab_top;
/**
* @var int tab_top_newpage
*/
public $tab_top_newpage;
/**
* Issuer
* @var Societe Object that emits
@@ -345,9 +371,9 @@ class pdf_sponge extends ModelePDFFactures
$default_font_size = pdf_getPDFFontSize($outputlangs); // Must be after pdf_getInstance
$pdf->SetAutoPageBreak(1, 0);
$heightforinfotot = 50 + (4 * $nbpayments); // Height reserved to output the info and total part and payment part
$heightforfreetext = (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT) ? $conf->global->MAIN_PDF_FREETEXT_HEIGHT : 5); // Height reserved to output the free text on last page
$heightforfooter = $this->marge_basse + (empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS) ? 12 : 22); // Height reserved to output the footer (value include bottom margin)
$this->heightforinfotot = 50 + (4 * $nbpayments); // Height reserved to output the info and total part and payment part
$this->heightforfreetext = (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT) ? $conf->global->MAIN_PDF_FREETEXT_HEIGHT : 5); // Height reserved to output the free text on last page
$this->heightforfooter = $this->marge_basse + (empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS) ? 12 : 22); // Height reserved to output the footer (value include bottom margin)
if (class_exists('TCPDF')) {
$pdf->setPrintHeader(false);
@@ -429,9 +455,9 @@ class pdf_sponge extends ModelePDFFactures
// $pdf->GetY() here can't be used. It is bottom of the second addresse box but first one may be higher
// $tab_top is y where we must continue content (90 = 42 + 48: 42 is height of logo and ref, 48 is address blocks)
$tab_top = 90 + $top_shift; // top_shift is an addition for linked objects or addons (0 in most cases)
$tab_top_newpage = (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD) ? 42 + $top_shift : 10);
// $this->tab_top is y where we must continue content (90 = 42 + 48: 42 is height of logo and ref, 48 is address blocks)
$this->tab_top = 90 + $top_shift; // top_shift is an addition for linked objects or addons (0 in most cases)
$this->tab_top_newpage = (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD) ? 42 + $top_shift : 10);
// You can add more thing under header here, if you increase $extra_under_address_shift too.
$extra_under_address_shift = 0;
@@ -452,7 +478,7 @@ class pdf_sponge extends ModelePDFFactures
'module_width' => 1, // width of a single module in points
'module_height' => 1 // height of a single module in points
);
$pdf->write2DBarcode($qrcodestring, 'QRCODE,M', $this->marge_gauche, $tab_top - 5, 25, 25, $styleQr, 'N');
$pdf->write2DBarcode($qrcodestring, 'QRCODE,M', $this->marge_gauche, $this->tab_top - 5, 25, 25, $styleQr, 'N');
$extra_under_address_shift += 25;
}
@@ -469,32 +495,32 @@ class pdf_sponge extends ModelePDFFactures
$extra_under_address_shift += $hookmanager->resArray['extra_under_header_shift'];
}
$tab_top += $extra_under_address_shift;
$tab_top_newpage += 0;
$this->tab_top += $extra_under_address_shift;
$this->tab_top_newpage += 0;
// Define heigth of table for lines (for first page)
$tab_height = $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext;
$tab_height = $this->page_hauteur - $this->tab_top - $this->heightforfooter - $this->heightforfreetext;
$nexY = $tab_top - 1;
$nexY = $this->tab_top - 1;
// Incoterm
$height_incoterms = 0;
if (!empty($conf->incoterm->enabled)) {
$desc_incoterms = $object->getIncotermsForPDF();
if ($desc_incoterms) {
$tab_top -= 2;
$this->tab_top -= 2;
$pdf->SetFont('', '', $default_font_size - 1);
$pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1, dol_htmlentitiesbr($desc_incoterms), 0, 1);
$pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $this->tab_top - 1, dol_htmlentitiesbr($desc_incoterms), 0, 1);
$nexY = max($pdf->GetY(), $nexY);
$height_incoterms = $nexY - $tab_top;
$height_incoterms = $nexY - $this->tab_top;
// Rect takes a length in 3rd parameter
$pdf->SetDrawColor(192, 192, 192);
$pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_incoterms + 1);
$pdf->Rect($this->marge_gauche, $this->tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_incoterms + 1);
$tab_top = $nexY + 6;
$this->tab_top = $nexY + 6;
$height_incoterms += 4;
}
}
@@ -521,7 +547,7 @@ class pdf_sponge extends ModelePDFFactures
$pagenb = $pdf->getPage();
if ($notetoshow) {
$tab_top -= 2;
$this->tab_top -= 2;
$tab_width = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
$pageposbeforenote = $pagenb;
@@ -534,7 +560,7 @@ class pdf_sponge extends ModelePDFFactures
$pdf->startTransaction();
$pdf->SetFont('', '', $default_font_size - 1);
$pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top, dol_htmlentitiesbr($notetoshow), 0, 1);
$pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $this->tab_top, dol_htmlentitiesbr($notetoshow), 0, 1);
// Description
$pageposafternote = $pdf->getPage();
$posyafter = $pdf->GetY();
@@ -553,29 +579,29 @@ class pdf_sponge extends ModelePDFFactures
$this->_pagehead($pdf, $object, 0, $outputlangs, $outputlangsbis);
}
// $this->_pagefoot($pdf,$object,$outputlangs,1);
$pdf->setTopMargin($tab_top_newpage);
$pdf->setTopMargin($this->tab_top_newpage);
// The only function to edit the bottom margin of current page to set it.
$pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext);
$pdf->setPageOrientation('', 1, $this->heightforfooter + $this->heightforfreetext);
}
// back to start
$pdf->setPage($pageposbeforenote);
$pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext);
$pdf->setPageOrientation('', 1, $this->heightforfooter + $this->heightforfreetext);
$pdf->SetFont('', '', $default_font_size - 1);
$pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top, dol_htmlentitiesbr($notetoshow), 0, 1);
$pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $this->tab_top, dol_htmlentitiesbr($notetoshow), 0, 1);
$pageposafternote = $pdf->getPage();
$posyafter = $pdf->GetY();
if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) { // There is no space left for total+free text
if ($posyafter > ($this->page_hauteur - ($this->heightforfooter + $this->heightforfreetext + 20))) { // There is no space left for total+free text
$pdf->AddPage('', '', true);
$pagenb++;
$pageposafternote++;
$pdf->setPage($pageposafternote);
$pdf->setTopMargin($tab_top_newpage);
$pdf->setTopMargin($this->tab_top_newpage);
// The only function to edit the bottom margin of current page to set it.
$pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext);
//$posyafter = $tab_top_newpage;
$pdf->setPageOrientation('', 1, $this->heightforfooter + $this->heightforfreetext);
//$posyafter = $this->tab_top_newpage;
}
@@ -588,11 +614,11 @@ class pdf_sponge extends ModelePDFFactures
$pdf->SetDrawColor(128, 128, 128);
// Draw note frame
if ($i > $pageposbeforenote) {
$height_note = $this->page_hauteur - ($tab_top_newpage + $heightforfooter);
$pdf->Rect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1);
$height_note = $this->page_hauteur - ($this->tab_top_newpage + $this->heightforfooter);
$pdf->Rect($this->marge_gauche, $this->tab_top_newpage - 1, $tab_width, $height_note + 1);
} else {
$height_note = $this->page_hauteur - ($tab_top + $heightforfooter);
$pdf->Rect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1);
$height_note = $this->page_hauteur - ($this->tab_top + $this->heightforfooter);
$pdf->Rect($this->marge_gauche, $this->tab_top - 1, $tab_width, $height_note + 1);
}
// Add footer
@@ -610,17 +636,17 @@ class pdf_sponge extends ModelePDFFactures
if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
$this->_pagehead($pdf, $object, 0, $outputlangs, $outputlangsbis);
}
$height_note = $posyafter - $tab_top_newpage;
$pdf->Rect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1);
$height_note = $posyafter - $this->tab_top_newpage;
$pdf->Rect($this->marge_gauche, $this->tab_top_newpage - 1, $tab_width, $height_note + 1);
} else {
// No pagebreak
$pdf->commitTransaction();
$posyafter = $pdf->GetY();
$height_note = $posyafter - $tab_top;
$pdf->Rect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1);
$height_note = $posyafter - $this->tab_top;
$pdf->Rect($this->marge_gauche, $this->tab_top - 1, $tab_width, $height_note + 1);
if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
if ($posyafter > ($this->page_hauteur - ($this->heightforfooter + $this->heightforfreetext + 20))) {
// not enough space, need to add page
$pdf->AddPage('', '', true);
$pagenb++;
@@ -633,12 +659,12 @@ class pdf_sponge extends ModelePDFFactures
$this->_pagehead($pdf, $object, 0, $outputlangs, $outputlangsbis);
}
$posyafter = $tab_top_newpage;
$posyafter = $this->tab_top_newpage;
}
}
$tab_height = $tab_height - $height_note;
$tab_top = $posyafter + 6;
$this->tab_top = $posyafter + 6;
} else {
$height_note = 0;
}
@@ -648,10 +674,10 @@ class pdf_sponge extends ModelePDFFactures
// Table simulation to know the height of the title line (this set this->tableTitleHeight)
$pdf->startTransaction();
$this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop);
$this->pdfTabTitles($pdf, $this->tab_top, $tab_height, $outputlangs, $hidetop);
$pdf->rollbackTransaction(true);
$nexY = $tab_top + $this->tabTitleHeight;
$nexY = $this->tab_top + $this->tabTitleHeight;
// Loop on each lines
$pageposbeforeprintlines = $pdf->getPage();
@@ -667,8 +693,8 @@ class pdf_sponge extends ModelePDFFactures
$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.
$pdf->setTopMargin($this->tab_top_newpage);
$pdf->setPageOrientation('', 1, $this->heightforfooter + $this->heightforfreetext + $this->heightforinfotot); // The only function to edit the bottom margin of current page to set it.
$pageposbefore = $pdf->getPage();
$showpricebeforepagebreak = 1;
@@ -677,14 +703,14 @@ class pdf_sponge extends ModelePDFFactures
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
if (isset($imglinesize['width']) && isset($imglinesize['height']) && ($curY + $imglinesize['height']) > ($this->page_hauteur - ($this->heightforfooter + $this->heightforfreetext + $this->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;
$curY = $this->tab_top_newpage;
// Allows data in the first page if description is long enough to break in multiples pages
if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) {
@@ -711,14 +737,14 @@ class pdf_sponge extends ModelePDFFactures
if ($pageposafter > $pageposbefore) { // There is a pagebreak
$pdf->rollbackTransaction(true);
$pageposafter = $pageposbefore;
$pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it.
$pdf->setPageOrientation('', 1, $this->heightforfooter); // The only function to edit the bottom margin of current page to set it.
$this->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc);
$pageposafter = $pdf->getPage();
$posyafter = $pdf->GetY();
//var_dump($posyafter); var_dump(($this->page_hauteur - ($heightforfooter+$heightforfreetext+$heightforinfotot))); exit;
if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) { // There is no space left for total+free text
//var_dump($posyafter); var_dump(($this->page_hauteur - ($this->heightforfooter+$this->heightforfreetext+$this->heightforinfotot))); exit;
if ($posyafter > ($this->page_hauteur - ($this->heightforfooter + $this->heightforfreetext + $this->heightforinfotot))) { // There is no space left for total+free text
if ($i == ($nblines - 1)) { // No more lines, and no space left to show total, so we create a new page
$pdf->AddPage('', '', true);
if (!empty($tplidx)) {
@@ -752,7 +778,7 @@ class pdf_sponge extends ModelePDFFactures
// We suppose that a too long description or photo were moved completely on next page
if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
$pdf->setPage($pageposafter);
$curY = $tab_top_newpage;
$curY = $this->tab_top_newpage;
}
$pdf->SetFont('', '', $default_font_size - 1); // We reposition the default font
@@ -923,9 +949,9 @@ class pdf_sponge extends ModelePDFFactures
while ($pagenb < $pageposafter) {
$pdf->setPage($pagenb);
if ($pagenb == $pageposbeforeprintlines) {
$this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code, $outputlangsbis);
$this->_tableau($pdf, $this->tab_top, $this->page_hauteur - $this->tab_top - $this->heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code, $outputlangsbis);
} else {
$this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code, $outputlangsbis);
$this->_tableau($pdf, $this->tab_top_newpage, $this->page_hauteur - $this->tab_top_newpage - $this->heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code, $outputlangsbis);
}
$this->_pagefoot($pdf, $object, $outputlangs, 1);
$pagenb++;
@@ -941,9 +967,9 @@ class pdf_sponge extends ModelePDFFactures
if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {
if ($pagenb == $pageposafter) {
$this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code, $outputlangsbis);
$this->_tableau($pdf, $this->tab_top, $this->page_hauteur - $this->tab_top - $this->heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code, $outputlangsbis);
} else {
$this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code, $outputlangsbis);
$this->_tableau($pdf, $this->tab_top_newpage, $this->page_hauteur - $this->tab_top_newpage - $this->heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code, $outputlangsbis);
}
$this->_pagefoot($pdf, $object, $outputlangs, 1);
// New page
@@ -960,11 +986,11 @@ 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, $hidetop, 0, $object->multicurrency_code, $outputlangsbis);
$bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
$this->_tableau($pdf, $this->tab_top, $this->page_hauteur - $this->tab_top - $this->heightforinfotot - $this->heightforfreetext - $this->heightforfooter, 0, $outputlangs, $hidetop, 0, $object->multicurrency_code, $outputlangsbis);
$bottomlasttab = $this->page_hauteur - $this->heightforinfotot - $this->heightforfreetext - $this->heightforfooter + 1;
} else {
$this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0, $object->multicurrency_code, $outputlangsbis);
$bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
$this->_tableau($pdf, $this->tab_top_newpage, $this->page_hauteur - $this->tab_top_newpage - $this->heightforinfotot - $this->heightforfreetext - $this->heightforfooter, 0, $outputlangs, 1, 0, $object->multicurrency_code, $outputlangsbis);
$bottomlasttab = $this->page_hauteur - $this->heightforinfotot - $this->heightforfreetext - $this->heightforfooter + 1;
}
// Display infos area
@@ -1432,10 +1458,16 @@ class pdf_sponge extends ModelePDFFactures
$posy = $pdf->GetY();
foreach ($TPreviousIncoice as &$fac) {
if ($posy > $this->page_hauteur - 4) {
if ($posy > $this->page_hauteur - 4 - $this->heightforfooter) {
$this->_pagefoot($pdf, $object, $outputlangs, 1);
$pdf->addPage();
$pdf->setY($this->marge_haute);
if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
$this->_pagehead($pdf, $object, 0, $outputlangs, $outputlangsbis);
$pdf->setY($this->tab_top_newpage);
}
else{
$pdf->setY($this->marge_haute);
}
$posy = $pdf->GetY();
}
@@ -1495,9 +1527,16 @@ class pdf_sponge extends ModelePDFFactures
$posy += $tab2_hl;
if ($posy > $this->page_hauteur - 4) {
if ($posy > $this->page_hauteur - 4 - $this->heightforfooter) {
$pdf->addPage();
$pdf->setY($this->marge_haute);
if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
$this->_pagehead($pdf, $object, 0, $outputlangs, $outputlangsbis);
$pdf->setY($this->tab_top_newpage);
}
else {
$pdf->setY($this->marge_haute);
}
$posy = $pdf->GetY();
}