如何在Chrome应用中显示PDF的数据URI? [英] How to display data URI for PDF in Chrome App?

查看:92
本文介绍了如何在Chrome应用中显示PDF的数据URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从JavaScript PDF库(jsPDF)生成的数据URI,看起来似乎还可以,因为当我用console.log显示它并将其粘贴到浏览器URL字段中时,它可以工作.但是,无论是在Chrome应用程序窗口还是普通的浏览器窗口中,我都无法从Chrome应用程序中显示它. URI像这样开始:

I have a data URI generated from a JavaScript PDF library (jsPDF) that seems to be OK, because when I display it with console.log and paste it into a browser URL field, it works. However, I can't get it to display from within the Chrome App, either in a Chrome App window or an ordinary browser window. The URI starts like this:

var uri = "data:application/pdf;base64,JVBERi0xLjMK ...";

当我这样做时:

chrome.app.window.create(uri);

窗口打开,但是它正在尝试加载URI:

The window opens, but it is trying to load the URI:

chrome-extension://gapkhdeaendagkjpchhemfhbpfcjgmep/data:application/pdf; base64,JVBERi0xLjMK ...

chrome-extension://gapkhdeaendagkjpchhemfhbpfcjgmep/data:application/pdf;base64,JVBERi0xLjMK ...

并且在窗口中显示消息找不到此网页".

and in the window there is the message "This webpage is not found".

当我这样做时:

window.open(uri);

window.open(chrome.runtime.getURL(uri));

绝对没有任何反应.没有打开浏览器窗口或选项卡,也没有消息写入控制台.

absolutely nothing happens. No browser window or tab opens, and no message is written to the console.

有人这样做吗?

推荐答案

chrome.app.window.create将仅从打包的应用程序中加载资源,因此您无法在其中转义chrome-extension://gapkhdea.../地址空间.

chrome.app.window.create will only load a resource from the packaged app, so you can't escape the chrome-extension://gapkhdea.../ address space there.

window.open时,将使用默认的系统处理程序打开URL.我不知道任何常见的系统配置都已为数据方案注册了默认处理程序,这可能就是为什么什么也没发生的原因.

window.open when called from a packaged app window will use the default system handler to open the URL. I don't know of any common system configurations which have a default handler registered for the data scheme, which is probably why nothing happens.

如果您的目标是在打包的应用程序中显示任意不受信任的内容,则

What you want, if your goal is to display arbitrary untrusted content within a packaged app, is the webview tag. You can embed a Webview element directly into an app window's contents. You can use a data URI for its src attribute, which can be assigned dynamically.

根据您的Chrome版本,当Webview尝试加载PDF查看器插件时,您可能会遇到权限问题,但是我只是使用一些PDF数据对其进行了测试,并在打包的应用程序中显示了精美的PDF渲染.

Depending on your Chrome version, you may encounter permissions issues when the Webview attempts to load the PDF viewer plugin, but I just tested this with some PDF data and got beautifully displayed PDF rendering inside of a packaged app.

这篇关于如何在Chrome应用中显示PDF的数据URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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