Search code examples
phppdfimagemagickimagickcolor-conversion

Converting colorful pdfs to fax-friendly grayscale pdf (with Imagick)


I am trying to make a colorful pdf conversion to fax-friendly pdf (not using too much ink). How Twilio handles it is very nice. I believe this is achievable with Imagick. My main motivation is doing the conversion with Imagick in my server-side, so that my input and output of Twilio conversion would not be completely different.

I tried these but they don't seem alike to Twilio's one:

$img->quantizeImage(255, Imagick::COLORSPACE_GRAY, 1, TRUE, FALSE);
$img->writeImage('output.png');

// and 
$img->quantizeImage(1, Imagick::COLORSPACE_GRAY, 1, TRUE, FALSE);

Above snippets result in fully color conversion.


Let me show you how Twilio does it:

Original PDF:

enter image description here

Twilio-converted PDF:

enter image description here

and zoomed version of Twilio-converted PDF and the pattern of solid background color:

enter image description here

enter image description here


Solution

  • You did not post the actual PDF, but a PNG image. So I used that. If this does not work, then post a link to your PDF.

    In Imagemagick, you can add a dither pattern. I presume there are equivalent commands in Imagick. Here are the Imagemagick commands.

    convert l9IYF.png -colorspace gray -ordered-dither o3x3 tmp.png
    

    enter image description here

    convert l9IYF.png -colorspace gray -ordered-dither h6x6a tmp2.png
    

    enter image description here

    See https://www.imagemagick.org/Usage/quantize/#ordered-dither and https://www.php.net/manual/en/imagick.orderedposterizeimage.php