From 34994c11dcc10903447b2822778badf4c3624234 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 18 Oct 2022 16:39:40 +0200 Subject: [PATCH] wip #5532 @1 --- src/PDFTools.php | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/PDFTools.php b/src/PDFTools.php index 8a8a911..3d3158f 100644 --- a/src/PDFTools.php +++ b/src/PDFTools.php @@ -7,6 +7,7 @@ use Cubist\Util\Files\Files; use Cubist\Util\Str; use Cubist\Util\Text; use DOMDocument; +use DOMElement; use DOMNode; use DOMXPath; use Cubist\PDF\CommandLine\FWSTK; @@ -226,11 +227,7 @@ class PDFTools $xpath->registerNamespace('svg', 'http://www.w3.org/2000/svg'); $xpath->registerNamespace('xlink', 'http://www.w3.org/1999/xlink'); $xpath->registerNamespace("php", "http://php.net/xpath"); - $toDelete = array( -// '//svg:defs/svg:clipPath', -// '//svg:defs/svg:image', -// '//svg:defs/svg:path', -// '//svg:defs/svg:pattern', + $toDelete = [ '//svg:defs/svg:g[starts-with(@id, "surface")]//svg:path', '//svg:defs/svg:g[starts-with(@id, "surface")]//svg:rect', '//svg:defs/svg:g[starts-with(@id, "surface")]//svg:use[starts-with(@xlink:href, "#image")]', @@ -239,16 +236,13 @@ class PDFTools '/svg:svg/svg:g[@id="surface1"]//svg:filter', '/svg:svg/svg:g[@id="surface1"]//svg:use[starts-with(@xlink:href, "#image")]', '//svg:svg/svg:g[@id="surface1"]//svg:use[starts-with(@xlink:href, "#image")]', - //'//svg:image', - ); + ]; + $toDeleteIfOrphan = [ + '//svg:image', + ]; - //global $svglog; - //$svglog = array('XPATH : ' . print_r($xpath, true)); foreach ($toDelete as $q) { $list = $xpath->query($q); - // $svglog[] = "Evaluate xpath query " . $q; - // $svglog[] = 'Give ' . $list->length . ' results'; - // $svglog[] = 'Deleting Nodes in ' . print_r($list, true); if (count($list)) { foreach ($list as $node) { /* @var $node DOMNode */ @@ -257,6 +251,21 @@ class PDFTools } } } + + foreach ($toDeleteIfOrphan as $q) { + $list = $xpath->query($q); + if (count($list)) { + foreach ($list as $node) { + /* @var $node DOMElement */ + $id = $node->getAttribute('id'); + if ($xpath->query('//*[@id="' . $id . '"]')->count() > 0) { + $parent = $node->parentNode; + $parent->removeChild($node); + } + } + } + } + $res = $svg->saveXML(); $res = preg_replace('//', '', $res); while (true) { -- 2.39.5