指定SVG填充颜色的TCPDF

指定SVG填充颜色的TCPDF,svg,pdf-generation,tcpdf,Svg,Pdf Generation,Tcpdf,TCPDF如何为SVG文件指定颜色? 我的是纯黑的,尽管文件中规定了其他颜色,但我似乎无法超越它。 理想情况下,我希望能够动态设置颜色 $pdf->SetTextColor(255,255,255); $pdf->SetTextColorArray(255,255,255); $pdf->SetFillColor(255,255,255); $pdf->SetFillColorArray(255,255,255); $pdf->SetDrawColor(255,2

TCPDF如何为SVG文件指定颜色?

我的是纯黑的,尽管文件中规定了其他颜色,但我似乎无法超越它。 理想情况下,我希望能够动态设置颜色

$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);
下面这些例子


方法复杂,生成速度也慢了一点,但效果不错

    $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);

改进了处理多个SVG文件的逻辑

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);

找到一个setSVGStyles,但不确定如何使用它