diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fe975a..c015eaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. - Fixed approval comments rendering as raw HTML in PDF - Fixed PDF crash when exporting Problems with linked items lacking serial/inventory fields - Fixed Change and Problem description exported as a single unstructured text block diff --git a/inc/common.class.php b/inc/common.class.php index 528f1ab..a883614 100644 --- a/inc/common.class.php +++ b/inc/common.class.php @@ -59,7 +59,6 @@ public function addStandardTab($itemtype, array &$ong, array $options) $withtemplate = $options['withtemplate']; } - // @phpstan-ignore-next-line function.impossibleType - is_numeric() kept for parity with CommonGLPI::addStandardTab(), whose $itemtype PHPDoc type makes it always false if (!is_numeric($itemtype) && ($obj = $dbu->getItemForItemtype($itemtype)) && (method_exists($itemtype, 'displayTabContentForPDF'))) { $titles = $obj->getTabNameForItem($this->obj, $withtemplate); if (!is_array($titles)) { diff --git a/inc/knowbaseitem.class.php b/inc/knowbaseitem.class.php index d32fe46..e01d23d 100644 --- a/inc/knowbaseitem.class.php +++ b/inc/knowbaseitem.class.php @@ -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'], diff --git a/inc/simplepdf.class.php b/inc/simplepdf.class.php index eb46c07..d505647 100644 --- a/inc/simplepdf.class.php +++ b/inc/simplepdf.class.php @@ -152,6 +152,9 @@ public function render() **/ public function output($name = false) { + if (ob_get_level() > 0) { + ob_clean(); + } if (!$name) { return $this->pdf->Output('glpi.pdf', 'S'); } @@ -353,21 +356,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( - '/]*src=["\']([^"\']*docid=(\d+)[^"\']*)["\'][^>]*>/i', + '/]*>\s*(]*src=["\']([^"\']*docid=(\d+)[^"\']*)["\'][^>]*>)\s*<\/a>/i', $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']; @@ -412,15 +413,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 = ''; + $content = str_replace($full_a_tag, $display_image_tag, $content); } } } @@ -447,13 +442,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; } diff --git a/phpstan.neon b/phpstan.neon index 3b12c3a..3f4ca49 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,6 +2,7 @@ parameters: parallel: maximumNumberOfProcesses: 2 level: 5 + treatPhpDocTypesAsCertain: false bootstrapFiles: - ../../stubs/glpi_constants.php - ../../vendor/autoload.php