Create a thumbnail preview of documents (PDF, DOC, XLS, etc.) in PHP (LAMP)

Dan picture Dan · Oct 10, 2011 · Viewed 15.2k times · Source

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).

Answer

Dmitri Snytkine picture Dmitri Snytkine · Oct 12, 2011

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