DEV Community

Cover image for How to convert a PDF page to an image using Nodejs
Gabriella Amaefule
Gabriella Amaefule

Posted on


How to convert a PDF page to an image using Nodejs

Recently, I had to work on a project where I was required to get the the cover page of pdf being sent to the server on each request. It was quite challenging because there were not enough resources to learn how to automate this task. I found a package which helped me, so I decided to share with the community!.

Project setup

RootFolder --
            |-pdfFileFolder --
Enter fullscreen mode Exit fullscreen mode

Install third party library

To effectively carry out this task, we need to install a third party library called pdf-poppler

npm i pdf-poppler
Enter fullscreen mode Exit fullscreen mode


copy the following line of code into the file labelled convert.js

import {default as pdfConverter} from 'pdf-poppler'
import path from 'path'

function convertImage(pdfPath) {

    let option = {
        format : 'jpeg',
        out_dir : 'C:\\Users\\user\\Desktop\\RootFolder\\pdfImageFolder',
        out_prefix : path.basename(pdfPath, path.extname(pdfPath)),
        page : 1
// option.out_dir value is the path where the image will be saved

    pdfConverter.convert(pdfPath, option)
    .then(() => {
        console.log('file converted')
    .catch(err => {
        console.log('an error has occurred in the pdf converter ' + err)


export default convertImage
Enter fullscreen mode Exit fullscreen mode

The convertImage function contains an argument which is the path to the PDF file to be converted to an image. This function with it's argument can be called in any file where needed.

Thank you for taking the time to read this post. If you've found this useful, please give it a ❤️ or 🦄, share and comment.

Top comments (6)

tdnine profile image

Hi, nice article and i followed it - it works fine for jpeg and png, but when i try tiff format it throws up an error. I am on a windows machine, so apparently it should work. The errors i received are

1) Command failed: -tiff -scale-to 1024

2) A list of syntax errors as such with various different fonts
Syntax Error: No display font for 'ArialNarrow'

3) Error writing TIFF header.

I tried with a scale property in options object with value set to 2048 and 4096. But both throwed up same errors as previous with the new scale values in error message.

Any suggestions why would this be happening, anybody else had the similar issue?


ayancoder profile image

pdf-poppler not supported in linux

chaimaa profile image
Chaimaa Zegoumou

you can use pdf-image; provided you install some packages locally

ricky11 profile image
Rishi U

Thanks, great article You should be able to also do this with gm module in node.

jakovglavac profile image

I spent almost one hour trying to convert pdf to image, and this finally worked. Awsome post.

gabriellaamah profile image
Gabriella Amaefule

I am glad it helped

Visualizing Promises and Async/Await 🤓

async await

☝️ Check out this all-time classic DEV post on visualizing Promises and Async/Await 🤓