TCPDF Specifying SVG Fill Color












1















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










share|improve this question























  • Found a setSVGStyles but unsure how to use it tcpdf.org/docs/srcdoc/TCPDF/class-TCPDF/#_setSVGStyles

    – Dwayne
    Dec 31 '18 at 1:58


















1















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










share|improve this question























  • Found a setSVGStyles but unsure how to use it tcpdf.org/docs/srcdoc/TCPDF/class-TCPDF/#_setSVGStyles

    – Dwayne
    Dec 31 '18 at 1:58
















1












1








1








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










share|improve this question














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







svg pdf-generation tcpdf






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 30 '18 at 23:14









DwayneDwayne

63




63













  • Found a setSVGStyles but unsure how to use it tcpdf.org/docs/srcdoc/TCPDF/class-TCPDF/#_setSVGStyles

    – Dwayne
    Dec 31 '18 at 1:58





















  • Found a setSVGStyles but unsure how to use it tcpdf.org/docs/srcdoc/TCPDF/class-TCPDF/#_setSVGStyles

    – Dwayne
    Dec 31 '18 at 1:58



















Found a setSVGStyles but unsure how to use it tcpdf.org/docs/srcdoc/TCPDF/class-TCPDF/#_setSVGStyles

– Dwayne
Dec 31 '18 at 1:58







Found a setSVGStyles but unsure how to use it tcpdf.org/docs/srcdoc/TCPDF/class-TCPDF/#_setSVGStyles

– Dwayne
Dec 31 '18 at 1:58














2 Answers
2






active

oldest

votes


















0














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





share|improve this answer































    0














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





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53982203%2ftcpdf-specifying-svg-fill-color%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














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





      share|improve this answer




























        0














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





        share|improve this answer


























          0












          0








          0







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





          share|improve this answer













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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 7:09









          DwayneDwayne

          63




          63

























              0














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





              share|improve this answer




























                0














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





                share|improve this answer


























                  0












                  0








                  0







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





                  share|improve this answer













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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 8:06









                  DwayneDwayne

                  63




                  63






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53982203%2ftcpdf-specifying-svg-fill-color%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith