1 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
首先,問(wèn)題是瀏覽器不會(huì)以這種方式將 PDF 數(shù)據(jù)解釋為 PDF。srcdoc被視為原始 HTML。
有幾種不同的方法可以實(shí)現(xiàn)“點(diǎn)擊生成”功能:
1) 您可以完全刪除 AJAX,而僅使用 HTML 表單標(biāo)記來(lái)完成此操作。使用target表單元素上的屬性來(lái)定位您的 PDF 查看器 iframe。
<body>
<!-- Set up our form to target the PDF viewer iframe.-->
<!-- Note: This will work with both GET and POST methods -->
<form action="/test2.php" method="get" target="pdf_viewer">
<input type="text" name="titel">
<button height="10px" width="30px" type="submit" name="create_pdf_btn" value="create worksheet">create worksheet</button>
</form>
<!-- Initially, frame is blank, will update to PDF generation URL on form submit.
I created a special empty HTML file for this purpose. -->
<iframe name="pdf_viewer" id="pdf_viewer" src="blank.html"></iframe>
</body>
然后,test2.php您只需按照原樣生成內(nèi)聯(lián) PDF。
2) 在您的服務(wù)器上生成文件并使用 AJAX 響應(yīng)傳遞保存的 PDF 所在的位置。下面的這個(gè)答案會(huì)打開(kāi)一個(gè)新窗口,window.open但您可以簡(jiǎn)單地將該window.open行替換document.getElementById('pdf_viewer').src為使用新URL更新的行。
3) 返回 Base64 并使用冗長(zhǎng)的數(shù)據(jù) URL。
添加回答
舉報(bào)