在HTML中嵌入PDF的推荐方法是什么?

iFrame吗? 对象? 嵌入?

Adobe是怎么说的呢?

在我的例子中,PDF是动态生成的,因此不能在刷新之前将其上传到第三方解决方案。


当前回答

看看这段代码-将PDF嵌入HTML

<!-- Embed PDF File -->
<object src="YourFile.pdf" type="application/pdf" title="SamplePdf" width="500" height="720">
    <a href="YourFile.pdf">shree</a> 
</object>

其他回答

我必须用React预览PDF,所以在尝试了几个库之后,我的最佳解决方案是获取数据并emed它。

const pdfBase64 = //fetched from url or generated with jspdf or other library

  <embed
    src={pdfBase64}
    width="500"
    height="375"
    type="application/pdf"
  ></embed>
<embed src="data:application/pdf;base64,..."/>
<object width="400" height="400" data="helloworld.pdf"></object>

这是快速、简单、直截了当的,不需要任何第三方脚本:

<embed src="http://example.com/the.pdf" width="500" height="375" 
 type="application/pdf">

更新(2/3/2021)

Adobe现在提供了自己的PDF嵌入API。

https://www.adobe.io/apis/documentcloud/dcsdk/pdf-embed.html

更新(1/2018):

Android上的Chrome浏览器不再支持PDF嵌入。你可以通过使用谷歌Drive PDF查看器来解决这个问题

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

如果可能的话,使用本机解决方案,它总是最好的解决方案,因为它来自本机浏览器(使用embed或iframe),或者你可以使用这个小库来支持你:https://pdfobject.com

大多数人推荐使用著名的pdf。js。它一直工作得很好,直到我需要使用ShadowDOM。有些页面是空白的(白色),有些是黑色的。我不可能知道发生了什么,而且它正在制作中:)。