[TikiWiki-commits] [Git][tikiwiki/tiki][master] [FIX] mPDF - Anchor links work in the browser but not when exported to a PDF

"Jonny Bradley \(@jonnybradley\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <68d5250a8ea14_2cdf7f0492b0@gitlab-sidekiq-low-urgency-cpu-bound-v2-fc8654dbb-2fmjq.mail>

Jonny Bradley pushed to branch master at Tiki Wiki CMS Groupware / Tiki


Commits:
39b9cfa3 by Domeshow Emmanuel at 2025-09-25T11:10:50+00:00
[FIX] mPDF - Anchor links work in the browser but not when exported to a PDF
---
* [FIX] mPDF - Anchor links work in the browser but not when exported to a PDF

See merge request tikiwiki/tiki!8533

- - - - -


1 changed file:

- lib/pdflib.php


Changes:

=====================================
lib/pdflib.php
=====================================
@@ -495,6 +495,9 @@ class PdfGenerator
             }
         }
 
+        // Insert <a name="id"></a> immediately before each element with an id
+        $html = $this->addNamedAnchorsBeforeIdTags($html);
+
         $pdfPages = $this->getPDFPages($html, $pdfSettings);
         //adding css styles with first page content
         $allCss = '<style>' .
@@ -733,6 +736,29 @@ class PdfGenerator
         return $mpdf->Output('', 'S');                  // Return as a string
     }
 
+    public function addNamedAnchorsBeforeIdTags(string $html): string
+    {
+        $dom = new DOMDocument();
+        libxml_use_internal_errors(true);
+        $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
+
+        $xpath = new DOMXPath($dom);
+        $nodes = $xpath->query('//*[@id]'); // all elements with an id
+
+        foreach ($nodes as $node) {
+            $id = $node->getAttribute('id');
+
+            // Create <a name="id"></a>
+            $a = $dom->createElement("a");
+            $a->setAttribute("name", $id);
+
+            // Insert it just before the node with the id
+            $node->parentNode->insertBefore($a, $node);
+        }
+
+        return $dom->saveHTML();
+    }
+
     public function getHtmlLayout($pageContent)
     {
         require_once('tiki-setup.php');



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/39b9cfa3b61abc2256e02156eb51746e5274f027

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/39b9cfa3b61abc2256e02156eb51746e5274f027
You're receiving this email because of your account on gitlab.com.

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.