Imagemagick How to Convert PDF with transparency to PNG - php

I have a project that has makes a PDF for print. I then have it converted to PNG to show back to the client, but the PNG is not retaining the transparency that is in the PDF. I checked the PDF by opening it up in Photoshop and the transparency exists, but it's not converting this data over to the PNG.
The PDF I am using is at: http://mattodesigns.com/PDFs/PNG/testing2.pdf
The page that I am running the conversion on is at: http://mattodesigns.com/PDFs/PNG/pdf2png.html
The PHP Code that I am using to convert the PDF to PNG is:
<?php
$source = 'http://mattodesigns.com/PDFs/PNG/testing2.pdf';
$target = 'testing2.png';
//PNG preview for front
$imagefront = new Imagick();
$imagefront->setResolution( 150, 150 );
$imagefront->readimage($source);
$imagefront->setImageFormat( "PNG32" );
$imagefront->writeImage($target);
?>
This is not working and not sure how I get this to retain the transparency that is in the PDF.I have been looking all over for a solution that is not via a command line and haven't been able to find out. I have also been trying all sorts of various combonation of Imagemagick settings with no luck.
If I need to make a mask, I can do that, but would rather not do that if I don't have to, as all the data is in the PDF itself.

For those who have the same issue, I ended up using GhostScript, following the answer on this topic : http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=14546&start=15
exec("gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 -dFirstPage=1 -dLastPage=1 -sOutputFile=output.png input.pdf");
It looks like ImageMagick is having problems handling semi-transparent PNGs, and GhostScript has much better results.

Related

Merging PDF's using Ghostscript outputs as grayscale / loses color

I have a script which takes a base64 string and converts it into a png image. I then use Imagick to to convert the png into a pdf which uses a AdobeRGB1998 icc profile. This all works as expected and creates a color pdf.
$data = str_replace("data:image/png;base64,","",$_POST["rsa"]);
$save = $path.$_POST["pdfname"].".png";
$imagick = new Imagick();
$decoded = base64_decode($data);
$imagick->readimageblob($decoded);
$imagick->writeImage($save);
$imagick->clear();
$imagick->destroy();
exec("convert $path.$_POST["pdfname"].".png -profile AdobeRGB1998.icc -density 300 ".$path.".$_POST["pdfname"].".pdf", $array);
However, when I then use Ghostscript to merge a bunch of these PDFs together using the below code it outputs in grayscale and I'm not sure why.
function getPDFs($e)
{
return $path.$e['pdffilename'].".pdf ";
}
$fileArray = array_map("getPDFs(", $_POST['item']);
$outputName = $path."LatestDispatch.pdf";
$cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName ";
foreach($fileArray as $file) {
$cmd .= $file." ";
}
$result = shell_exec($cmd);
I switched to using Ghostscript as opposed to imagick to merge pdfs. Imagick returned a merged color pdf but seemed to hit a limit as to how many PDF's imagick was able to merge at once. Ghostscript doesn't have any problems or limits as to how many PDFs it merges so would like to continue with this method as opposed to Imagick, however I need to resolve this issue of PDF's outputting in grayscale, not color. Can anyone help please?
Thanks!
First; Ghostscript doesn't 'merge' PDF files. When you present it with a list of PDF files as input, and use the pdfwrite device to output a single PDF file, it isn't 'merging' the PDF files.
What happens is that each PDF file is fully interpreted to produce a sequence of marking operations, these operations are then passed to the device. For rendering devices they render a bitmap. For PDF output, they are emitted as PDF equivalent operations.
So the content of the output PDF file bears no relation to the content of the input files. The appearance should be the same, but its not 'merging'.
You haven't said what version of Ghostscript you are using, or where you sourced it from (or even what OS you are using, but I guess some flavour of Linux). There's nothing obviously wrong with the command line, but if I were you I'd start by not using a script. Just use Ghostscript manually from the shell to see what happens. If that works, ans the script doesn't, then there's something wrong with the script, and you aren't mimicking it properly from teh shell. Have the script print out the precise command line and try to see if there's some difference in what you typed at the shell.
If it behaves the same, then its puzzling. But without some example to look at, there's little I can advise.

