PDF to Image with external node.js package

Hi Guys, I am trying to convert a pdf to image using the pdf-image (pdf-image - npm) package. I have installed the dependencies and modified the settings.js file accordingly. No errors during restart. The challenge I have now is that when using the package in a function node I get a "TypeError: PDFImage is not a function" error. Code in function node is only:

var PDFImage = global.get('pdf-image');
var pdfImage = PDFImage(msg.payload);

Any ideas would be much appreciated. Thanks

The readme says...

var PDFImage = require("pdf-image").PDFImage;
 
var pdfImage = new PDFImage("/tmp/slide.pdf");
pdfImage.convertFile().then(function (imagePaths) {
  // [ /tmp/slide-0.png, /tmp/slide-1.png ]
});
 

Depending on how you did the require in settings.js you might need to do something like...

var PDFImage = global.get('pdf-image').PDFImage;
var pdfImage = new PDFImage(... Etc)

What does this show you?

var PDFImage = global.get('pdf-image');
node.warn(PDFImage)

Thanks for the reply Steve, the node.warn(PDFImage) throws no errors. So thats loading correctly. I also understand that I might be trying to use this package incorrectly as I have a buffer or Base64 that I want to convert and not necessarily provide a file location.

Require was done as follows:

functionGlobalContext: {
        PDFImage:require('pdf-image')
 }

Ok, but do you see an object in the debug output containing .PDFImage?

As for using it with a buffer or base64 string, check the repo or ask in the issues.

No object no, it only says "undefined"

image

Then I don't think it is loading.

What version of node-red are you running?

Currently running Node-red v1.3.4

In which case, you can use the new feature functionexternalmodules instead...

https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option

Brilliant thanks. I implemented the functionexternalmodules feature and can see that it loads correctly now:

image

However when trying to use the function I still get the "TypeError: PDFImage is not a constructor" error. only code I have in my function node now is:

var pdfImage = new PDFImage(msg.payload);
return msg;

Where msg.payload is a .pdf buffer. Also tried pointing to a pdf location as in the readme files but get the same error unfortunately

Try...

var pdfImage = new PDFImage.PDFImage(msg.payload);
... Do something with it / read the docs

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.