在 PHP (LAMP) 中创建文档(PDF、DOC、XLS 等)的缩略图预览

问题描述 投票:0回答:3

当用户将某些文件上传到我的网站时(例如 .doc、.xls、.pdf 等),我希望能够生成(文档第一页的)预览缩略图。我在 LAMP 堆栈中使用 PHP,但对可以完成这项工作的任何库或命令行工具都很满意(Linux 是首选)。

php linux graphics lamp
3个回答
9
投票

将某些文档格式转换为图像并不容易。仅靠 php 无法做到这一点。 执行此操作的“正确”方法是首先在您的服务器上安装可以打开该格式文档的程序。 例如,对于 .doc 文档,您可以使用 OpenOffice 它还可以打开大多数其他文档格式 然后你需要设置你的开放式办公室以“无头”模式工作,将输出发送到虚拟显示器(XVFB 是你在 Linux 上需要的)

然后您的 php 脚本将调用 OpenOffice,将路径传递给上传的文档。 OpenOffice 将实际打开该文档。然后你需要从屏幕缓冲区创建一个图像。你可以为此使用 ImageMagick

然后,一旦您捕获了屏幕,就可以将其调整为缩略图。

查看此链接了解更多详情

http://www.mysql-apache-php.com/website_screenshot.htm


0
投票

最好的方法是将所有文档转换为PDF 之后你可以制作预览缩略图

&这就是简单的解释 如何在 PHP 中将 PDF 文档转换为预览图像?


0
投票

考虑到可以使用的各种可用 API(有些需要订阅),有很多方法可以解决这个问题。如果首选方法是在不依赖第三方应用程序的情况下使用本机

PHP
,则有一些库可以派上用场,例如PHP Office请注意,根据您的 PHP 使用哪个版本作为较旧的已弃用版本的版本仍然可以在网上找到)。

有很多方法可以做到这一点,这个答案遵循的方法应该需要composer和内置的

Imagick
扩展
PHP
中的可用性,以方便使用库。该答案应仅涵盖为 Excel、PDF 和 Word 文件创建缩略图的方法,至于 PowerPoint 文件,处理它的
PHP
库由于缺少 PDF 编写器而在创建缩略图方面存在问题,如前所述在这个 StackOverFlow 问题中(将 PPT 和 PPTX 转换为 PDF - PHP).

安装 composer 并确保

Imagick
扩展在您的
PHP
版本中可用后,运行以下 composer 代码以使用 composer 安装库(只需转到您的项目目录并在那里打开 cmd):

PHPWord

composer require phpoffice/phpword:dev-master

PHPSPreadsheet

composer require phpoffice/phpspreadsheet

在执行此任务的

PHP
脚本顶部添加这些行:

require_once '../vendor/autoload.php'; // Calls Composer

use PhpOffice\PhpSpreadsheet\IOFactory as SpreadsheetIOFactory;;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf as excelMPDF;
use \PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use \PhpOffice\PhpSpreadsheet\Style\Fill;

use \PhpOffice\PhpWord\IOFactory as wordIOFactory;
use PhpOffice\PhpWord\Writer\Pdf\Mpdf as wordMPDF;

方法是通过将所有非 PDF 文档转换为 PDF,然后利用

Imagick
PHP 扩展来创建所需的缩略图。

我已经为你写了一个函数。如前所述,它首先启动

Imagick
对象,然后根据输入文件的扩展名相应地创建缩略图。请注意,此代码只需要文件的路径以及路径名,而无需上传任何内容,因为库正在读取相关文件并相应地扩展
Imagick

注:

[0]
$im->readImage
函数中加入,表示PDF的第一页

$im = new Imagick();
$im->setResolution(600, 600);
if($ext == 'pdf'){
    $im->readImage($pf . '[0]');
} else if ($ext == 'xls' || $ext == 'xlsx') {
   $spreadsheet = SpreadsheetIOFactory::load('path/to/file.xlsx');
   $spreadsheet->getActiveSheet()->getParent()->getDefaultStyle()->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID);
   $spreadsheet->getActiveSheet()->getParent()->getDefaultStyle()->getFill()->getStartColor()->setARGB('FFFFFFFF');
   // Create a new PDF writer using mPDF
   $writer = new excelMPDF($spreadsheet);   
   // Set the output file path
   $outputFilePath = 'path/filename.pdf';
   // Write the PDF to the output file path
   $writer->save($outputFilePath);
   $im->readImage('path/filename.pdf'. '[0]');
} else if ($ext == 'doc' || $ext == 'docx') {
     // Load the Word document
    $phpWord = wordIOFactory::load('path/to/file.docx');
    // Set up the PDF writer
    $writer = new wordMPDF($phpWord);   
    // Set the output file path
    $outputFilePath = 'path/filename.pdf';

   // Write the PDF to the output file path
   $writer->save($outputFilePath);
   $im->readImage('path/filename.pdf'. '[0]');
}
// Set the background color to white
$im->setImageBackgroundColor('#FFFFFF');
$im->setImageFormat('jpg');
$im->setImageFilename('image_name.jpg');
$fileHandle = fopen('path/image_name.jpg', "w");
$im->writeImageFile($fileHandle);
$output = $im->getimageblob();
$outputtype = $im->getFormat();
header('Content-Type: ' . $outputtype);
$im->destroy(); 

echo 'Thumbnail Created!';

代码非常简单明了,它也可以集成到您正在处理的任何项目中。不幸的是,由于 PHPPresentation 处理 PowerPoint 文件的限制,目前本答案不包括在内。

希望这会有所帮助,并节省了某人的时间。

© www.soinside.com 2019 - 2024. All rights reserved.