Is there a way to set a file name when jspdf output()

My site draws a column chart. I have a button to save it as a PDF file showing it before saving.

The way it works now is this.

  1. google.visualization draws the chart.
  2. html2canvas takes a screenshot of the chart.
  3. jsPDF inserts the screenshot in a PDF file and shows it to the user.

The problem is the name of the PDF file. It is something like 5d78c1eb-0829-4e7e-8ffc-71cf1f102f56.pdf and the url is blob:http://example.com/5d78c1eb-0829-4e7e-8ffc-71cf1f102f56 When user sees the PDF and clicks save he receives this awful file name.

enter image description here Now I show the PDF this way:

window.open(doc.output('bloburl'), '_blank');

I can set desired file name if I change this line to this:

doc.save('sample-file.pdf');

But in such a case the file just downloads but I need to show it first.

Is there a way to show the PDF and give it a desirable name? I tried this:

window.open(doc.output('bloburl', {filename: 'myFileName.pdf'}), '_blank');

But it did not help.

Another way I see is not showing the PDF from jsPDF, but sending images to the server and making a PDF file there using TCPDF. The files made with TCPDF can have a name I give it, but I think it is dumb to send the images there and back.

So the question is how can I make a PDF and show it to the user with the name I want?

Upvotes: 8

Views: 13048

Answers (3)

paulo antonio
paulo antonio

Reputation: 11

A solution was given on stackoverflow: How to display a javascript File object in Chrome's PDF viewer with its name?

    var doc = new jsPDF();
    doc.setProperties({
        title: "This is my title"
    });
    ...

The PDF filename does not change, but Chrome uses the title for TAB display enter image description here

Upvotes: 1

Darkserqer
Darkserqer

Reputation: 1

This works for me

doc.setProperties({
        title: "MyTitle.pdf"
    }).html(element, {
        callback: function (doc) {
            window.open(doc.output('bloburl'), "_blank");
        },
});

Upvotes: -1

Nikolaus
Nikolaus

Reputation: 1869

At the moment the answer is no. You can download it directly, as mentioned in the accepted answer of this question: Download with filename

But you create an objectUrl and therefore the filename is always the url. Maybe you could create an browser-extension for this...but I haven't had the time to try yet. Furthermore, you can't expect your visitors to have the extension installed.

Upvotes: 0

Related Questions