php-svg-lib icon indicating copy to clipboard operation
php-svg-lib copied to clipboard

CMYK support for SVG in print-PDF

Open pittikolbenhirse opened this issue 9 years ago • 2 comments

bon jour, while dompdf supports cmyk( c,m,y,k ) php-svg-lib does not. is it possible (for me) to change that? merci d'avance, christoph

pittikolbenhirse avatar Sep 22 '16 05:09 pittikolbenhirse

I've found a solution but faild to open a pull request :)

it needs two little changes:

in /dompdf/lib/php-svg-lib/src/Svg/Style.php

add:

static function getArray($color)
    {
        $c = array(null, null, null, null, "hex" => null);
        $c = $color;
        $c["c"] = $c[0];
        $c["m"] = $c[1];
        $c["y"] = $c[2];
        $c["k"] = $c[3];
        $c["hex"] = "cmyk($c[0],$c[1],$c[2],$c[3])";
        return $c;
    }

and in static function parseColor($color) add:

// cmyk( c,m,y,k )
        if (strpos($color, "cmyk") !== false) {

            $i = strpos($color, "(");
            $j = strpos($color, ")");

            // Bad color value
            if ($i === false || $j === false) {
                return null;
            }

            $values = explode(",", mb_substr($color, $i + 1, $j - $i - 1));

            if (count($values) != 4) {
                return null;
            }

            $values = array_map(function($c) {
                return min(1.0, max(0.0, floatval(trim($c))));
            }, $values);
            $cache[$color] = self::getArray($values);

            return $cache[$color];
        }

in /dompdf/lib/php-svg-lib/src/Svg/Surface/SurfaceCpdf.php in public function setStyle(Style $style) change:

if ($fill = $style->fill) {
    $canvas->setColor(array($fill[0]/255, $fill[1]/255, $fill[2]/255), true);   
}

to:

if ($fill = $style->fill) {
    if($fill[3]!== null){
        $canvas->setColor(array($fill[0], $fill[1], $fill[2], $fill[3]), true);
    } else {
        $canvas->setColor(array($fill[0]/255, $fill[1]/255, $fill[2]/255), true);
    }            
 }

example svg-file:

<?xml version="1.0" encoding="utf-8"?>
<svg version="1.2" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="135px" height="135px" viewBox="0 0 135 135" xml:space="preserve">
<rect x="5" y="5" style="fill: cmyk(1.0,0.0,0.0,0.0);" width="60" height="60"/>
<rect x="70" y="5" style="fill: cmyk(0.0,1.0,0.0,0.0);" width="60" height="60"/>
<rect x="5" y="70" style="fill: cmyk(0.0,0.0,1.0,0.0);" width="60" height="60"/>
<rect x="70" y="70" style="fill: cmyk(0.0,0.0,0.0,1.0);" width="60" height="60"/>
</svg>

example php-file:

<?php 
require_once("../dompdf/autoload.inc.php"); 
$html = '<style>@page { size: 135px 135px; margin: 0; }</style><img src="CMYK.svg" />';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->set_option( 'dpi' , '300' );
$dompdf->loadHtml($html);
$dompdf->render();
$output = $dompdf->output();
$dompdf->stream();
?>

pittikolbenhirse avatar Sep 23 '16 06:09 pittikolbenhirse

You should leave this issue open so the changes can be incorporated. If it's closed then it may be forgotten.

bsweeney avatar Sep 23 '16 12:09 bsweeney