Convert image to pdf with compression and mulipage pdf functionality in php

I have tried to use fpdf,fpdi, tcpdf, htlm2pdf to create pdfs from image files. I can get them all to work but the problem is I cant get them to compress the file with pdf compression. If I have a 5mb image and use any of the above to create the pdf I get a 5mb pdf file. If I create the image using using adobe I can get the pdf down to about 700kb with no noticeable reduction in the image clarity. I have tried the convertapi API and this also gives me a compressed pdf file which is what I want but it can only do single pages. I am also needing to be able to convert multiple images into a single multipage pdf. Anyone other ideas would be highly appreciated.
If you want reduce the size of pdf files, the best way is with Ghostscript:
exec('gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dBATCH -sOutputFile='.$output_file.' '.$file.' ',$output, $return_var);
If you want each page in differents files:
exec('convert -density 300 -trim "'.$file.'" -resize 1000 -quality 85 -colorspace RGB -background white "'.$destination.'" &', $output, $return_var);
Be careful with the & at the end of the $destination, it will name your file "mypdf.pdf" with 5 pages in "mypdf-0.pdf, mypdf-1.pdf..." for each page.

How to convert 10MB PDF to SVG quickly with appropriate size for web

Good day, I have read all questions and answers about "PDF to SVG", "PDF to PNG, PNG to SVG" and had no success in accomplishing my task.
I have a PDF that is about 10mb (blueprints from archiCAD) and after using pdf2svg to convert to SVG, the SVG is about 70mb. Conversion takes 14sec, but the web page load takes about 150sec because of the size of the SVG.
My question is - how can i convert PDF in PHP to SVG with appropriate size for web without quality loss and without gzipping it?
This code converts PDF to SVG that is 70MB:
$fileName = 'in.pdf';
$targetName = 'out.svg';
exec("pdf2svg ".escapeshellarg($fileName)." ".escapeshellarg($targetName));
This one converts PDF to 500kb SVG with HUGE quality loss, some path loss, text loss, etc.
exec("convert -density 200 ".escapeshellarg($fileName)." middle.png");
exec("inkscape middle.png --export-plain-svg=".escapeshellarg($targetName));
Ive tried imagemagick, autotrace, potrace, inkscape, pdf2svg.
I tried the answer from "convert pdf to svg". Other questions (on SO) weren't even close to what I need.
I've read almost every link on Google and almost every topic accomplish my task, but they all start with a with small size PDF so it doesn't solve my problem.
why you want absolutly in SVG?
If you reduce the PDF with Ghostscript, the images will stay like they are, just change resolution.
I've convert PDF in smallers size with ghostscript. It's easiest and faster than with convert ( imagick).
I think you can find in http://www.ghostscript.com/doc/current/Use.htm some parts of the answer.
I use this for reduce PDF in "ebook" quality:
exec('gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dBATCH -sOutputFile='.$outfile.' '.$file.' ',$output, $return_var);

Convert PDF to TIFF, convert from ImageMagick

I need help to convert a PDF file to TIFF file, I do this with PHP and ImageMagick from Linux, like this
exec('convert -density 110 * -compress LZW out.tiff');
But this sometimes fail, so I need to know the best way to convert a PDF to a TIFF, without lost quality, or if someone knows a better way to do this.
Thanks.
Try Ghostscript, here you have an example.
<?php
$outputTiff = "/var/www/html/document.tiff";
$inputPDF = "/var/www/html/document.pdf";
//Execute the ghost script that takes the PDF as an input and saves it as tiff.
$response = exec("gs -SDEVICE=tiffg4 -r600x600 -sPAPERSIZE=letter -sOutputFile=$outputTiff -dNOPAUSE -dBATCH $inputPDF");
?>

