'Using pdf.js on a node server

I want to convert a pdf to an image server-side, using node.js. My input for this task is pdf's url, and the desired output is a base64 string, representing an image.

I've decided to try pdf.js (https://github.com/mozilla/pdf.js) and node-canvas (https://github.com/Automattic/node-canvas) - my plan is to read the pdf, render it to canvas and get image's base64 from the canvas.

But pdf.js plays up server-side, I create a get document task, as described in examples:

import pdfjs from 'pdfjs-dist';

const t = pdfjs.getDocument('http://cdn.mozilla.net/pdfjs/helloworld.pdf');
  t.promise.then(function (doc) {
    console.log('got doc');
    console.log(doc);
  })
  .catch(err => {
    console.log(err);
  });

But nothing just happens. Promise neither resolves, nor rejects. How can I fix that and make it work? What am I doing wrong?

Maybe there's another solution, that would allow me to get converted image's base 64 without storing it to the filesystem (all pdf to image converters for node I've seen so far save images to drive, but that's not the desired behaviour for me)?



Solution 1:[1]

Try this piece of Code:

https://github.com/mozilla/pdf.js/blob/master/examples/node/pdf2png/pdf2png.js

Try using Async/Await inside Async function

const doc = await pdfjs.getDocument('http://cdn.mozilla.net/pdfjs/helloworld.pdf').promise;

const page = await doc.getPage(1);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Soviut