生成JPG';s来自使用PHP的办公文件(.doc.ppt等)


Generating JPG's from office files (.doc .ppt etc) using PHP

我正在构建一个应用程序,人们可以在其中上传文件并与其他人共享。我们希望做的一部分是允许人们在线预览文件。

有没有一种简单的方法可以为文档的前X个页面生成jpg?然后我们可以将这些jpg放在网页中,让用户预览。

我曾考虑在服务器上安装开放式办公室,但希望有一个php库可以做同样的工作。

有人能帮忙吗?

干杯


顺便说一句,不一定是jpg,任何图像文件都可以(实际上甚至是pdf也可以)

使用com类试试这个:

您可以使用com类将办公文件转换为jpg

COM类参考:-

http://us2.php.net/manual/en/class.com.php

或以下代码是将ppt转换为jpg格式的

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

?>

或将单词转换为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;
?>

您不能使用Office Interop来自动执行这样的任务,请参阅Microsoft的原因:

https://support.microsoft.com/en-us/kb/257757

最好的方法是使用功能强大的库,如Aspose.Slides(与ppt、pptx、强大的操作兼容),这些库被设计为用作API。

您可以通过NetPhp库使用PHP中的Aspose.Slides。这里有一个例子:

http://www.drupalonwindows.com/en/blog/powerpoint-presentation-images-php-drupal-example

相关的代码就是这一段,它有一些Drupal特定的东西,但你可以看到它是如何进行的,并使它在其他地方工作:

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

对于特定于PHP的选项,您可以使用PHPWord-此库是用PHP编写的,提供了从不同文档文件格式(包括.doc.docx)读取和写入的类,但它无法从所有Office文件进行转换。

要在任何平台上转换任何Office文件,您可以使用Zamzar等文件转换API。它可以从所有Office格式(DOC/DOCX/PPT/PPTX/XLS/XLSX)转换为图像(JPG、PNG、GIF等)和PDF。

从PHP调用的代码如下(更多信息请参阅文档)。

<?php
// Build request
$endpoint = "https://api.zamzar.com/v1/jobs";
$apiKey = "YOUR_KEY";
$sourceFilePath = "/tmp/my.doc"; // Or DOCX/PPT/PPTX/XLS/XLSX
$targetFormat = "jpg";
$sourceFile = curl_file_create($sourceFilePath);    
$postData = array(
  "source_file" => $sourceFile,
  "target_format" => $targetFormat
);
// Send request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $apiKey . ":");
$body = curl_exec($ch);
curl_close($ch);
// Process response (with link to converted files)
$response = json_decode($body, true);
print_r($response);
?>