PDF Thumbnail always generated as grayscale using Imagick

I am stuck and could really use some help on this one. I am using PHP and Imagick to generate a thumbnail which is working great. However, I noticed that CMYK PDFs are generated always as grayscale. So I tested this by taking the CMYK PDF and manually converting it to RGB with Adobe Acrobat Pro. Then I re-ran it through the following code and it produced a color image. I know about
$image->transformImageColorSpace(1);
or
$image->setImageColorSpace(1);
However this doesn't work. What is the correct way for converting a pdf to a color PNG image? I have looked at the following links with no luck:
http://php.net/manual/en/imagick.setimagecolorspace.php
Convert PDF to JPEG with PHP and ImageMagick
Any help on this one would be great.
Here is the code:
$filePath = fileSaveUserUpload("path/to/file", ""); //path changed here...
$_SESSION['FILEPATH'] = $filePath;
//-------------first makes a thumbnail of first page in image/pdf
$extension_pos = strrpos($filePath, '.'); // find position (number) of the last dot, so where the extension starts
$image = new Imagick();
$image->readImage($filePath."[0]"); //reads an image at a path(first page only in this case)
$image->transformImageColorSpace(1); //convert to RGB
$image->setbackgroundcolor('white'); //replace transparency with this color
$image->setCompression(Imagick::COMPRESSION_LOSSLESSJPEG);
$image->setCompressionQuality(150);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE); //remove transparency
$image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN); //make everything that was transparent white
$image->thumbnailImage(0,250); //max height 300 but try and preserve aspect ratio (wdithxheight)
$thumbnail = substr($filePath, 0, $extension_pos) . '_thumb.png';// . substr($filePath, $extension_pos);
$image->writeImage($thumbnail);
$image->clear();
$image->destroy();
UPDATE:
I am using the following imagick version:
ImageMagick 6.9.1-2 Q16 x86 2015-04-14
3.3.0RC2
GhostScript Version:
9.18
Here is the original PDF (changed it to a picture here):
Here is the thumbnail that it produced:
This ONLY happens with CMYK PDFs. If I take this same PDF and convert it to RGB through adobe acrobat it comes out color. I tested this and it still holds true.
greeting from 2019. was having this problem still on gs 9.26 (9.27 doesnt work at all)
set the colourspace BEFORE loading the file, transform it AFTER.
// setup imagick for colour
$Img = new Imagick();
$Img->SetResolution(200,200);
$Img->SetColorspace(Imagick::COLORSPACE_SRGB);
// now read pdf first page.
$Img->ReadImage("{$File}[0]");
$Img->TransformImageColorSpace(Imagick::COLORSPACE_SRGB);
// the rest of your stuff.
You may try changing:
$image->transformImageColorSpace(1);
to
$image->transformImageColorSpace(Imagick::COLORSPACE_RGB);.
I'm not sure what you are trying to do with the 1, but according to PHP.net, there are predefined colorspaces that can be added to transformImageColorSpace(); to have the image output with the correct colorspace.
The problem you are seeing is likely to be an old version of GhostScript that is not doing the conversion correctly. Below is the image produced when calling your code on a system that has GhostScript version 8.70 installed:
btw you almost certainly want to be using SRGB colorspace, not plain old RGB. SRGB is the correct one to use for displaying images on computer screens.
$image->transformImageColorSpace(\Imagick::COLORSPACE_SRGB);
Here is a command line to test ghostscript by itself doing the conversion:
./gs-916-linux_x86_64 \
-q -dQUIET -dSAFER -dBATCH \
-dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
-dAlignToPixels=0 -dGridFitTT=1 -sDEVICE=pngalpha \
-dFirstPage=1 -dLastPage=3 \
-dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r72 \
-sOutputFile=gs-%d.png tree-blackandwhite.pdf
It should convert the image to a PNG. You'd probably need to replace ./gs-916-linux_x86_64 with just gs.

Categories