7

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');
1
  • 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
    Commented Sep 10, 2018 at 14:02

3 Answers 3

7

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.

3
  • It worked for me, thanks. Very surprised that they did not whitelisted application/pdf... Commented Oct 10, 2018 at 7:03
  • is this a bug or they are not supporting application/pdf
    – anmrk
    Commented Oct 13, 2018 at 16:09
  • 1
    Or text/csv for that matter
    – zaitsman
    Commented Jun 17, 2020 at 1:41
2

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]
);
1
  • Now application/pdf is included. Seems like enough complained about those silly restrictions... Commented Mar 11, 2020 at 13:41
1

As of now (2020-03-11), the list of permitted MIME Types (sic!) now includes:

  • application/octet-stream — Default value for binary files
  • application/json — JSON format
  • application/pdf — Portable Document Format (.pdf)
  • video/ — All video/* mime types
  • audio/ — All audio/* mime types
  • image/ — All image/* mime types
  • font/ — All font/* mime types
  • text/plain — Text (.txt)
  • text/markdown — Markdown (.md)
  • application/zip — Zip archive (.zip)
  • application/x-bzip — Bzip archive (.bz)
  • application/x-rar-compressed — RAR archive (.rar)
  • application/x-tar — Tape archive (.tar)

Thus using application/pdf for all your Blob needs, should work - I tested it with application/zip.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .