Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

CMYK support for SVG in print-PDF #9

Open
pittikolbenhirse opened this issue Sep 22, 2016 · 2 comments
Open

CMYK support for SVG in print-PDF #9

pittikolbenhirse opened this issue Sep 22, 2016 · 2 comments

Comments

@pittikolbenhirse
Copy link

pittikolbenhirse commented Sep 22, 2016

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
Copy link
Author

pittikolbenhirse commented Sep 23, 2016

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

@bsweeney
Copy link
Member

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants