下载JSPDF生成的具有非默认标题的PDF
这是我第一次使用JSPDF
,所以如果这是一个基本问题,请原谅我。
我已经成功生成了PDF,并在浏览器上的一个新标签中打开它,并在topleft上显示了正确的文件名。但是,当我尝试下载文件时,文件名不是在那里显示的文件,而是默认的“下载.pdf”,无论它是什么文件。
我发现最简单的解决方案是,
const doc = new jsPDF();
// add some content here.
doc.save("the real file name i want.pdf");
哪个解决方案在本地保存没有任何问题,但是,我仍然想先查看浏览器上的文件,然后决定是否下载它……
我进行了一些研究,这些帖子很重要,但它们要么没有直接解决我的问题,要么似乎过时了.....
到目前为止,我最好的方法是:
doc.output('dataurlnewwindow'); // open in a new tab but save with default name
window.open(doc.output('bloburi', {filename: `${name} pos.pdf`}), '_blank') // open in a new tab but save with blob name
任何帮助将不胜感激,谢谢您!
This is my first time using jspdf
so please forgive me if this is a basic problem.
I have successfully generated the pdf, opening it in a new tab on my browser, with correct filename displayed on topleft. However, when I try to download the file, the filename is not the one thats shows there but the default "download.pdf" no matter what file it is.
I figure out that the simplest solution is
const doc = new jsPDF();
// add some content here.
doc.save("the real file name i want.pdf");
which saves the file locally with no issue, however, I still want to see the file on the browser first and then decide whether to download it or not......
I did some research around, these posts are relevant but they either do not directly solve my issue or seems kind of outdated.....
How to open generated pdf using jspdf in new window
So far, my best approaches were:
doc.output('dataurlnewwindow'); // open in a new tab but save with default name
window.open(doc.output('bloburi', {filename: `${name} pos.pdf`}), '_blank') // open in a new tab but save with blob name
Any help will be appreciated, thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您创建对象URI,则无法更改文件名,因此您可以使用PDF查看器库显示它在线以便您可以更改下载按钮以工作或将其上传到某些服务器,以便您可以自定义名称。
If you create an object URI, the filename can't be altered so you could instead use a pdf viewer library to display it inline so that you can alter the download button to work or upload it to some server so that you can customise the name.