Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF Specifying SVG Fill Color

How does TCPDF specify color to a SVG file? https://tcpdf.org

Mine come out solid black even though other colours are specified in the files and I can not seem to over ride it. Ideally I want to be in a position to dynamically set the color

$pdf->SetTextColor(255,255,255);
$pdf->SetTextColorArray(255,255,255);
$pdf->SetFillColor(255,255,255);
$pdf->SetFillColorArray(255,255,255);
$pdf->SetDrawColor(255,255,255);
$pdf->ImageSVG($file=get_stylesheet_directory_uri().'/images/icon-bath.svg', $x=155, $y=124, $w=10, $h=10, $link='', $align='', $palign='', $border=0, $fitonpage=false);

Following these examples

https://tcpdf.org/examples/example_058/

https://hotexamples.com/examples/-/TCPDF/ImageSVG/php-tcpdf-imagesvg-method-examples.html

like image 688
Dwayne Avatar asked Oct 26 '25 02:10

Dwayne


1 Answers

Complicated method and slows the generation a fair bit but it works

    $svg = get_stylesheet_directory_uri().'/images/icon.svg';
    $doc = new DOMDocument;
    $doc->load($svg);
    $xpath = new DOMXPath($doc);
    $xpath->registerNamespace('svg', 'http://www.w3.org/2000/svg');
    $path_list = $xpath->query('svg:path');
    $circle_list = $xpath->query('svg:circle');
    foreach ($path_list as $path) {
        $path->setAttribute('fill', '#FFFFFF');
    }
    foreach ($circle_list as $circle) {
        $circle->setAttribute('fill', '#FFFFFF');
    }
    $svgString = $doc->saveXML();

    $pdf->ImageSVG('@' . $svgString, $x=130, $y=124, $w=10, $h=10, $link='', $align='', $palign='', $border=0, $fitonpage=false);
like image 115
Dwayne Avatar answered Oct 28 '25 04:10

Dwayne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!