From 3e391b25213fb7496ca690381681922942b2aebf Mon Sep 17 00:00:00 2001 From: Charles Delachapelle Evarisk Date: Mon, 23 Feb 2026 11:02:52 +0100 Subject: [PATCH 1/2] #1296 [PDF/ODF] add : function initDocumentGenerator --- core/modules/saturne/modules_saturne.php | 118 +++++++++++++++-------- lib/medias.lib.php | 1 + 2 files changed, 80 insertions(+), 39 deletions(-) diff --git a/core/modules/saturne/modules_saturne.php b/core/modules/saturne/modules_saturne.php index 5869983f..5ab065e1 100644 --- a/core/modules/saturne/modules_saturne.php +++ b/core/modules/saturne/modules_saturne.php @@ -818,6 +818,84 @@ public function buildDocumentFilename($objectDocument, $outputLangs, $object, $m return $dir . '/' . $fileName; } + /** + * Init documents values for pdf and ODT + * + * @param Object $object Object of the page you are on. + * @param Translate $outputLangs Lang object to use for output. + * @param string $srcTemplatePath Path of the source template + * @param array $moreParam More param (Object/user/etc) + * @param string $file Path of the source template + * + * @return int|odf|TCPDF Return an -1 if there is an error or return a PDF or an ODF object if success + * @throws Exception + */ + public function initDocumentGenerator($object, $outputLangs, $srcTemplatePath,$moreParam, $file) + { + global $action, $conf, $hookmanager, $moduleNameLowerCase, $mysoc, $user; + + if ($this->type == 'odt') {// Make substitution + $substitutionArray = []; + complete_substitutions_array($substitutionArray, $outputLangs, $object); + // Call the ODTSubstitution hook + $parameters = ['file' => $file, 'object' => $object, 'outputlangs' => $outputLangs, 'substitutionarray' => &$substitutionArray]; + $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks + + // Open and load template + require_once ODTPHP_PATH . 'odf.php'; + try { + $odfHandler = new odf( + $srcTemplatePath, + [ + 'PATH_TO_TMP' => $conf->$moduleNameLowerCase->dir_temp, + 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy + 'DELIMITER_LEFT' => '{', + 'DELIMITER_RIGHT' => '}' + ] + ); + } catch (Exception $e) { + $this->error = $e->getMessage(); + dol_syslog($e->getMessage()); + return -1; + } + + // Define substitution array + $substitutionArray = getCommonSubstitutionArray($outputLangs, 0, null, $object); + $arrayObjectFromProperties = $this->get_substitutionarray_each_var_object($object, $outputLangs); + $arraySoc = $this->get_substitutionarray_mysoc($mysoc, $outputLangs); + $arraySoc['mycompany_logo'] = preg_replace('/_small/', '_mini', $arraySoc['mycompany_logo']); + + $tmpArray = array_merge($substitutionArray, $arrayObjectFromProperties, $arraySoc, $moreParam['tmparray']); + if (isModEnabled('multicompany')) { + $tmpArray['entity'] = $conf->entity; + } else { + $tmpArray['entity'] = ''; + } + + $this->fillTags($odfHandler, $outputLangs, $tmpArray, $moreParam); + return $odfHandler; + } + $pdf = pdf_getInstance($this->format); + $defaultFontSize = pdf_getPDFFontSize($outputLangs) + 2; + + if (class_exists('TCPDF')) { + $pdf->setPrintHeader(false); + $pdf->setPrintFooter(false); + } + + $pdf->SetFont(pdf_getPDFFont($outputLangs)); + $pdf->Open(); + $pdf->SetDrawColor(128, 128, 128); + $pdf->SetTitle($outputLangs->convToOutputCharset($this->document_type)); + $pdf->SetSubject($outputLangs->transnoentities($this->document_type)); + $pdf->SetCreator('Dolibarr ' . DOL_VERSION); + $pdf->SetAuthor($outputLangs->convToOutputCharset($user->getFullName($outputLangs))); + $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); + $pdf->setPageOrientation($this->orientation, 1, $this->marge_basse); + $pdf->SetAutoPageBreak(1, $this->marge_basse); + return $pdf; + } + /** * Function to build a document on disk * @@ -866,45 +944,7 @@ public function write_file(SaturneDocuments $objectDocument, Translate $outputLa return -1; } - // Make substitution - $substitutionArray = []; - complete_substitutions_array($substitutionArray, $outputLangs, $object); - // Call the ODTSubstitution hook - $parameters = ['file' => $file, 'object' => $object, 'outputlangs' => $outputLangs, 'substitutionarray' => &$substitutionArray]; - $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks - - // Open and load template - require_once ODTPHP_PATH . 'odf.php'; - try { - $odfHandler = new odf( - $srcTemplatePath, - [ - 'PATH_TO_TMP' => $conf->$moduleNameLowerCase->dir_temp, - 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy - 'DELIMITER_LEFT' => '{', - 'DELIMITER_RIGHT' => '}' - ] - ); - } catch (Exception $e) { - $this->error = $e->getMessage(); - dol_syslog($e->getMessage()); - return -1; - } - - // Define substitution array - $substitutionArray = getCommonSubstitutionArray($outputLangs, 0, null, $object); - $arrayObjectFromProperties = $this->get_substitutionarray_each_var_object($object, $outputLangs); - $arraySoc = $this->get_substitutionarray_mysoc($mysoc, $outputLangs); - $arraySoc['mycompany_logo'] = preg_replace('/_small/', '_mini', $arraySoc['mycompany_logo']); - - $tmpArray = array_merge($substitutionArray, $arrayObjectFromProperties, $arraySoc, $moreParam['tmparray']); - if (isModEnabled('multicompany')) { - $tmpArray['entity'] = $conf->entity; - } else { - $tmpArray['entity'] = ''; - } - - $this->fillTags($odfHandler, $outputLangs, $tmpArray, $moreParam); + $this->type == 'pdf' ? $this->initDocumentGenerator($object, $outputLangs, $srcTemplatePath, $moreParam, $file) : $odfHandler = $this->initDocumentGenerator($object, $outputLangs, $srcTemplatePath, $moreParam, $file); // Replace labels translated $tmpArray = $outputLangs->get_translations_for_substitutions(); diff --git a/lib/medias.lib.php b/lib/medias.lib.php index a904f2dd..83353fb7 100644 --- a/lib/medias.lib.php +++ b/lib/medias.lib.php @@ -382,6 +382,7 @@ function saturne_get_thumb_name(string $filename, string $thumbType = 'small', s $fileExtension = pathinfo($filename, PATHINFO_EXTENSION); if (!empty($filePath)) { + require_once __DIR__ . './dolibarr.lib.php'; $filePathThumb = $filePath . '/thumbs'; if (!dol_is_dir($filePathThumb)) { dol_mkdir($filePathThumb); From 543f1ca69b3898124e74eb807b57c2e0ca7988b2 Mon Sep 17 00:00:00 2001 From: Charles Delachapelle Evarisk Date: Wed, 25 Feb 2026 08:59:00 +0100 Subject: [PATCH 2/2] #1296 [PDF/ODF] add : function initDocumentGenerator --- core/modules/saturne/modules_saturne.php | 124 ++++++++++++++--------- 1 file changed, 78 insertions(+), 46 deletions(-) diff --git a/core/modules/saturne/modules_saturne.php b/core/modules/saturne/modules_saturne.php index 5ab065e1..3a7e343d 100644 --- a/core/modules/saturne/modules_saturne.php +++ b/core/modules/saturne/modules_saturne.php @@ -818,63 +818,56 @@ public function buildDocumentFilename($objectDocument, $outputLangs, $object, $m return $dir . '/' . $fileName; } + /** * Init documents values for pdf and ODT * * @param Object $object Object of the page you are on. * @param Translate $outputLangs Lang object to use for output. - * @param string $srcTemplatePath Path of the source template * @param array $moreParam More param (Object/user/etc) * @param string $file Path of the source template * - * @return int|odf|TCPDF Return an -1 if there is an error or return a PDF or an ODF object if success + * @return void * @throws Exception */ - public function initDocumentGenerator($object, $outputLangs, $srcTemplatePath,$moreParam, $file) + public function initOdtDocument($object, $outputLangs,$moreParam, $file) { - global $action, $conf, $hookmanager, $moduleNameLowerCase, $mysoc, $user; - - if ($this->type == 'odt') {// Make substitution - $substitutionArray = []; - complete_substitutions_array($substitutionArray, $outputLangs, $object); - // Call the ODTSubstitution hook - $parameters = ['file' => $file, 'object' => $object, 'outputlangs' => $outputLangs, 'substitutionarray' => &$substitutionArray]; - $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks - - // Open and load template - require_once ODTPHP_PATH . 'odf.php'; - try { - $odfHandler = new odf( - $srcTemplatePath, - [ - 'PATH_TO_TMP' => $conf->$moduleNameLowerCase->dir_temp, - 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy - 'DELIMITER_LEFT' => '{', - 'DELIMITER_RIGHT' => '}' - ] - ); - } catch (Exception $e) { - $this->error = $e->getMessage(); - dol_syslog($e->getMessage()); - return -1; - } - - // Define substitution array - $substitutionArray = getCommonSubstitutionArray($outputLangs, 0, null, $object); - $arrayObjectFromProperties = $this->get_substitutionarray_each_var_object($object, $outputLangs); - $arraySoc = $this->get_substitutionarray_mysoc($mysoc, $outputLangs); - $arraySoc['mycompany_logo'] = preg_replace('/_small/', '_mini', $arraySoc['mycompany_logo']); + global $action, $conf, $hookmanager, $mysoc; + $substitutionArray = []; + complete_substitutions_array($substitutionArray, $outputLangs, $object); + // Call the ODTSubstitution hook + $parameters = ['file' => $file, 'object' => $object, 'outputlangs' => $outputLangs, 'substitutionarray' => &$substitutionArray]; + $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks + + // Open and load template + require_once ODTPHP_PATH . 'odf.php'; + + // Define substitution array + $substitutionArray = getCommonSubstitutionArray($outputLangs, 0, null, $object); + $arrayObjectFromProperties = $this->get_substitutionarray_each_var_object($object, $outputLangs); + $arraySoc = $this->get_substitutionarray_mysoc($mysoc, $outputLangs); + $arraySoc['mycompany_logo'] = preg_replace('/_small/', '_mini', $arraySoc['mycompany_logo']); + + $tmpArray = array_merge($substitutionArray, $arrayObjectFromProperties, $arraySoc, $moreParam['tmparray']); + if (isModEnabled('multicompany')) { + $tmpArray['entity'] = $conf->entity; + } else { + $tmpArray['entity'] = ''; + } + } - $tmpArray = array_merge($substitutionArray, $arrayObjectFromProperties, $arraySoc, $moreParam['tmparray']); - if (isModEnabled('multicompany')) { - $tmpArray['entity'] = $conf->entity; - } else { - $tmpArray['entity'] = ''; - } + /** + * Init documents values for pdf + * + * @param Translate $outputLangs Lang object to use for output. + * + * @return void + * @throws Exception + */ + public function initPdfDocument($outputLangs) + { + global $user; - $this->fillTags($odfHandler, $outputLangs, $tmpArray, $moreParam); - return $odfHandler; - } $pdf = pdf_getInstance($this->format); $defaultFontSize = pdf_getPDFFontSize($outputLangs) + 2; @@ -893,7 +886,27 @@ public function initDocumentGenerator($object, $outputLangs, $srcTemplatePath,$m $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); $pdf->setPageOrientation($this->orientation, 1, $this->marge_basse); $pdf->SetAutoPageBreak(1, $this->marge_basse); - return $pdf; + } + + /** + * Init documents values for pdf and ODT + * + * @param Object $object Object of the page you are on. + * @param Translate $outputLangs Lang object to use for output. + * @param string $srcTemplatePath Path of the source template + * @param array $moreParam More param (Object/user/etc) + * @param string $file Path of the source template + * + * @return void + * @throws Exception + */ + public function initDocumentGenerator($object, $outputLangs,$moreParam, $file) + { + if ($this->type == 'odt') { + $this->initOdtDocument($object, $outputLangs,$moreParam, $file); + } else { + $this->initPdfDocument($outputLangs); + } } /** @@ -944,11 +957,30 @@ public function write_file(SaturneDocuments $objectDocument, Translate $outputLa return -1; } - $this->type == 'pdf' ? $this->initDocumentGenerator($object, $outputLangs, $srcTemplatePath, $moreParam, $file) : $odfHandler = $this->initDocumentGenerator($object, $outputLangs, $srcTemplatePath, $moreParam, $file); + $this->initDocumentGenerator($object, $outputLangs, $moreParam, $file); // Replace labels translated $tmpArray = $outputLangs->get_translations_for_substitutions(); + if ($this->type == 'odt') { + try { + $odfHandler = new odf( + $srcTemplatePath, + [ + 'PATH_TO_TMP' => $conf->$moduleNameLowerCase->dir_temp, + 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy + 'DELIMITER_LEFT' => '{', + 'DELIMITER_RIGHT' => '}' + ] + ); + } catch (Exception $e) { + $this->error = $e->getMessage(); + dol_syslog($e->getMessage()); + return -1; + } + } + + $this->fillTags($odfHandler, $outputLangs, $tmpArray, $moreParam); // Call the beforeODTSave hook $parameters = ['odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputLangs, 'substitutionarray' => &$tmpArray]; $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks