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

iFrame吗? 对象? 嵌入?

Adobe是怎么说的呢?

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


当前回答

这是我对AXIOS和Vue.js所做的:

        axios({
            url: `urltoPDFfile.pdf`,
            method: 'GET',
            headers: headers,
            responseType: 'blob'
        })
        .then((response) => {
            this.urlPdf = URL.createObjectURL(response.data)
        })
        .catch((error) => {
            console.log('ERROR   ', error)
        })

将urlPDF动态添加到HTML:

<object width='100%' height='600px' :data='urlPdf' type='application/pdf'></object>

其他回答

如果你不想自己托管PDF.JS,你可以试试DocDroid。它类似于谷歌Drive PDF查看器,但允许自定义品牌。

PdfToImageServlet使用ImageMagick的convert命令。

使用的例子: < img src = " / webAppDirectory / PdfToImageServlet ? pdfFile = / usr /共享/杯/数据/ default-testpage.pdf ' >

我必须用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>

看看这段代码-将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>

在我得到嵌入base64编码PDF的问题之前,因为URI限制,所以任何超过2MB的文件都不会在Chrome上正确呈现。

我的解决方案是:

转换uri编码为Blob: 基于Blob生成临时DOM字符串。 const blob = dataURItoBlob(this.dataUrl); var temp_url = window.URL.createObjectURL(blob); 决定你想把iframe附加到DOM的位置: const target = document.querySelector(targetID); 目标。innerHTML =' <iframe src='${temp_url}' type="application/pdf"></iframe> .