Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export as svg / pdf? #176

Closed
skyhirider opened this issue Oct 5, 2015 · 12 comments
Closed

Export as svg / pdf? #176

skyhirider opened this issue Oct 5, 2015 · 12 comments

Comments

@skyhirider
Copy link

Can FileSaver save as .svg and .pdf ?

There isn't a svg example on the demo page but there are topics about svg here so I am unsure.

@tinaramsey87
Copy link

I've had success saving files as .pdf
Not sure about .svg

@jmcollin78
Copy link

@tinaramsey87 Can you give me a example for saving as pdf please ?
My app download a pdf after server generation and I try to force the browser to download the content.
Many thank's in advance.

@tinaramsey87
Copy link

tinaramsey87 commented Nov 4, 2015

Sorry it has taken me so long to reply!
First of all, your file needs to be in blob form for FileSaver to save it using its saveAs() function. My file was in base64 so I had to convert it to a blob - here's some code to do that http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript (use the first answer) - it will look something like:

var base64toBlob = function(b64Data, contentType, sliceSize) {
    contentType = contentType || '';
    sliceSize = sliceSize || 512;

    var byteCharacters = atob(b64Data);
    var byteArrays = [];

    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        var slice = byteCharacters.slice(offset, offset + sliceSize);

        var byteNumbers = new Array(slice.length);
        for (var i=0; i<slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }

        var byteArray = new Uint8Array(byteNumbers);

        byteArrays.push(byteArray);
    }
    var blob = new Blob(byteArrays, {type: contentType});
    return blob;
}

fileData will be the base64 string (but you might already be working with a blob, so you may not need that step). contentType must be 'application/pdf' to save as a pdf. Here's my code to save:
var blob = base64toBlob(fileData, 'application/pdf') saveAs(blob, fileName)
the fileData and fileName were returned from the server.

@jmcollin78
Copy link

Many thank's for reply. I will check that rapidly

@tinaramsey87
Copy link

OK let me know if that works for you!

@migorman
Copy link

Hi @tinaramsey87 @skyhirider @jmcollin78 please i'm using FileSaver but it works for me just with .txt can u provide me your solution that works even for pfd or jpg ...
var blob = new Blob([data], { type: contentType });
FileSaver.saveAs(blob, fileName);

@tinaramsey87
Copy link

@migorman the code I posted should work. If you have code that you're trying to use post it and I can maybe help...

@jmcollin78
Copy link

Finally I gave up without success. I finally use the old good method: generate pdf server side and download it.

@tinaramsey87
Copy link

Oh, @migorman , you need to declare contentType as 'application/pdf'. I don't see where you've declared it so make sure you're doing that somewhere. You can just do:
var blob = new Blob([data], {type: 'application/pdf'});

@migorman
Copy link

@tinaramsey87 yeah sure contentType was a variable in my case and it toooks 'application/pdf and diffrent other values,i have found a solution thanks for your help :)

@tinaramsey87
Copy link

I am glad it worked for you :)

@jimmywarting
Copy link
Collaborator

jimmywarting commented May 21, 2016

  • Content type shouldn't matter so much when saving the file. Content-type is only there to help browser render the file correctly it won't get baked into the file when you save it.
  • FileSaver.js can save any format but don't expect it to convert the input to pdf, svg, txt, html, png, jpg or anything else. It's the content that you give to FileSaver that will be saved.
  • If you have a jpg image and need to save it as png then you have to convert the binary/blob first before giving it to the FileSaver

Bottom line is FileSaver don't have something called "Save as" it only has "Save blob"

Think we can close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants