html2pdf icon indicating copy to clipboard operation
html2pdf copied to clipboard

External SVG files: Unable to get the size of the image - Fix propose

Open sumerolira opened this issue 4 years ago • 1 comments

Hello; I noticed that when trying to access an external svg file I get this error message: "Unable to get the size of the image"

I modified the code to fix it (if it is really a bug and I'm not missing something) in my local files and now it works, but I'm not very skilled in git usage and open source contributions and I would prefer propose this modification with this post instead of a commit. This does not work yet for in-url encoded svg and I haven't tested it extensively yet.

in function _drawImage in Html2Pdf.php I added before '// if the image does not exist, or can not be loaded':

$svg=false;
if (substr($src, -4)==".svg") {
	$svg=true;
	$content=file_get_contents($src);
	$xml = simplexml_load_string($content);
	$infos=[];
	$infos[0]=(float)$xml["width"];
	$infos[1]=(float)$xml["height"];
}

Then in '// display the image' block, instead of '$this->pdf->Image($src, $x, $y, $w, $h, '', $this->_isInLink);'

if (!$svg)
	$this->pdf->Image($src, $x, $y, $w, $h, '', $this->_isInLink);
else
	$this->pdf->ImageSVG($src, $x, $y, $w, $h, '', $this->_isInLink);

Thank you for your attention

sumerolira avatar Aug 28 '21 23:08 sumerolira

I also ran into this issue and was first confused, because the SVG file is reachable via link. If the proposed fix is suitable for @spipu I may spare some time to make a pull request.

k00ni avatar Jan 31 '23 12:01 k00ni