Convert image to pdf with compression and mulipage pdf functionality in php - 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.

Related

ImageMagick File is too Big

I'm working with imagemagick to convert images from .tiff to .jpeg and make its thumbanils. The converstion from .tiff into .jpeg is OK but the problem comes when I want use imagemagick to move to other folders with others resolutions and creating its thumbnails. Imagemagick says that image in $path.$destinationPath.$ref.".jpeg" is too big but that image doesn't exist...
-Here it is my code:
exec("/usr/bin/convert ".$tiffPath.$ref.".tiff ".$CommonPath.$sourcePath.$ref.".jpeg");
// Ok until here
if (!file_exists($CommonPath.$destinationPath.$ref.".jpeg"))
{
$executa = "/usr/bin/convert -size 800x800 ".$CommonPath.$sourcePath.$ref.".jpeg -thumbnail 800x800 ".$path.$destinationPath.$ref.".jpeg";
exec($executa);
}
Imagemagick returns the following:
convert: unable to open image $CommonPath.$destinationPath.$ref.".jpeg": File is too big # error/blob.c/OpenBlob/2589.
Thanks in advance!
Your second ImageMagick command is malformed. You have:
$executa = "/usr/bin/convert -size 800x800 ".$CommonPath.$sourcePath.$ref.".jpeg -thumbnail 800x800 ".$path.$destinationPath.$ref.".jpeg";
The -size 800x800 is for creating a new image via xc: or canvas:. It may be confusing the command line to think you have two input images. It is certainly not needed. Try removing it, such as
$executa = "/usr/bin/convert ".$CommonPath.$sourcePath.$ref.".jpeg -thumbnail 800x800 ".$path.$destinationPath.$ref.".jpeg";

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.

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.

Categories