Jspdf: Saving PDF with image fails

Created on 1 Mar 2016  Â·  15Comments  Â·  Source: MrRio/jsPDF

Create a ticket with an image, it will fail on the following line:

var datauri = ('' + type).substr(0, 6) === 'dataur'
                    ? 'data:application/pdf;base64,' + btoa(buildDocument()) : 0;

With the error:

Error: Error in function Error (native): Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
    at Console.$window.console.error (http://localhost:3333/App/Business/AngularErrorHandler.js:43:23)
    at Object.__safeCallWrapper [as output] (http://localhost:3333/Scripts/jsPDF/jspdf.js:471:44)

Most helpful comment

Same problem here. Only fails on png's for me.

All 15 comments

Can you try get an example running – http://jsbin.com/rilace/edit?html,js,output

Yes, here it is:
http://jsbin.com/holakawuho/1/edit?html,js,output

Strangely, that works for me on mobile Chrome on iOS, but not on desktop

Odd thing is that .save() works, but .output() doesn't

Thanks for that. Yeah that is odd. git-bisect time!

I'm seeing the same thing on Windows with latest Chrome and Firefox. Any word on a fix?

As an example, here's the image data I'm trying to add:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAEZUlEQVR4nOXbXagVVRgG4Oeco3lOaj+GZmlBafRDxS4itAiiKIiiiyK9EKKoiIQg+iOwoovKCgKFIqggCiMkshNEFF4kFRQFFobYH1LRD2JKGpVinuliuWm7z96zZ2avmdnaC+/Vmfm+7117Zr3rW2sO/x/cgO+RtPCjOguqCg1scLDwJvfUVlUFmI3nsV9n8Qm+ra26EjEV9+B33YU3OV5LhSXianytt/AmH6unzPg4E+/KLrzJZXUUGxPHYBX2yS8+ESbIQxIjuAPbFROeCJPjaNWFx8Bl2KS48EPWAU7FOv0LP+QcYAZWCouWrOKyXDvwDjCEm/CLfL/sBtnWAAPtAIvxqfyP9ZP4IuO1jYq05MJ8rMGEfML34kaszXj9wDnAKB7Cn/L/6jtxKVbkuGegHGCJyW1qVm7FGbhWvqdmvAJdPXEePlBMeIJPMAdnYXfOe2t1gDl4QXqb2otvYAyz8F2B+2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Data, "PNG", 0, 0, imageWidth, imageHeight);

Same problem here. Only fails on png's for me.

I've reverted to 1.0.272 until this is addressed.

Same problem too. Fail with canvas having a drawn image then masked by a rounded rectangle. The Firefox error is more verbose:

Error in function n/At: String contains an invalid character DOMException [InvalidCharacterError: "String contains an invalid character"
code: 5
nsresult: 0x80530005
location: ***/jspdf.min.js:279]

See also: WindowBase64.btoa().

Some progress with this issue?
If you replaced in the jspdf.min.js file:
btoa(vt())
to:
btoa(unescape(encodeURIComponent(vt())))

The file is generated correctly, but the images are not seen.

trying to render charts and pies here, same problem.
I could've live with save() if only png was better than jpeg, in the rendered pdf document

Same issue here. JPG works fine, PNG fails.

I have images (logos) saved on an external domain. Some are png, some are jpeg. To work around this issue, I wrote the following function. It takes an image url and converts it to jpeg data uri.

    getDataUri(imgUrl, function(dataUri) {
        logo = dataUri;
        console.log("logo=" + logo);
    });

    getDataUri(url, cb)
     {
            var image = new Image();
            image.setAttribute('crossOrigin', 'anonymous'); //getting images from external domain

            image.onload = function () {
                var canvas = document.createElement('canvas');
                canvas.width = this.naturalWidth;
                canvas.height = this.naturalHeight; 

                //next three lines for white background in case png has a transparent background
                var ctx = canvas.getContext('2d');
                ctx.fillStyle = '#fff';  /// set white fill style
                ctx.fillRect(0, 0, canvas.width, canvas.height);

                canvas.getContext('2d').drawImage(this, 0, 0);

                cb(canvas.toDataURL('image/jpeg'));
            };

            image.src = url;
       }

@MrRio any update on this? I'm getting similar errors. Doesn't occur in 1.0.272 but does in 1.2.61.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mellisa0109 picture mellisa0109  Â·  3Comments

yankeeBrit picture yankeeBrit  Â·  3Comments

mackersD picture mackersD  Â·  4Comments

BarathArivazhagan picture BarathArivazhagan  Â·  4Comments

sayo96 picture sayo96  Â·  3Comments