diff --git a/CHANGELOG.md b/CHANGELOG.md
index b4225c10..aa2ceff1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix errors when generating a pdf with software license/installation tabs
- Incomplete columns when exporting Users or Groups
+### Changed
+
+- Improve PDF generation performance (fewer SQL queries, faster rendering, session released during the export)
+
## [4.1.6] - 2026-10-06
### Fixed
diff --git a/front/export.massive.php b/front/export.massive.php
index 23024007..d44c593b 100644
--- a/front/export.massive.php
+++ b/front/export.massive.php
@@ -79,6 +79,9 @@
}
$itempdf = new $pdf_class($item);
+
+ Session::writeClose();
+
$itempdf->generatePDF($tab_id, $tab, ($pag ?? 0));
} else {
throw new RuntimeException('Missing PDF plugin hook for type: ' . $type);
diff --git a/front/export.php b/front/export.php
index dd044400..75e03ac8 100644
--- a/front/export.php
+++ b/front/export.php
@@ -74,6 +74,9 @@
}
$itempdf = new $pdf_class($item);
+
+ Session::writeClose();
+
$itempdf->generatePDF([$_POST['itemID']], $tab, ($_POST['page'] ?? 0));
} else {
throw new RuntimeException('Missing PDF plugin hook for type: ' . $type);
diff --git a/hook.php b/hook.php
index c6d1a75c..c4bcb660 100644
--- a/hook.php
+++ b/hook.php
@@ -39,6 +39,11 @@ function plugin_pdf_postinit()
foreach ($PLUGIN_HOOKS['plugin_pdf'] as $type => $typepdf) {
CommonGLPI::registerStandardTab($type, $typepdf);
}
+
+ $active_plugins = Plugin::getPlugins();
+ if (in_array('datainjection', $active_plugins, true) || in_array('geststock', $active_plugins, true)) {
+ $PLUGIN_HOOKS['menu_entry']['pdf'] = 'front/preference.form.php';
+ }
}
diff --git a/inc/changetask.class.php b/inc/changetask.class.php
index c438de71..f14169b6 100644
--- a/inc/changetask.class.php
+++ b/inc/changetask.class.php
@@ -44,8 +44,6 @@ public static function pdfForChange(PluginPdfSimplePDF $pdf, Change $job)
/** @var DBmysql $DB */
global $DB;
- $dbu = new DbUtils();
-
$ID = $job->getField('id');
$result = $DB->request([
@@ -101,17 +99,17 @@ public static function pdfForChange(PluginPdfSimplePDF $pdf, Change $job)
$planification .= '
' . sprintf(
__s('%1$s: %2$s'),
__s('By'),
- $dbu->getUserName($data['users_id_tech']),
+ PluginPdfCommon::getUserNameCached($data['users_id_tech']),
);
}
- $lib = $data['taskcategories_id'] ? Dropdown::getDropdownName('glpi_taskcategories', $data['taskcategories_id']) : '';
+ $lib = $data['taskcategories_id'] ? PluginPdfCommon::getDropdownNameCached('glpi_taskcategories', $data['taskcategories_id']) : '';
$pdf->displayLine(
'' . Toolbox::stripTags($lib),
Html::convDateTime($data['date']),
Html::timestampToString($data['actiontime'], false),
- Toolbox::stripTags($dbu->getUserName($data['users_id'])),
+ Toolbox::stripTags(PluginPdfCommon::getUserNameCached($data['users_id'])),
Toolbox::stripTags($planification),
1,
);
diff --git a/inc/changevalidation.class.php b/inc/changevalidation.class.php
index 2a8e7132..9cde8c65 100644
--- a/inc/changevalidation.class.php
+++ b/inc/changevalidation.class.php
@@ -44,8 +44,6 @@ public static function pdfForChange(PluginPdfSimplePDF $pdf, Change $change)
/** @var DBmysql $DB */
global $DB;
- $dbu = new DbUtils();
-
$pdf->setColumnsSize(100);
$pdf->displayTitle('' . __s('Approvals for the change', 'pdf') . '');
@@ -91,10 +89,10 @@ public static function pdfForChange(PluginPdfSimplePDF $pdf, Change $change)
$pdf->displayLine(
TicketValidation::getStatus($row['status']),
Html::convDateTime($row['submission_date']),
- $dbu->getUserName($row['users_id']),
+ PluginPdfCommon::getUserNameCached($row['users_id']),
trim(html_entity_decode($row['comment_submission'] ?? '', ENT_QUOTES, 'UTF-8')),
Html::convDateTime($row['validation_date']),
- $dbu->getUserName($row['users_id_validate']),
+ PluginPdfCommon::getUserNameCached($row['users_id_validate']),
trim(html_entity_decode($row['comment_validation'] ?? '', ENT_QUOTES, 'UTF-8')),
);
}
diff --git a/inc/common.class.php b/inc/common.class.php
index bf20a7a9..3e3f224d 100644
--- a/inc/common.class.php
+++ b/inc/common.class.php
@@ -449,6 +449,46 @@ public static function getGroupNames(array $groups_ids): string
return Toolbox::stripTags(implode(', ', $names));
}
+ /**
+ * Get the name of a user, memoized for the current request
+ *
+ * @param int $users_id
+ *
+ * @return string
+ */
+ public static function getUserNameCached($users_id): string
+ {
+ static $names = [];
+
+ $users_id = (int) $users_id;
+ if (!array_key_exists($users_id, $names)) {
+ $dbu = new DbUtils();
+ $names[$users_id] = $dbu->getUserName($users_id);
+ }
+
+ return $names[$users_id];
+ }
+
+ /**
+ * Get the name of a dropdown value, memoized for the current request
+ *
+ * @param string $table
+ * @param int $id
+ *
+ * @return string
+ */
+ public static function getDropdownNameCached(string $table, $id): string
+ {
+ static $names = [];
+
+ $key = $table . '#' . (int) $id;
+ if (!array_key_exists($key, $names)) {
+ $names[$key] = (string) Dropdown::getDropdownName($table, $id);
+ }
+
+ return $names[$key];
+ }
+
/**
* Get the groups ids of several items of the same type, indexed by item id
*
diff --git a/inc/commonitilcost.class.php b/inc/commonitilcost.class.php
index 8532c2bc..0a1b05c5 100644
--- a/inc/commonitilcost.class.php
+++ b/inc/commonitilcost.class.php
@@ -121,7 +121,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $job)
$data['name'],
Html::convDate($data['begin_date']),
Html::convDate($data['end_date']),
- Toolbox::stripTags(Dropdown::getDropdownName(
+ Toolbox::stripTags(PluginPdfCommon::getDropdownNameCached(
'glpi_budgets',
$data['budgets_id'],
)),
diff --git a/inc/config.class.php b/inc/config.class.php
index 7dc583b5..7279dba4 100644
--- a/inc/config.class.php
+++ b/inc/config.class.php
@@ -150,8 +150,6 @@ public static function install(Migration $mig)
public static function showConfigForm($item)
{
- /** @var array $PDF_DEVICES */
- global $PDF_DEVICES;
$config = self::getInstance();
$config->showFormHeader();
@@ -167,7 +165,7 @@ public static function showConfigForm($item)
}
$options = [];
- foreach ($PDF_DEVICES as $option => $value) {
+ foreach (self::getDevices() as $option => $value) {
$options[$option] = $option . ' - ' . $value[0] . ' (' . $value[1] . ')';
}
@@ -360,28 +358,42 @@ public static function currency()
'ZWL' => [__s('Zimbabwe Dollar', 'pdf'), '$', false]];
}
- public static function formatNumber($value)
+ /**
+ * Get the list of the currencies
+ *
+ * @return array
+ */
+ public static function getDevices()
{
- /** @var array $PDF_DEVICES */
+ /** @var array|null $PDF_DEVICES */
global $PDF_DEVICES;
+ return $PDF_DEVICES ??= self::currency();
+ }
- $config = new Config();
- $language = '';
- foreach ($config->find(['context' => 'core',
- 'name' => 'language']) as $row) {
- $language = $row['value'];
- }
- $user = new User();
- $user->getFromDB($_SESSION['glpiID']);
- if (!empty($user->fields['language'])) {
- $language = $user->fields['language'];
+ public static function formatNumber($value)
+ {
+ static $formatters = [];
+
+ $users_id = (int) ($_SESSION['glpiID'] ?? 0);
+ if (!isset($formatters[$users_id])) {
+ $config = new Config();
+ $language = '';
+ foreach ($config->find(['context' => 'core',
+ 'name' => 'language']) as $row) {
+ $language = $row['value'];
+ }
+ $user = new User();
+ $user->getFromDB($_SESSION['glpiID']);
+ if (!empty($user->fields['language'])) {
+ $language = $user->fields['language'];
+ }
+ $formatters[$users_id] = numfmt_create($language, NumberFormatter::CURRENCY);
}
$currency = PluginPdfConfig::getInstance();
- $fmt = numfmt_create($language, NumberFormatter::CURRENCY);
- $val = numfmt_format_currency($fmt, (float) $value, $currency->getField('currency'));
- foreach ($PDF_DEVICES as $option => $value) {
+ $val = numfmt_format_currency($formatters[$users_id], (float) $value, $currency->getField('currency'));
+ foreach (self::getDevices() as $option => $value) {
if ($currency->fields['currency'] == $option) {
$sym = $value[1];
@@ -392,11 +404,8 @@ public static function formatNumber($value)
public static function currencyName()
{
- /** @var array $PDF_DEVICES */
- global $PDF_DEVICES;
-
$config = self::getInstance();
- foreach ($PDF_DEVICES as $option => $value) {
+ foreach (self::getDevices() as $option => $value) {
if ($config->getField('currency') == $option && isset($value[2])) {
return $value[0];
}
diff --git a/inc/infocom.class.php b/inc/infocom.class.php
index 37b45a45..e06a9615 100644
--- a/inc/infocom.class.php
+++ b/inc/infocom.class.php
@@ -42,8 +42,7 @@ public function __construct(?CommonGLPI $obj = null)
public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
{
/** @var array $CFG_GLPI */
- /** @var array $PDF_DEVICES */
- global $CFG_GLPI, $PDF_DEVICES;
+ global $CFG_GLPI;
$ID = $item->getField('id');
@@ -210,7 +209,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
$currency = PluginPdfConfig::getInstance();
$sym = '';
- foreach ($PDF_DEVICES as $option => $value) {
+ foreach (PluginPdfConfig::getDevices() as $option => $value) {
if ($currency->fields['currency'] == $option) {
$sym = $value[1];
}
diff --git a/inc/item_softwareversion.class.php b/inc/item_softwareversion.class.php
index 8217ceb7..632f05f7 100644
--- a/inc/item_softwareversion.class.php
+++ b/inc/item_softwareversion.class.php
@@ -437,6 +437,38 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, $item)
$title = sprintf(__s('%1$s: %2$s'), $title, count($output));
$pdf->displayTitle($title);
+ $licenses_by_version = [];
+ foreach ($DB->request([
+ 'SELECT' => [
+ 'glpi_softwarelicenses.*',
+ 'glpi_softwarelicensetypes.name AS type',
+ ],
+ 'FROM' => 'glpi_items_softwarelicenses',
+ 'INNER JOIN' => [
+ 'glpi_softwarelicenses' => [
+ 'ON' => [
+ 'glpi_items_softwarelicenses' => 'softwarelicenses_id',
+ 'glpi_softwarelicenses' => 'id',
+ ],
+ ],
+ ],
+ 'LEFT JOIN' => [
+ 'glpi_softwarelicensetypes' => [
+ 'ON' => [
+ 'glpi_softwarelicenses' => 'softwarelicensetypes_id',
+ 'glpi_softwarelicensetypes' => 'id',
+ ['AND' => ['glpi_items_softwarelicenses.itemtype' => 'Computer']],
+ ],
+ ],
+ ],
+ 'WHERE' => [
+ 'glpi_items_softwarelicenses.items_id' => $ID,
+ ],
+ ]) as $licdata) {
+ $version_key = $licdata['softwareversions_id_use'] ?: $licdata['softwareversions_id_buy'];
+ $licenses_by_version[$version_key][] = $licdata;
+ }
+
$cat = -1;
foreach ($output as $soft) {
if ($soft['softwarecategories_id'] != $cat) {
@@ -459,45 +491,9 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, $item)
// From Item_SoftwareVersion::displaySoftsByCategory()
$verid = $soft['verid'];
- $query_license_params = [
- 'SELECT' => [
- 'glpi_softwarelicenses.*',
- 'glpi_softwarelicensetypes.name AS type',
- ],
- 'FROM' => 'glpi_items_softwarelicenses',
- 'INNER JOIN' => [
- 'glpi_softwarelicenses' => [
- 'ON' => [
- 'glpi_items_softwarelicenses' => 'softwarelicenses_id',
- 'glpi_softwarelicenses' => 'id',
- ],
- ],
- ],
- 'LEFT JOIN' => [
- 'glpi_softwarelicensetypes' => [
- 'ON' => [
- 'glpi_softwarelicenses' => 'softwarelicensetypes_id',
- 'glpi_softwarelicensetypes' => 'id',
- ['AND' => ['glpi_items_softwarelicenses.itemtype' => 'Computer']],
- ],
- ],
- ],
- 'WHERE' => [
- 'glpi_items_softwarelicenses.items_id' => $ID,
- 'OR' => [
- 'glpi_softwarelicenses.softwareversions_id_use' => $verid,
- [
- 'AND' => [
- 'glpi_softwarelicenses.softwareversions_id_use' => 0,
- 'glpi_softwarelicenses.softwareversions_id_buy' => $verid,
- ],
- ],
- ],
- ],
- ];
$lic = '';
- foreach ($DB->request($query_license_params) as $licdata) {
+ foreach (($verid !== null ? $licenses_by_version[$verid] ?? [] : []) as $licdata) {
$installed[] = $licdata['id'];
$lic .= (empty($lic) ? '' : ', ') . '' . $licdata['name'] . ' ' . $licdata['serial'];
if (!empty($licdata['type'])) {
diff --git a/inc/itilfollowup.class.php b/inc/itilfollowup.class.php
index 1ef03b1d..d5e38660 100644
--- a/inc/itilfollowup.class.php
+++ b/inc/itilfollowup.class.php
@@ -44,8 +44,6 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item, $pr
/** @var DBmysql $DB */
global $DB;
- $dbu = new DbUtils();
-
$ID = $item->getField('id');
$type = $item->getType();
@@ -88,14 +86,14 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item, $pr
'' . __s('Requester') . ''); // Author
foreach ($result as $data) {
- $lib = $data['requesttypes_id'] ? Dropdown::getDropdownName('glpi_requesttypes', $data['requesttypes_id']) : '';
+ $lib = $data['requesttypes_id'] ? PluginPdfCommon::getDropdownNameCached('glpi_requesttypes', $data['requesttypes_id']) : '';
if ($data['is_private']) {
$lib = sprintf(__s('%1$s (%2$s)'), $lib, __s('Private'));
}
$pdf->displayLine(
Toolbox::stripTags($lib),
Html::convDateTime($data['date']),
- Toolbox::stripTags($dbu->getUserName($data['users_id'])),
+ Toolbox::stripTags(PluginPdfCommon::getUserNameCached($data['users_id'])),
);
$content = $data['content'];
diff --git a/inc/itilsolution.class.php b/inc/itilsolution.class.php
index 94302fe2..5242b135 100644
--- a/inc/itilsolution.class.php
+++ b/inc/itilsolution.class.php
@@ -44,8 +44,6 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
/** @var DBmysql $DB */
global $DB;
- $dbu = new DbUtils();
-
$pdf->setColumnsSize(100);
$soluce = $DB->request(
@@ -63,7 +61,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
$pdf->displayTitle($title);
foreach ($soluce as $row) {
if ($row['solutiontypes_id']) {
- $title = Toolbox::stripTags(Dropdown::getDropdownName(
+ $title = Toolbox::stripTags(PluginPdfCommon::getDropdownNameCached(
'glpi_solutiontypes',
$row['solutiontypes_id'],
));
@@ -89,7 +87,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
sprintf(
__s('%1$s %2$s'),
__s('By'),
- Toolbox::stripTags($dbu->getUserName($row['users_id_approval'])),
+ Toolbox::stripTags(PluginPdfCommon::getUserNameCached($row['users_id_approval'])),
)
. '';
}
diff --git a/inc/networkport.class.php b/inc/networkport.class.php
index bfb2390f..6d78d937 100644
--- a/inc/networkport.class.php
+++ b/inc/networkport.class.php
@@ -52,7 +52,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
$pdf->setColumnsSize(100);
$result = $DB->request(
- ['FROM' => 'glpi_networkports'] + ['SELECT' => ['id', 'name', 'logical_number'],
+ ['FROM' => 'glpi_networkports'] + ['SELECT' => ['id', 'name', 'logical_number', 'instantiation_type', 'mac'],
'WHERE' => ['items_id' => $ID,
'itemtype' => $type],
'ORDER' => ['name', 'logical_number']],
@@ -71,29 +71,27 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
$pdf->displayTitle($title);
foreach ($result as $devid) {
- $netport = new NetworkPort();
- $netport->getfromDB(current($devid));
- $instantiation_type = $netport->fields['instantiation_type'];
+ $instantiation_type = $devid['instantiation_type'];
$instname = call_user_func([$instantiation_type, 'getTypeName']);
$pdf->displayTitle('' . $instname . '');
$pdf->displayLine('' . sprintf(
__s('%1$s: %2$s'),
'#',
- $netport->fields['logical_number'],
+ $devid['logical_number'],
));
$pdf->displayLine('' . sprintf(
__s('%1$s: %2$s'),
__s('Name') . '',
- $netport->fields['name'],
+ $devid['name'],
));
$contact = new NetworkPort();
$netport2 = new NetworkPort();
$add = __s('Not connected.');
- $cid = $contact->getContact($netport->fields['id']);
+ $cid = $contact->getContact($devid['id']);
if ($cid && $netport2->getFromDB($cid) && ($device2 = $dbu->getItemForItemtype($netport2->fields['itemtype'])) && $device2->getFromDB($netport2->fields['items_id'])) {
$add = $netport2->getName() . ' ' . __s('on') . ' ' . $device2->getName() . ' (' . $device2->getTypeName() . ')';
}
@@ -107,7 +105,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
$netportethernet = new NetworkPortEthernet();
$speed = $type = '';
- if ($netportethernet->getFromDBByCrit(['networkports_id' => $netport->fields['id']])) {
+ if ($netportethernet->getFromDBByCrit(['networkports_id' => $devid['id']])) {
$speed = NetworkPortEthernet::getPortSpeed($netportethernet->fields['speed']);
$type = NetworkPortEthernet::getPortTypeName($netportethernet->fields['type']);
}
@@ -135,7 +133,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
$pdf->displayLine('' . sprintf(
__s('%1$s: %2$s'),
__s('MAC') . '',
- $netport->fields['mac'],
+ $devid['mac'],
));
$ips = $DB->request(
@@ -154,7 +152,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
],
],
'WHERE' => [
- 'glpi_networknames.items_id' => $netport->fields['id'],
+ 'glpi_networknames.items_id' => $devid['id'],
'glpi_networknames.itemtype' => NetworkPort::class,
],
],
diff --git a/inc/problemtask.class.php b/inc/problemtask.class.php
index f89d5065..e4bd9c5d 100755
--- a/inc/problemtask.class.php
+++ b/inc/problemtask.class.php
@@ -44,8 +44,6 @@ public static function pdfForProblem(PluginPdfSimplePDF $pdf, Problem $job)
/** @var DBmysql $DB */
global $DB;
- $dbu = new DbUtils();
-
$ID = $job->getField('id');
//////////////Tasks///////////
@@ -108,17 +106,17 @@ public static function pdfForProblem(PluginPdfSimplePDF $pdf, Problem $job)
$planification .= '
' . sprintf(
__s('%1$s: %2$s'),
__s('By'),
- $dbu->getUserName($data['users_id_tech']),
+ PluginPdfCommon::getUserNameCached($data['users_id_tech']),
);
}
- $lib = $data['taskcategories_id'] ? Dropdown::getDropdownName('glpi_taskcategories', $data['taskcategories_id']) : '';
+ $lib = $data['taskcategories_id'] ? PluginPdfCommon::getDropdownNameCached('glpi_taskcategories', $data['taskcategories_id']) : '';
$pdf->displayLine(
'' . Toolbox::stripTags($lib),
Html::convDateTime($data['date']),
Html::timestampToString($data['actiontime'], false),
- Toolbox::stripTags($dbu->getUserName($data['users_id'])),
+ Toolbox::stripTags(PluginPdfCommon::getUserNameCached($data['users_id'])),
Toolbox::stripTags($planification),
1,
);
diff --git a/inc/simplepdf.class.php b/inc/simplepdf.class.php
index b30456f9..5ac8b375 100644
--- a/inc/simplepdf.class.php
+++ b/inc/simplepdf.class.php
@@ -251,33 +251,10 @@ private function displayInternal($gray, $padd, $defalign, $miny, $msgs)
$msgs,
);
- /* dry run - compute max cell height */
- $this->pdf->startTransaction();
- $i = 0;
- foreach ($msgs as $msg) {
- if ($i < count($this->cols)) {
- $this->pdf->writeHTMLCell(
- $this->colsw[$i], // $w (float) Cell width. If 0, the cell extends up to the right margin.
- $miny, // $h (float) Cell minimum height. The cell extends automatically if needed.
- '', // $x (float) upper-left corner X coordinate
- '', // $y (float) upper-left corner Y coordinate
- $msg, // $html (string) html text to print. Default value: empty string.
- 0, // $border (mixed) Indicates if borders must be drawn around the cell. The value can be a number: