简体   繁体   English

将JSPDF生成的pdf放入iframe

[英]Place a JSPDF-generated pdf into an iframe

I have the following piece of code that generates a pdf using the https://github.com/MrRio/jsPDF library: 我有以下代码使用https://github.com/MrRio/jsPDF库生成pdf:

 var generatePdfFromHtml = function(pdf, html, callback) { console.log("Generating Pdf"); pdf.html(html, { 'callback': function(pdf) { if (callback && typeof callback === 'function') { console.log("Offering Pdf as callback"); callback(pdf); } } }); } $(document).ready(function() { /** * @var {jsPDF} pdf */ var pdf = new jsPDF('p', 'pt', 'a4'); $("#myform").on('submit', function(e) { e.preventDefault(); var val = $("#inputData").val(); /* Dummy Html to emulate the situation I am */ val = "<b>" + val + "</b>"; generatePdfFromHtml(pdf, val, function(returnedPDF) { var blob = pdfFromCallback.output('blob'); console.log(blob) /* Write Pdf into #display Iframe */ }); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js" integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/" crossorigin="anonymous"></script> <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.js"></script> <form id="myform"> <input id="inputData" name="metallica" placeholder="Enter a metallica song"><button type="submit">Enter Sandman</button> </form> <iframe id="display"></iframe> 

What I want is to place the generated blob pdf into the iframe having id #display do you know how to do that? 我想要的是将生成的blob pdf放入id为#display的iframe中你知道怎么做吗?

I also found that Mozilla has made the pdf.js allowing us to renbder pdf when not supported thus in case that pdf rendering not supported how I can use pdf.js to render it? 我还发现Mozilla已经使pdf.js允许我们在不支持的情况下渲染pdf因此如果不支持pdf渲染我如何使用pdf.js来渲染它?

Look at this code I edited in order to render the pdf into an iframe : 查看我编辑的代码,以便将pdf呈现为iframe

 var generatePdfFromHtml = function(pdf, html, callback) { console.log("Generating Pdf"); pdf.html(html, { 'callback': function(pdf) { if (callback && typeof callback === 'function') { console.log("Offering Pdf as callback"); callback(pdf); } } }); } $(document).ready(function() { /** * @var {jsPDF} pdf */ var pdf = new jsPDF('p', 'pt', 'a4'); $("#myform").on('submit', function(e) { e.preventDefault(); var val = $("#inputData").val(); /* Dummy Html to emulate the situation I am */ val = "<b>" + val + "</b>"; generatePdfFromHtml(pdf, val, function(returnedPDF) { console.log("Generated Pdf"); var blob = pdfFromCallback.output('blob'); /* Write Pdf into #display Iframe */ var blob_url = URL.createObjectURL(blob); var iframeElementContainer = document.getElementById('display'); iframeElementContainer.src=blob_url; }); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js" integrity="sha384-NaWTHo/8YCBYJ59830LTz/P4aQZK1sS0SneOgAvhsIl3zBu8r9RevNg5lHCHAuQ/" crossorigin="anonymous"></script> <script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.2.2/pdf.js"></script> <form id="myform"> <input id="inputData" name="metallica" placeholder="Enter a metallica song"><button type="submit">Enter Sandman</button> </form> <iframe id="display" style="width:100%; height:100%; overflow:scroll;"></iframe> 

As you can see the following piece of code does the job: 正如您所看到的,以下代码完成了这项工作:

 var blob_url = URL.createObjectURL(blob);
 var iframeElementContainer = document.getElementById('display');
 iframeElementContainer.src=blob_url;

In other words we actually generate a url and we set it as the iframe's src. 换句话说,我们实际上生成了一个url,我们将其设置为iframe的src。 The dirty job of generating the url is the URL.createObjectURL function. 生成url的脏工作是URL.createObjectURL函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM