Skip to content

apphancer/wkhtmltopdf-stroke-dashoffset-for-svg

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 

wkhtmltopdf-stroke-dashoffset-for-svg

PHP helper class that calculates values for stroke-dasharray parameter of SVG images.

There is a known bug in WKHTMLTOPDF which prevents donut charts generated using SVG images to correctly render the stroke-dashoffset parameter resulting in incorrect data visualisation.

The bug is reported here wkhtmltopdf/wkhtmltopdf#3031

The solution is to ignore the stroke-dashoffset parameter and have a complex calculation of the four points used in the stroke-dasharray parameter. This is where this Helper class comes into place.

Usage

$svgChart = new SvgChart($score = 33, $maxScore = 100);
$dashArray = $svgChart->dashArray();

Example

$svgChart = new SvgChart(33, 100);
<svg width="212px" height="212px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 212 212">
    <!-- full circle, at 90 radius, is 566 units in circumference -->
    <!-- now try a second set (to right) with dash offset 0, to work around
         problem in wkHTMLtoPDF (ignoring dash offset) -->
    <g>
        <!-- background full circle 566 on, 0 off -->
        <circle r="90" cy="106" cx="106" stroke-width="20"
                stroke="#d9d9d9" fill="none"
                stroke-dasharray="424 283"></circle>
        <!-- filled portion of arc. to get around dashoffset being ignored,
             dash cycle is CW, length is .25 arc on, .5 off, .25 on  -->
        <circle r="90" cy="106" cx="106" stroke-width="20"
                stroke="#26af61" fill="none"
                stroke-dasharray="<?= $svgChart->dashArray() ?>"></circle>
        <text transform="translate(80, 125)" style="font-size:36pt">33</text>
    </g>
</svg>

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages