javascript - How to set PDF title for base64 file while opening in new tab - Stack Overflow

I'm trying to open a base64 PDF file in a new tab by using iframe. While opening that PDF in new t

I'm trying to open a base64 PDF file in a new tab by using iframe. While opening that PDF in new tab file name is showing as data: attached my screenshot below. How can I change or set the file name?.

Note: I don't want to download the PDF I need to open in a new tab. Thanks in advance

My Code

const pdfWindow = window.open('');
pdfWindow.document.write(
  "<iframe width='100%' height='100%' src='data:application/pdf;base64, " +
    encodeURI(base64PDF) +
    "'></iframe>",
);

I'm trying to open a base64 PDF file in a new tab by using iframe. While opening that PDF in new tab file name is showing as data: attached my screenshot below. How can I change or set the file name?.

Note: I don't want to download the PDF I need to open in a new tab. Thanks in advance

My Code

const pdfWindow = window.open('');
pdfWindow.document.write(
  "<iframe width='100%' height='100%' src='data:application/pdf;base64, " +
    encodeURI(base64PDF) +
    "'></iframe>",
);

Share Improve this question asked Jun 8, 2020 at 9:27 PravinPravin 4412 gold badges9 silver badges26 bronze badges 1
  • do you mean the result of <title> tags in HTML pages? – CanUver Commented Jun 8, 2020 at 10:39
Add a ment  | 

3 Answers 3

Reset to default 3

Maybe this way you can use. There may have been a mistake in the arrangements.

let file = 'data:application/pdf;base64'
let prntWin = window.open();
prntWin.document.write("<html><head><title>your title in here</title></head><body>"
    + '<iframe width="100%" height="100%" src="'+ file + '" '
    + 'type="application/pdf" ></body></html>');
prntWin.document.close();

There is a way to show the filename you want, but it needs to save the base64 to LocalFileSystem.

Then the iframe load this file in LocalFileSystem.

And it needs user to grant permission once to the browser.

It works on Chrome. You can refer to below demo:

https://pdf-filename.glitch.me/

Souce:

https://glitch./edit/#!/pdf-filename?path=index.html%3A1%3A0

The download button fail to work...

Edit:

If you would like to use pdf.js, you can refer to:

https://daniel4wong-pdfjs-demo.glitch.me/

The title that you are refering to is in the metadata of the PDF document itself. You will need to edit the title in the document.

You can use an online editor (like https://pdfcandy./edit-pdf-meta.html) or any suitable PDF application.

If you are generating your PDF's on the backend you need to set the proper metadata over there.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744773501a4592906.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信