I need to convert single Powerpoint (PPT) slides/files to JPG or PNG format on linux but haven't found any way of doing so successfully so far. I have heard that it can be done with open office via php but haven't found any examples or much useful documentation. I'd consider doing it with python or java also, but I'm unsure which route to take.
I understand that it can be done using COM on a Windows server but would really like to refrain from doing so if possible.
Any ideas/pointers gratefully received. (And yes, I have searched the site and others before posting!)
Thanks in advance,
Rob Ganly
Quick answer (2 steps):
## First converts your presentation to PDF
unoconv -f pdf presentation.ppt
## Then convert your PDF to jpg
convert presentation.pdf presentation_%03d.jpg
And voilá.
Explaning a little more:
I had already follow in the same need. Convert a powerpoint set of slides into a set of images. I haven't found one tool to exactly this. But I have found unoconv which converts libreoffice formats to other formats, including jpg, png and PDF. The only drawback is that unoconv only converts one slide to a jpg/png file, but when converting to PDF it converts the whole presentation to a multiple page PDF file. So the answare were convert the PPT to PDF and with imagemagick's convert, convert the multiple page PDF to a set of images.
Unoconv is distributed within Ubuntu distribution
apt-get install unoconv
And convert is distributed with the imagemagick package
apt-get install imagemagick
In my blog there is an entry about this
This can be done from PHP using a 3d party library (Aspose.Slides). It will work on both .ppt and .pptx, and it's lightning fast.
Here is the relevant piece of code in PHP:
$runtime->RegisterAssemblyFromFile("libraries/_bin/aspose/Aspose.Slides.dll", "Aspose.Slides");
$runtime->RegisterAssemblyFromFullQualifiedName("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing");
$sourcefile = "D:\\MYPRESENTATION.ppt";
$presentation = $runtime->TypeFromName("Aspose.Slides.Presentation")->Instantiate($sourcefile);
$format = $runtime->TypeFromName("System.Drawing.Imaging.ImageFormat")->Png;
$x = 0;
/** #var \NetPhp\Core\NetProxyCollection */
$slides = $presentation->Slides->AsIterator();
foreach ($slides as $slide) {
$bitmap = $slide->GetThumbnail(1, 1);
$destinationfile ="d:\\output\\slide_{$x++}.png";
$bitmap->Save($destinationfile, $format);
}
$presentation->Dispose();
It does not use Office Interop (which is NOT recommended for server side automation) and is lightining fast.
You can control the output format, size and quality of the images. Indeed you get a .Net Bitmap object so you can do with it whatever you want.
The original post is here:
http://www.drupalonwindows.com/en/blog/powerpoint-presentation-images-php-drupal-example
Related
I'm trying now to convert a PDF file to JPG, using ImageMagick with PHP and CakePHP. The PDF is in perfect shape and it's right the way it should be, but the image generated from the PDF is always overflowing the borders of the file.
Until now, I've tried tweaking the code for the generation with no sucess, reading a lot from the PHP docs (http://php.net/manual/pt_BR/book.imagick.php).
Here are the convertion code:
$image = new Imagick();
$image->setResolution(300,300);
$image->setBackgroundColor('white');
$image->readImage($workfile);
$image->setGravity(Imagick::GRAVITY_CENTER);
$image->setOption('pdf:fit-to-page',true);
$image->setImageFormat('jpeg');
$image->setImageCompression(imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(60);
$image->scaleImage(1200,1200, true);
$image->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);
$image->writeImage(WWW_ROOT . 'files' . DS . 'Snapshots' . DS . $filename);
Here are the results:
https://imgur.com/a/ISBmDMv
The first image is the PDF before the conversion and the second one, the image generated from the PDF where the right side text overflows.
So, why this is happening? And if someone got some alternative for any tech used (the GhostScript, ImageMagick, etc) is also welcome!
Thanks everyone!
Its very hard to say why you see the result you do, without seeing the original PDF file, rather than a picture of it.
The most likely explanation is that your original PDF file uses a font, but does not embed that font in the PDF. When Ghostscript comes to render it to an image it must then substitute 'something' in place of the missing font. If the metrics (eg spacing) of the substituted font do not match precisely the metrics of the missing font, then the rendered text will be misplaced/incorrectly sized. Of course since its not using the same font it also won't match the shapes of the characters either.
This can result in several different kinds of problems, but what you show is pretty typical of one such class of problem. Although you haven't mentioned it, I can also see several places in the document where text overwrites as well, which is another symptom of exactly the same problem.
If this is the case then the Ghostscript back channel transcript will have told you that it was unable to find a font and is substituting a named font for the missing one. I can't tell you if Imagemagick stores that anywhere, my guess would be it doesn't. However you can copy the command line from the ImagMagick profile.xml file and then use that to run Ghostscript yourself, and then you will be able to see if that's what is happening.
If this is what is happening then you must either;
Create your PDF file with the fonts embedded (this is good practice anyway)
Supply Ghostscript with a copy of the missing font as a substitute
Live with the text as it is
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.
I am generating PNG file with cairo extension of PHP. The image contains a background and a text. Now I want to compress these images by PHP after its generated by cairo. Is there any library to do this?
I found pngcrush tool. But its a command line tool. I dont want to invoke system call. If there is not PHP solution a C solution would do. In that case I'll make a PHP extension.
I have read this related question. But there is no answer in it.
You can use imagepng() ...
//If you don't already have a handle to the image and it's just on the file system...
$im = imagecreatefrompng("yourGenerateFile.png");
$quality = 5; //0 - 9 (0= no compression, 9 = high compression)
imagepng($im, 'file/to/save.png', $quality); //leave out filename if you want it to output to the buffer
imagedestroy($im);
I would take a look at PngOptimizer. You can get the source for it at the bottom of the page, and it has a separated CLI version too.
Only problem is that source is C++ , not ANSI C. I have never made a PHP extension, so i don't know if it makes a difference.
For C code take a look at ImageMagick. It looks like there is a PHP extension too.
have anyone come across a php code that convert text or doc into pdf ?
it has to follow the same format as the original txt or doc file meaning the line feed as well as new paragraph...
Converting from DOC to PDF is possible using phpLiveDocx:
$phpLiveDocx = new Zend_Service_LiveDocx_MailMerge();
$phpLiveDocx->setUsername('username')
->setPassword('password');
$phpLiveDocx->setLocalTemplate('document.doc');
// necessary as of LiveDocx 1.2
$phpLiveDocx->assign('dummyFieldName', 'dummyFieldValue');
$phpLiveDocx->createDocument();
$document = $phpLiveDocx->retrieveDocument('pdf');
file_put_contents('document.pdf', $document);
unset($phpLiveDocx);
For text to PDF, you can use the pdf extension is PHP.
You can view the examples here.
Have a look at this SO question. Using OpenOffice in command line mode for conversions can be done, though you'd have to search a bit for the conversion macro's. I'm not saying it's light-weight though :)
See HTML_ToPDF. It also works for text.
It has been a long time since I touched PHP, but if you can make web service calls from it then try this product. It provides excellent conversion fidelity. It also supports additional formats including Infopath, Excel, PowerPoint etc as well as Watermarking support.
Please note that I have worked on this product so the usual disclaimers apply.
We recently installed the latest version of ImageMagick onto our Linux server. I seem to be having issues performing the most basic of tasks.
I am running this command line:
/usr/bin/convert /location/to/source/design.ai /location/to/save/output.jpg
Unfortunatly is saves design.jpg as an illustrator file (if I rename the file to output.ai it opens). Even if I do this:
/usr/bin/convert /location/to/source/design.ai -rotate 90 /location/to/save/design.jpg
It rotates the file and saves again as an illustrator document. This happens with all filetypes (e.g. png, bmp, etc...)
It appears ImageMagick cannot figure out what I want it converted to and just saves as the same file type.
Any ideas on fixing this?
Regards:
John
(Yes, McKay is properly right. This question would be better placed at serverfault.)
But I have an idea. By doing 'convert' only one gets a hint at the bottom:
To specify a particular image format, precede the filename
with an image format name and a colon (i.e. ps:image) or specify the
image type as the filename suffix (i.e. image.ps).
Perhaps convert gets confused by the path given.
So you could try this:
convert /location/to/source/design.ai output.jpg
or
convert /location/to/source/design.ai jpg:/location/to/save/output.jpg
Regards
Sigersted