如何在html中嵌入pdf文件?

Nic*_*nie 5 html php browser pdf iframe

我想将 pdf 文件嵌入到我的 html 中(使用 embed 标签或 iframe 标签)。

我尝试过以下两种。所有这些都可以在 Firefox 上正常运行,但不能在 chrome、chromium 和 android 浏览器上运行

<embed type="application/pdf" src="myfile.pdf"></embed>
<iframe class="embed-responsive-item" src="myfile.pdf" allowfullscreen></iframe>
Run Code Online (Sandbox Code Playgroud)

我还尝试使用php读取pdf文件,然后使用iframe来显示它,如下所示

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$_GET['pdf']);
@readfile($CONFIG['PDF']."/".$_GET['pdf']);
Run Code Online (Sandbox Code Playgroud)

同样,这不适用于 chrome / chromium 和移动浏览器。它仅适用于桌面版 Firefox。

我不确定这是否是重要信息,但我还设置了 X-Frame-Options“ALLOW-FROM mydomain.com”

在这里您将找到问题所在的示例。

有没有其他方法可以在网页上嵌入 pdf 文件?

Far*_*han 8

Android 上的 Chrome 浏览器不再支持 PDF 嵌入。您可以使用 Google Drive PDF 查看器获取此信息:

<embed src="https://drive.google.com/viewerng/
viewer?embedded=true&url=http://example.com/the.pdf" width="500" height="375">
Run Code Online (Sandbox Code Playgroud)

您还可以使用 Google PDF 查看器来实现此目的。您需要先将 PDF 上传到某处,然后使用其 URL:

<iframe src="http://docs.google.com/gview? url=http://example.com/mypdf.pdf&embedded=true" style="width:718px; height:700px;" frameborder="0"></iframe>
Run Code Online (Sandbox Code Playgroud)