使用javascript在新窗口中打开PDF字符串 [英] Opening PDF String in new window with javascript

查看:452
本文介绍了使用javascript在新窗口中打开PDF字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式化的PDF字符串,看起来像

I have a formatted PDF string that looks like

%PDF-1.73 0 obj<<< /Type /Group /S /Transparency /CS /DeviceRGB >> /Resources 2 0 R/Contents 4 0 R>> endobj4 0 obj<> streamx��R=o�0��+��=|vL�R���l�-��ځ,���Ge�JK����{���Y5�����Z˯k�vf�a��`G֢ۢ��Asf�z�ͼ��`%��aI#�!;�t���GD?!���<�����B�b��

...

00000 n 0000000703 00000 n 0000000820 00000 n 0000000926 00000 n 0000001206 00000 n 0000001649 00000 n trailer << /Size 11 /Root 10 0 R /Info 9 0 R >>startxref2015%%EOF

我试图在一个新窗口中打开这个字符串作为PDF文件。每当我使用window.open()并将字符串写入新选项卡时,它认为文本应该是HTML文档的内容。我希望它能够识别出这是一个PDF文件。

I am trying to open up this string in a new window as a PDF file. Whenever I use window.open() and write the string to the new tab it thinks that the text should be the contents of an HTML document. I want it to recognize that this is a PDF file.

非常感谢任何帮助

推荐答案

仅供参考,以下

window.open("data:application/pdf," + encodeURI(pdfString)); 

在Chrome中不再有效。昨天,我遇到了同样的问题并尝试了这个解决方案,但没有用(它是'不允许将顶部框架导航到数据URL')。您无法再在新窗口中直接打开数据URL。
但是,您可以将其包装在iframe中,并在下面的新窗口中打开它。 =)

does not work anymore in Chrome. Yesterday, I came across with the same issue and tried this solution, but did not work (it is 'Not allowed to navigate top frame to data URL'). You cannot open the data URL directly in a new window anymore. But, you can wrap it in iframe and make it open in a new window like below. =)

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

这篇关于使用javascript在新窗口中打开PDF字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