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

发布于 2024-12-09 09:00:14 字数 121 浏览 1 评论 0原文

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

When users upload certain files to my site (such as .doc, .xls, .pdf, etc) I'd like to be able to generate a preview thumbnail (of the first page of the document). I'm working with PHP in a LAMP stack but would be happy with any library or command-line tool that can do the job (Linux highly preferred).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身以后 2024-12-16 09:00:14

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

然后您的 php 脚本将调用 OpenOffice,传递上传文档的路径。 OpenOffice 实际上会打开该文档。然后您需要从屏幕缓冲区创建图像。您可以使用 ImageMagick 来完成此操作

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

查看此链接了解更多详细信息

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

It's not easy to convert certain document formats to image. php alone cannot do this.
The 'proper' way to do this is to first of all have the program installed on your server that can open the document in that format.
For example, for .doc documents you can use OpenOffice
it also can open most other document formats
You then need to setup your open office to work in 'headless' mode, sending the output to virtual display (XVFB is what you going to need on Linux)

You php script will then call OpenOffice, passing the path to uploaded doc. OpenOffice will actually open that doc. Then you need to create an image from the screen buffer. You can use ImageMagick for that

Then once you have the capture of your screen you can resize it to a thumbnail.

Look at this link for more details

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

我是有多爱你 2024-12-16 09:00:14

考虑到可以使用的各种可用 API(有些需要订阅),有很多方法可以解决这个问题。如果首选方法是使用本机 PHP 而不依赖第三方应用程序,那么有一些库可以派上用场,例如 PHP Office请注意,它会根据您的 PHP 版本而改变要使用的版本,因为仍可以在网上找到较旧的已弃用版本)。

有很多方法可以做到这一点,这个答案遵循的方法需要 composer 和内置的可用性在 Imagick 扩展PHP以方便使用图书馆。这个答案将涵盖为 Excel、PDF 和 Word 文件仅适用于 PowerPoint 文件处理它的 PHP 库由于缺少 PDF 编写器而在创建缩略图时出现问题,如此StackOverFlow 问题(将 PPT 和 PPTX 转换为 PDF - PHP)

安装 Composer 并确保 PHP 版本中的 Imagick 扩展可用后,运行以下 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 扩展正在相应地读取相关文件。

注意:$im->readImage函数中添加了[0],表示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 文件的限制,目前此答案不包括在内。

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

There are plenty of ways to tackle this issue considering a wide range of available APIs that can be used (some require a subscription). If the preferred approach would be to use native PHP without relying on third-party applications, there are a few libraries that can come in handy such as PHP Office (Note that it varies which version to use based on your PHP version as an older deprecated version can still be found online).

There are many ways to do it, the approach that this answer follows shall require the availability of composer and built-in Imagick extension in PHP to facilitate using the library. This answer shall cover the way to create thumbnails for Excel, PDF, and Word files only as for PowerPoint files the PHP library that handles it has an issue with creating thumbnails due to lack of PDF writer as stated in this StackOverFlow Question (Convert PPT and PPTX to PDF - PHP).

After installing composer and ensuring the availability of Imagick extension within your PHP version, run the below composer codes to install the library using composer (simply go to your project directory and open cmd there):

PHPWord

composer require phpoffice/phpword:dev-master

PHPSPreadsheet

composer require phpoffice/phpspreadsheet

add those lines at the top of the PHP script that shall perform this task:

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;

The approach would be by making a certain tweak by converting all Non-PDF documents to PDF and then utilizing the Imagick PHP extension to create the desired thumbnail.

I have written a function that does that for you. As explained earlier it starts by initiating Imagick object and then creates the thumbnail accordingly based on the input file's extension. Note that this code only requires the path to the file along with the path name without the need to upload anything as the respected files are being read by the library and Imagick extension accordingly.

Note: [0] is added in the $im->readImage function to indicate the first page of the 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!';

The code is pretty straightforward and simple it also can be integrated into whatever project you are working on. Unfortunately, due to limitations with PHPPresentation handling PowerPoint files shall not be covered in this answer as of now.

Hope this helps and that it saved someone's time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文