Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [UNRELEASE]

- Fix missing images in exported Knowledge Base PDFs

## [4.0.1] - 2025-03-10

### Added
Expand Down
23 changes: 17 additions & 6 deletions inc/knowbaseitem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,23 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, KnowbaseItem $item)
'UTF-8',
)));

$answer
= Toolbox::stripTags(Glpi\Toolbox\Sanitizer::unsanitize(html_entity_decode(
$item->getField('answer'),
ENT_QUOTES,
'UTF-8',
)));
$answer = Glpi\Toolbox\Sanitizer::unsanitize(Html::entity_decode_deep($item->getField('answer')));
$answer = preg_replace('#data:image/[^;]+;base64,#', '@', $answer);

preg_match_all('/<img [^>]*src=[\'"]([^\'"]*docid=([0-9]*))[^>]*>/', $answer, $res, PREG_SET_ORDER);

foreach ($res as $img) {
$docimg = new Document();
$docimg->getFromDB((int) $img[2]);
$width = null;

if (preg_match('/\bwidth=["\']?(\d+)/i', $img[0], $match_width)) {
$width = $match_width[1];
}

$path = '<img src="file://' . GLPI_DOC_DIR . '/' . $docimg->fields['filepath'] . '" width="' . $width . '"/>';
Comment on lines 88 to 94

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps we should take the example of the plugin's ticket.class.php class (which also processes images in the content)

 $content = Glpi\Toolbox\Sanitizer::unsanitize(Html::entity_decode_deep($job->fields['content']));

        $content = preg_replace('#data:image/[^;]+;base64,#', '@', $content);

        preg_match_all('/<img [^>]*src=[\'"]([^\'"]*docid=([0-9]*))[^>]*>/', $content, $res, PREG_SET_ORDER);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I took the code from ticket.class.php for image processing. This fixes the problem that prevented the plugin from displaying images but if the image had been resized, it was not taken into account.

$answer = str_replace($img[0], $path, $answer);
}

$pdf->setColumnsSize(100);

Expand Down