JsPdf throwing errors when trying to add PNG image to pdf document

By On

I am able to add a jpg image to a pdf document using insert image but for some reason the code throws an error when I try to add a png image. Why would this be failing? This is the error that I see getting thrown

Error in function n/At: String contains an invalid character DOMException: "String contains an invalid character"

The code I am using is fairly basic from the examples I have found online.

img.onload = function() {
 var width = pdfdoc.internal.pageSize.width;
 var ratio = width / this.naturalWidth;
 var height = this.naturalHeight * ratio;
 var padding = 15;//jQuery("#docpadding").val();
 var yPadding = 25;
 pdfdoc.addImage(this, "PNG", padding, yPadding, width - (padding*2), height);

1 Answer

- Advertisment -
Answered By Chris Evans On

Have you enabled png support through the libraries? If you check the latest code in the github repo, you will see there is a png_support.js file in the repo https://github.com/MrRio/jsPDF/tree/master/src/modules . You need to include this script as part of your project if you want to support PNG files.

You will also need to include png.js and zlib.js from https://github.com/MrRio/jsPDF/tree/master/src/libs if you want the png support to work without errors.