admin管理员组

文章数量:1355654

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 (.js) and 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('.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)?

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./mozilla/pdf.js) and node-canvas (https://github./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/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)?

Share Improve this question asked Jun 13, 2017 at 11:28 tristantzaratristantzara 5,9276 gold badges28 silver badges47 bronze badges 4
  • 1 PDF.js does not know node.js I/O apis -- download pdf yourself and use getDocument({data: downloadedData}) to open PDF – async5 Commented Jun 13, 2017 at 12:57
  • 4 See github./mozilla/pdf.js/tree/master/examples/node/pdf2png – async5 Commented Jun 13, 2017 at 18:15
  • @async5 Thank you very much! that helps. Sorry to bother you, but I have a very similar-looking situation but with the render api now - stackoverflow./questions/44800345/… perhaps, you have some ideas? (node-canvas itself is working ok, I've checked) – tristantzara Commented Jun 28, 2017 at 11:16
  • @async5 - it appeared that while pdf.js has an option to render to image in node, it's very limited - I've faced a lot of issues where it didn't render all the text (because it needs canvas and simulated canvas for node doesn't quite do everything that pdf.js requires). Very unreliable approach if you consider using it in a production environment. – vir us Commented Mar 7, 2020 at 18:46
Add a ment  | 

1 Answer 1

Reset to default 2

Try this piece of Code:

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

Try using Async/Await inside Async function

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

const page = await doc.getPage(1);

本文标签: javascriptUsing pdfjs on a node serverStack Overflow