如何为base64文件设置PDF标题,同时在新标签页中打开。

问题描述 投票:0回答:1

我试图使用iframe在新的标签页中打开一个base64的PDF文件。在新的标签页中打开该PDF文件时,文件名显示为 数据。 下面附上我的截图。如何更改或设置文件名?

注:我不想下载的PDF,我需要在一个新的标签打开。先谢谢你

我的代码

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

enter image description here

javascript
1个回答
1
投票

有一种方法可以显示你想要的文件名,但它需要保存base64到 本地文件系统.

然后在iframe中加载这个文件 本地文件系统.

而且需要用户对浏览器授予一次权限。enter image description here

它可以在Chrome浏览器上运行。你可以参考下面的演示。

https:/pdf-filename.glitch.me

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

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

下载按钮失效...

编辑。

如果你想使用pdf.js,你可以参考:

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


0
投票

你所指的标题是在PDF文档本身的元数据中。你需要编辑文档中的标题。

你可以使用一个在线编辑器(如 https:/pdfcandy.comedit-pdf-meta.html。)或任何合适的PDF应用程序。

如果你是在后台生成你的PDF,你需要在那边设置适当的元数据。


0
投票

也许这种方式你可以使用。可能在安排上出现了错误。

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();
© www.soinside.com 2019 - 2024. All rights reserved.