I'm building an application where people can upload files and share them with other people. Part of what we are looking to do is allow people to preview the file on-line.
Is there a strait forward way to generate a jpgs for the first X amount of pages from a document? We could then place these jpgs in the web page allowing the user to preview.
I have looked at installing open office on the server but was hoping there was a php library somewhere that does the same job.
Can anybody help?
Cheers
Btw, doesnt have to be jpg, any image file would be fine (actually even pdf would be ok)
Try this with com class:
You can use com class for convert office file to jpg
COM class Reference: -
http://us2.php.net/manual/en/class.com.php
or below code is convert ppt to jpg format
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
$ppApp = new COM("PowerPoint.Application");
$ppApp->Visible = True;
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
$ppName = "MySlides.ppt";
$FileName = "MyPP";
//*** Open Document ***//
$ppApp->Presentations->Open(realpath($ppName));
//*** Save Document ***//
$ppApp->ActivePresentation->SaveAs($strPath."/".$FileName,17); //'*** 18=PNG, 19=BMP **'
//$ppApp->ActivePresentation->SaveAs(realpath($FileName),17);
$ppApp->Quit;
$ppApp = null;
?>
PowerPoint Created to Folder <b><?=$FileName?></b>
</body>
</html>
---------------------------
Or try this :-
$powerpnt = new COM("powerpoint.application") or die("Unable to instantiate Powerpoint");
$presentation = $powerpnt->Presentations->Open(realpath($file), false, false, false) or die("Unable to open presentation");
foreach($presentation->Slides as $slide)
{
$slideName = "Slide_" . $slide->SlideNumber;
$exportFolder = realpath($uploadsFolder);
$slide->Export($exportFolder."\\".$slideName.".jpg", "jpg", "600", "400");
}
$powerpnt->quit();
?>
or convert word to jpg
<?php
// starting word
$word = new COM("word.application") or die("Unable to instantiate Word");
echo "Loaded Word, version {$word->Version}\n";
//bring it to front
$word->Visible = 1;
//open an empty document
$word->Documents->Add();
//do some weird stuff
$word->Selection->TypeText("This is a test...");
$word->Documents[1]->SaveAs("Useless test.doc");
//closing word
$word->Quit();
//free the object
$word = null;
?>
You cannot use Office Interop to automate such a task, see Microsoft reasons for that here:
https://support.microsoft.com/en-us/kb/257757
The best approach is to use a powerful library such as Aspose.Slides (compatibility with ppt, pptx, powerful manipulation) that are designed to be used as an API.
You can consume Aspose.Slides from PHP by means of the NetPhp library. There is an example here:
http://www.drupalonwindows.com/en/blog/powerpoint-presentation-images-php-drupal-example
The relevant piece of code is this one, it has some Drupal specific stuff, but you can see how it goes and make it work on other places:
protected function processFilePowerpoint(array $file, array &$files) {
/** @var \Drupal\wincachedrupal\NetPhp */
$netphp = \Drupal::service('netphp');
$runtime = $netphp->getRuntime();
$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");
$destination = strtr(PresentacionSlide::UPLOAD_LOCATION, ['[envivo_presentacion:id]' => $this->entity->id()]);
file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$sourcefile = drupal_realpath($file['tmppath']);
$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) {
$x++;
$bitmap = $slide->GetThumbnail(1, 1);
$destinationfile = $destination . "\\slide_{$x}.png";
$bitmap->Save(drupal_realpath($destinationfile), $format);
$files[] = PresentacionSlide::fromFile($destinationfile);
}
$presentation->Dispose();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With