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

房东的猫 提交于 2019-12-22 04:11:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/40657964/how-to-display-a-javascript-file-object-in-chromes-pdf-viewer-with-its-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!