Blog

Back to blog posts

Node / Javascript – Generate thumbnail image of PDF or Office file

Published Jul 14, 2021

Intro

If you are writing node / javascript 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.

Take a screenshot of a website with Node / Javascript

Step 1) Open up your package manager and run the command

npm install –save api2pdf

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.

var Api2Pdf = require('api2pdf');   
var a2pClient = new Api2Pdf('YOUR-API-KEY');

a2pClient.libreOfficeThumbnail('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx').then(function(result) { console.log(result); });

And that’s it! Modify the code as you see fit. Hopefully this saves you time and makes generating thumbnail images of PDFs and office fileseasy and painless for those writing node / javascript code.

See full github library

We have a whole nodejs 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.node