Convert PDF to TIFF, convert from ImageMagick - php

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

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

Imagemagick How to Convert PDF with transparency to PNG

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.

Convert PDF to JPG image with PHP

I am using ImageMagik to try and convert the contents of a PDF to JPG, but keep getting an empty jpg. I have made sure the perms are 777 on everything for testing so I am a little lost how to continue.
Here is the script I am running
<?php
exec('convert testfile.pdf output.jpg', $output, $return_var);
?>
Try this.
<?php
$pdf = 'testfile.pdf';
$save = 'output.jpg';
exec('convert "'.$pdf.'" -colorspace RGB -resize 800 "'.$save.'"', $output, $return_var);
?>
Use the absolute path to the binary, like this:
exec('/usr/bin/convert testfile.pdf output.jpg', $output, $return_var);
But make sure your convert binary is actually on /usr/bin you can check that out with the following command:
which convert
convert -normalize yourfile.pdf[0] yourdestination.jpg
ImageMagick internally use GhostScript and Generally the conversion of ImageMagick is slow Comparing to Ghoastscript, so If you are only interested on getting convert pdf to images then Ghostscript gs command is faster.
below is an sample wrapper around Ghostscript which I wrote few days back.
PDFLib-Php
$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->setImageQuality(95);
$pdflib->setDPI(300);
$pdflib->setPageRange(1,$pdflib->getNumberOfPages());
$pdflib->convert();
Here you have my solution. Use Imagick directly in your php code.
Convert all PDF pages to JPG
// create Imagick object
$imagick = new Imagick();
// Reads image from PDF
$imagick->readImage('file.pdf');
// Writes an image
$imagick->writeImages('converted.jpg', false);
Convert specific PDF page to JPG
// create Imagick object
$imagick = new Imagick();
// Read image from PDF
$imagick->readImage('test.pdf[0]');
// Writes an image
$imagick->writeImages('converted_page_one.jpg');
Another way to deal with this problem is to use spatie/pdf-to-image library.
Cheers!

Categories