将JSPDF生成的pdf放入iframe

问题描述 投票:0回答:1

我有以下代码使用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来渲染它?

javascript pdf blob jspdf pdf.js
1个回答
0
投票

看看我编辑的代码,以便将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.createObjectURL功能。

© www.soinside.com 2019 - 2024. All rights reserved.