最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论