Page 1 of 1
Making a colorful pdf fax-friendly
Posted: 2018-01-28T12:04:24-07:00
by senty
I am trying to make a colorful pdf fax-friendly (convert to gray-scale and make the size A4). How Twilio does it for fax is very impressive, and ultimately I am trying to achieve it.
This is the original pdf, ie my input.pdf (please notice that resolution is 120x120):
https://drive.google.com/open?id=1Ppw79 ... NtD3bIqYOn_
When I give the above pdf to Twilio, this is how they convert it (which is very impressive):
https://drive.google.com/open?id=1YdC9e ... EJ-4URsS8P
Using the below code, I achieved something close to Twilio's one, but mine is getting a bit blurry (whereas Twilio's one is crystal sharp).
The output pdf:
https://drive.google.com/file/d/1bZhUuq ... sp=sharing
Code: Select all
$imagick = new \Imagick();
$imagick->readImage($path);
$imagick->resizeImage(595, 842, \Imagick::FILTER_CATROM, 1, true);
$imagick->setBackgroundColor(new \ImagickPixel('white'));
$imagick->quantizeImage(255, \Imagick::COLORSPACE_GRAY, 1, TRUE, FALSE);
$imagick->orderedPosterizeImage('o8x8');
$w = $imagick->getImageWidth();
$h = $imagick->getImageHeight();
$imagick->extentImage ( 595 , 843 , ($w-595)/2, ($h-843)/2 );
$imagick->setImageFormat('pdf');
$imagick->writeImage($endpath);
Re: Making a colorful pdf fax-friendly
Posted: 2018-01-28T12:18:57-07:00
by fmw42
You need to set a large density before reading the input PDF, such as 600. See setImageResolution in Imagick. I gave you the same suggestion one stack overflow.
Re: Making a colorful pdf fax-friendly
Posted: 2018-01-28T12:33:13-07:00
by senty
Yes, I saw your answer on StackOverflow, and all your answers were very helpful, however;
Code: Select all
$imagick->readImage($path);
$imagick->Imagick::setImageResolution( 600, 600 )
$imagick->resizeImage(595, 842, \Imagick::FILTER_CATROM, 1, true);
If I try `$imagick->Imagick::setImageResolution( 600, 600 );`, it ends up generating a PDF in a size of '0.99 x 1.40 in' (so not an A4 size), whereas without that line, the pdf size is "8.26 x 11.71 in" or Twilio's export is "8.50 x 11 in".
What I mean is using 'setImageResolution' manipulates the extending image size
Re: Making a colorful pdf fax-friendly
Posted: 2018-01-28T12:55:23-07:00
by fmw42
Keep the setImageResolution (600,600) before reading the input. But after resizeImage, set the density back to 72 (dpi) or whatever when divided into the pixel size 595x842 will give your desired A4 inches.
See
https://www.imagemagick.org/script/comm ... s.php#page
595/72=8.27
842/72=11.7
You can set it to different densities for Width and Height in setImageResolution so that you get the exact dimension in inches that you want.
The density 600 is needed before reading the PDF so that it renders at high resolution without blurring. The density 72 resets the density for the given pixel dimensions 595*842 so that you get your desired size in inches for the output.
Re: Making a colorful pdf fax-friendly
Posted: 2018-01-28T13:17:41-07:00
by senty
Can you please show me in the snippet where to add it? I couldn't understand 'before reading the input' and 'after resize, set the density back to 72 (dpi)'.
Code: Select all
$imagick->readImage($path);
$imagick->Imagick::setImageResolution( 600, 600 ) // as far as I understand, this is at the right place
$imagick->resizeImage(595, 842, \Imagick::FILTER_CATROM, 1, true);
// but what to do here? how to set it back to 72dpi?
Re: Making a colorful pdf fax-friendly
Posted: 2018-01-28T15:07:08-07:00
by fmw42
$imagick = new \Imagick();
$imagick->Imagick::setImageResolution( 600, 600 );
$imagick->readImage($path);
$imagick->resizeImage(595, 842, \Imagick::FILTER_CATROM, 1, true);
$imagick->setBackgroundColor(new \ImagickPixel('white'));
$imagick->quantizeImage(255, \Imagick::COLORSPACE_GRAY, 1, TRUE, FALSE);
$imagick->orderedPosterizeImage('o8x8');
$w = $imagick->getImageWidth();
$h = $imagick->getImageHeight();
$imagick->extentImage ( 595 , 843 , ($w-595)/2, ($h-843)/2 );
$imagick->Imagick::setImageResolution( 72, 72 );
$imagick->setImageFormat('pdf');
$imagick->writeImage($endpath);
Re: Making a colorful pdf fax-friendly
Posted: 2018-02-10T07:07:23-07:00
by atariZen
Is that PERL? I can't read it. Would someone please post the answer in commandline format?
Thanks!
Re: Making a colorful pdf fax-friendly
Posted: 2018-02-10T11:30:56-07:00
by fmw42
The code above is for PHP Imagick.
Please identify your IM version and platform, since syntax may differ. Also post an example PDF to some free hosting service such as dropbox.com that will not change the format (or zip the PDF). The explain exactly what you want to do relative to your input PDF regarding background, output size, format, etc.
See the top-most post in this forum "IMPORTANT: Please Read This FIRST Before Posting" at
http://www.imagemagick.org/discourse-se ... f=1&t=9620
For novices, see
http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/