Blog

Back to blog posts

PHP – Generate thumbnail image of PDF or Office file

Published Jul 14, 2021

Intro

If you are writing php code, you might be tasked with generating an image preview or thumbnail image of a PDF or Microsoft Office file like Word, Powerpoint, Excel, or even email files. If you ended up on this article you probably have been doing research for hours and getting nowhere. Maybe you ended up down a rabbit hole of looking up Libreoffice and docker containers and who knows what. Your frustration is mounting. Totally get it. Thankfully we created a REST API for you to solve this for you in minutes.

Our API will take your PDF, docx, xlsx, pptx, eml, msg, or any other file that can be reasonable opened by LibreOffice and take an image of the first page. So if you have a multi-page file, it will only grab the first page as an image. This is incredibly useful for those building galleries of image previews for files.

Generate thumbnail image of PDF or Office file with PHP

Step 1) Install the PHP client library from github: https://github.com/Api2Pdf/api2pdf.php

Step 2) Grab an API key from https://portal.api2pdf.com. Only takes 60 seconds.

Step 3) Use the sample code below and replace “YOUR-API-KEY” with the api key you acquired in step 2.

require_once 'your-own-directory/Api2Pdf.php';
$apiClient = new Api2Pdf('YOUR-API-KEY'); 

$result = $apiClient->libreOfficeThumbnail('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx');
echo $result->getFile();

And that’s it! Modify the code as you see fit. Hopefully this saves you time and makes generating thumbnails of PDFs and office files easy and painless for those writing php code.

See full github library

We have a whole php based client library for our API that does a lot more than just this. Check out the full library capabilities here: https://github.com/Api2Pdf/api2pdf.php