Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fix images display in PDF: Make them display at their original position instead of at the end of the document.
- Fix image rendering in Change/Problem descriptions and Problem task PDF exports

## [4.1.3] - 2026-06-24
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../PluginsMakefile.mk
2 changes: 1 addition & 1 deletion inc/knowbaseitem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, KnowbaseItem $item)
);
$pdf->displayLine(
$dbu->getUserName($item->fields['users_id']),
Html::convDateTime($item->fields['date']),
Html::convDateTime($item->fields['date_creation']),
Html::convDateTime($item->fields['date_mod']),
Dropdown::getYesNo($item->fields['is_faq']),
$item->fields['view'],
Expand Down
30 changes: 8 additions & 22 deletions inc/simplepdf.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public function render()
**/
public function output($name = false)
{
ob_clean();
if (!$name) {
return $this->pdf->Output('glpi.pdf', 'S');
}
Expand Down Expand Up @@ -351,21 +352,19 @@ public function displayText($name, $content = '', $minline = 3, $maxline = 100)
// Decode HTML entities BEFORE searching for images
$content = html_entity_decode($content, ENT_QUOTES, 'UTF-8');

// Extract images and remove from HTML - TCPDF will render them separately
preg_match_all(
'/<img\b[^>]*src=["\']([^"\']*docid=(\d+)[^"\']*)["\'][^>]*>/i',
'/<a\b[^>]*>.*?(<img\b[^>]*src=["\']([^"\']*docid=(\d+)[^"\']*)["\'][^>]*>).*?<\/a>/is',
$content,
$matches,
PREG_SET_ORDER,
);

$images_to_display = [];
if ($matches !== []) {
$document = new Document();
foreach ($matches as $match) {
$full_img_tag = $match[0];
$original_url = $match[1];
$docid = (int) $match[2];
$full_a_tag = $match[0];
$full_img_tag = $match[1];
$docid = (int) $match[3];

if ($document->getFromDB($docid) && isset($document->fields['filepath'])) {
$file_path = GLPI_DOC_DIR . '/' . $document->fields['filepath'];
Expand Down Expand Up @@ -410,15 +409,9 @@ public function displayText($name, $content = '', $minline = 3, $maxline = 100)
$display_height = (int) ($display_height * $ratio);
}

// Store image info for later insertion
$images_to_display[] = [
'tag' => $full_img_tag,
'path' => $file_path,
'width' => $display_width,
'height' => $display_height,
];
// Remove the img tag from HTML - we'll add images separately
$content = str_replace($full_img_tag, '', $content);
// Replace the original image by a properly resized one + remove the a tag
$display_image_tag = '<img src="file://' . $file_path . '" width="' . $display_width . '" height="' . $display_height . '">';
$content = str_replace($full_a_tag, $display_image_tag, $content);
}
}
}
Expand All @@ -445,13 +438,6 @@ public function displayText($name, $content = '', $minline = 3, $maxline = 100)

$this->displayInternal(240, 0.5, self::LEFT, $minline * 5, [$formatted_content]);

// Now add extracted images after the text with their original dimensions
foreach ($images_to_display as $img_info) {
if (file_exists($img_info['path'])) {
$this->addPngFromFile($img_info['path'], $img_info['width'], $img_info['height']);
}
}

/* Restore */
[$this->cols, $this->colsx, $this->colsw, $this->align, ] = $save;
}
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parameters:
parallel:
maximumNumberOfProcesses: 2
level: 5
treatPhpDocTypesAsCertain: false
bootstrapFiles:
- ../../stubs/glpi_constants.php
- ../../vendor/autoload.php
Expand Down
Loading