3

I am having problems when I try to use the lib jsPDF in order to generate dynamic PDF documents and download them from the browser. The problem is 'Unsupported mime type'. This is due to the function getBlob() that generates an instance new Blob() with the type = 'application/pdf' into options parameter. I was using this lib last Friday all day and it was working fine. But today Monday's morning is not working anymore. This could be due to the WInter 19 release switch? Or maybe Salesforce is restricting the mime types supported into the lightning community. I have ensured that the File upload and download security for PDF is Hybrid. Thanks a lot!

The code is the following:

var doc = new jsPDF('p', 'pt');
            console.log(doc);
            var data = doc.output();
            console.log('### output: ', data);

            var buffer = new ArrayBuffer(data.length);
            var array = new Uint8Array(buffer);

            console.log('### buffer: ', buffer);

            for (var i = 0; i < data.length; i++) {
                array[i] = data.charCodeAt(i);
            }
            console.log('### array: ', array);
            var blob = new Blob(
                [array],
                {type: 'application/pdf'}
            );

            console.log('### blob: ', blob);

            saveAs(blob,'test.pdf');
  • We have faced with the same problem today on our sandboxes updated to Winter19 - just in our case we are using JsZip library, and it throws 'Unsupported mime types' for 'application/zip'. Checked on sandboxes with Summer18 - everything works without an error. Another thing I've tried so far, setting lightning component version to 39.0 (for this version Locker service is disabled), also works fine - could be used as a temporary workaround. Curious whether this is SF bug or new LockerService restriction. – wesaw Sep 10 '18 at 14:02
2

We had the same problem and we opened a case: we've been told that the LockerService whitelisted the following MIME Types:

application/octet-stream
application/json
video/
audio/
image/
font/
text/plain
text/markdown

In our case we changed all the MIME Type in our code to "text/plain" and our code still worked fine. So you can try to change your code to:

var blob = new Blob(
     [array],
     {type: 'text/plain'}
);

Tell us if this works in your case or if you get some errors doing that.

  • It worked for me, thanks. Very surprised that they did not whitelisted application/pdf... – Julien Gaïotto Oct 10 '18 at 7:03
  • is this a bug or they are not supporting application/pdf – anmrk Oct 13 '18 at 16:09
1

As AlfaCode mentioned, it looks like Locker Service is now restricting files based on MIME type that can be sent through the browser:

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/security_js_mime.htm

PDF files are not on the whitelist.

Another way to get around the issue is by not specifying a content-type when constructing the Blob. That way it would default to application/octet-stream which is supported according to the above article.

In your case, I would try changing

var blob = new Blob(
    [array],
    {type: 'application/pdf'}
);

to just

var blob = new Blob(
    [array]
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.