edisonneza/jspdf-invoice-template

Node.js - Image is undefined

okletzmayr opened this issue ยท 3 comments

Hey there, thanks a lot for creating this template! ๐Ÿ‘

My use case is running this on a Node.js server (well, a single script for now ๐Ÿ˜…), and I ran into the issue that Node.js can't create new Image() objects since it's missing the DOM/window references.

I wanted to share the quick fix I applied on my end, it's a bit rough, so I didn't open a PR for it:

  if (param.logo.src) {
    var imageHeader;
    if(typeof window === "undefined") {
      imageHeader = param.logo.src;
    } else {
      imageHeader = new Image();
      imageHeader.src = param.logo.src;
    }
    //doc.text(htmlDoc.sessionDateText, docWidth - (doc.getTextWidth(htmlDoc.sessionDateText) + 10), currentHeight);
    doc.addImage(
      imageHeader,
      10 + param.logo.margin.left,
      currentHeight - 5 + param.logo.margin.top,
      param.logo.width,
      param.logo.height
    );
  }

This enables me to pass either a base64-encoded image, or an Uint8Array in Node.js, essentially exposing the first argument of the doc.addImage() function.

Hi @okletzmayr,
thank you for your suggestion. I'll change it asap.

Just a question, did you run into this issue also?
ReferenceError: Blob is not defined

Yeah, I'm falling back to blob.arrayBuffer() via outputType: OutputType.ArrayBuffer, since Blob is also missing/experimental in Node.js.

Fixed and updated the package to version 1.0.4 for Nodejs.

Thanks for your feedback and let me know if another issue in the future.