Supersharp Supersharp - 12 days ago 12
Javascript Question

How to display a javascript File object in Chrome's PDF viewer with its name?

I've got a PDF file in the form of a Blob object (generated with jsPDF), that I want to display in an

<iframe>
element.

I can do that easily this way:

iframe.src = URL.createObjectURL( blob )


The PDF is rendered correctly but I get an esoteric string in place of its name (see image below of Chrome's PDF viewer).

So I tried to convert the Blob to a File object in order to give it a human-readable name.

var file = new File( [blob], 'a_name.pdf', { type: 'application/pdf' } )
iframe.src = URL.createObjectURL( file )


It works with Firefox: the name is kept when saving the file from the header's PDF viewer.
Unfortunately it is dropped in Chrome and replaced with a random file name before being loaded in the PDF viewer.

Do you know if it is possible to display the PDF File object in the
<iframe>
with its file name?

Chrome's PDF viewer

Answer

You can set the title of the PDF generated by jsPDF with the setProperties method:

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

That title will be displayed in the PDF viewer in Chrome. You can test it in the Live Demo page.