将 JSPDF 生成的 pdf 放入 iframe
Place a JSPDF-generated pdf into an iframe
我有以下使用 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>
我想要的是将生成的 blob pdf 放入具有 id #display
的 iframe 中,你知道该怎么做吗?
我还发现 Mozilla 已经制作了 pdf.js
允许我们在不支持的情况下重新生成 pdf 因此如果不支持 pdf 渲染我如何使用 pdf.js 来渲染它?
看看我编辑的这段代码,以便将 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>
如您所见,以下代码可以完成工作:
var blob_url = URL.createObjectURL(blob);
var iframeElementContainer = document.getElementById('display');
iframeElementContainer.src=blob_url;
换句话说,我们实际上生成了一个url,并将其设置为iframe 的src。生成 url 的肮脏工作是 URL.createObjectURL
函数。
我有以下使用 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>
我想要的是将生成的 blob pdf 放入具有 id #display
的 iframe 中,你知道该怎么做吗?
我还发现 Mozilla 已经制作了 pdf.js
允许我们在不支持的情况下重新生成 pdf 因此如果不支持 pdf 渲染我如何使用 pdf.js 来渲染它?
看看我编辑的这段代码,以便将 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>
如您所见,以下代码可以完成工作:
var blob_url = URL.createObjectURL(blob);
var iframeElementContainer = document.getElementById('display');
iframeElementContainer.src=blob_url;
换句话说,我们实际上生成了一个url,并将其设置为iframe 的src。生成 url 的肮脏工作是 URL.createObjectURL
函数。