Reputation: 91
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
Upvotes: 4
Views: 2305
Reputation: 91
Improved logic to handle multiple SVG files
function getModifiedSvgString($svg, $color) {
$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', $color);
}
foreach ($circle_list as $circle) {
$circle->setAttribute('fill', $color);
}
return $doc->saveXML();
}
// Icon 1
$svg = get_stylesheet_directory_uri() . '/images/icon-1.svg';
$svgString = getModifiedSvgString($svg, $UserCompanyColourSecondaryFontHex);
$pdf->ImageSVG($file = '@' . $svgString, $x = 130, $y = 124, $w = 10, $h = 10, $link = '', $align = '', $palign = '', $border = 0, $fitonpage = false);
// Icon 2
$svg = get_stylesheet_directory_uri() . '/images/icon-2.svg';
$svgString = getModifiedSvgString($svg, $UserCompanyColourSecondaryFontHex);
$pdf->ImageSVG($file = '@' . $svgString, $x = 155, $y = 124, $w = 10, $h = 10, $link = '', $align = '', $palign = '', $border = 0, $fitonpage = false);
// Icon 3
$svg = get_stylesheet_directory_uri() . '/images/icon-3.svg';
$svgString = getModifiedSvgString($svg, $UserCompanyColourSecondaryFontHex);
$pdf->ImageSVG($file = '@' . $svgString, $x = 178, $y = 124, $w = 18, $h = 10, $link = '', $align = '', $palign = '', $border = 0, $fitonpage = false);
Upvotes: 4
Reputation: 91
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);
Upvotes: 1