如何以编程方式强制 Chrome 在新选项卡中显示 PDF 文件?

发布于 2024-11-30 16:23:24 字数 622 浏览 2 评论 0 原文

我知道可以手动执行此操作(单击 PDF 链接时按住 Ctrl 键)。

但我需要这样的解决方案:

-- 当用户单击生成 PDF 的链接时,Chrome 将在浏览器中在新选项卡中显示 PDF

-- 覆盖该行为并显示下载对话框。

有什么想法吗?

更多信息:

在我的页面上有一个提交用于生成 PDF 的表单。该表单是使用 anchorjquery Submit 的组合来提交的。

然后 PDF 返回到浏览器 - 在 FF 上我得到下载对话框,但在 Cr 上我得到在同一选项卡中加载的 PDF。

//form elements

<a id="submit" target="_blank" href="http://example.com/#">download pdf</a>


<script>

$('#submit').click(function(e) {

    e.preventDefault();

    $('#lost_form').submit();

});

</script>

I am aware that one can do this manually (hold Ctrl down when clicking the PDF link).

But I need a solution like this:

-- when a user clicks on the link that generates the PDF, Chrome will either display the PDF in-browser in a new tab

or

-- override that behavior and show the download dialog.

Any ideas?

MORE INFO:

On my page there is a form that is submitted to generate the PDF. The form is submitted using a combination of anchor and jquery submit.

The PDF is then returned to the browser - on FF I get the download dialog, but on Cr I get the PDF loaded in the same tab.

//form elements

<a id="submit" target="_blank" href="http://example.com/#">download pdf</a>


<script>

$('#submit').click(function(e) {

    e.preventDefault();

    $('#lost_form').submit();

});

</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヅ她的身影、若隐若现 2024-12-07 16:23:24

在 PDF 链接中,使用 target="_blank"< /code>,如下所示:

<a href="//path/to/document.pdf" target="_blank">PDF link text</a>

如果您想下载 PDF 而不是在浏览器中打开,则需要设置 Content-Disposition HTTP 响应标头 类似于

Content-Disposition: attachment; filename=document.pdf

In the link to the PDF, use target="_blank", like so:

<a href="//path/to/document.pdf" target="_blank">PDF link text</a>

If you want to make the PDF download instead of open in-browser, you need to set the Content-Disposition HTTP response header to something like

Content-Disposition: attachment; filename=document.pdf
轻许诺言 2024-12-07 16:23:24

onclick 打开一个新窗口,您的 PDF 路径在此 url 中

http://docs.google.com/gview?url=<?php echo $pdffilefath;?>&embedded=true

onclick open a new window, with your PDF path in this url

http://docs.google.com/gview?url=<?php echo $pdffilefath;?>&embedded=true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文