diff --git a/CHANGELOG.md b/CHANGELOG.md index bc1e1c0..d31ee31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/inc/knowbaseitem.class.php b/inc/knowbaseitem.class.php index 5447797..c3e01a7 100644 --- a/inc/knowbaseitem.class.php +++ b/inc/knowbaseitem.class.php @@ -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('/]*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 = ''; + $answer = str_replace($img[0], $path, $answer); + } $pdf->setColumnsSize(100);