使用PHP从办公文件(.doc .ppt等)生成JPG的方法

7
我正在开发一个应用程序,人们可以上传文件并与其他人分享。我们的目标之一是允许人们在线预览文件。
有没有一种简单明了的方法可以为文档的前X页生成JPGs?然后我们可以将这些JPG放置在网页中,让用户预览。
我已经尝试在服务器上安装OpenOffice,但希望能够找到一个在PHP库中完成相同工作的解决方案。
有人可以提供帮助吗?
顺便说一下,不一定非得是JPG,任何图像文件都可以(实际上PDF也可以)。

我认为如果没有任何将它们转换为文本或PHP扩展的方法,这是不可能的!如果有任何方法,我很乐意听听。 - user1350140
嗨,马特,它不必是JPG格式...任何图像文件都可以。 - Chris Headleand
3个回答

4

请尝试使用COM类:

您可以使用COM类将Office文件转换为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;
?>

3
如果这个解决方案成功,它将需要基于Windows的服务器! - user1350140
@Abid Hussain 我认为这并不全对,据我记得,COM 仅限于 Windows。你会发现 Com 类被归类为仅限于 Windows 的扩展。请参考 http://us2.php.net/manual/en/refs.utilspec.windows.php。 - user1350140
1
难以置信没有人给这个答案投票。在Windows机器上测试了代码(显然),两个选项都像魔法一样运行良好。干得好,Abid!附注:对于任何阅读此内容的人,您还需要在计算机上安装PowerPoint。 - Ben
我不明白“sample”这个词怎么会产生JPG格式的文件。 - jossif
转换Word为JPG的功能无法正常工作。它只能创建新的文档文件,而不能生成JPG格式的文件。请问有什么建议吗? - J.Rob
显示剩余3条评论

1

您不能使用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();
  }

-1

对于 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);
?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接