首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP从办公室文件(.doc、.ppt等)生成JPG

使用PHP从办公室文件(.doc、.ppt等)生成JPG
EN

Stack Overflow用户
提问于 2012-08-02 19:07:44
回答 3查看 18.4K关注 0票数 7

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

是否有一种海峡前进的方式为第一个X页从一个文档生成一个jpgs?然后,我们可以将这些jpgs放在网页中,允许用户预览。

我已经考虑过在服务器上安装开放的office,但是我希望在某个地方有一个php库来完成同样的工作。

有人能帮忙吗?

干杯

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

EN

回答 3

Stack Overflow用户

发布于 2012-08-02 19:16:58

使用com类尝试如下:

您可以使用com类将office文件转换为jpg。

COM类参考:-

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

或下面的代码将ppt转换为jpg格式。

代码语言:javascript
复制
<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();

?>

或将word转换为jpg

代码语言:javascript
复制
<?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;
?>
票数 4
EN

Stack Overflow用户

发布于 2016-02-06 18:42:01

不能使用Office自动执行此类任务,请参阅Microsoft的原因:

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

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

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

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

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2017-11-03 23:16:40

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

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

从PHP调用的代码如下(更多信息在医生里)。

代码语言:javascript
复制
<?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);
?>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11784155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档