【问题标题】:how to convert html to pdf in modal with Jquery?如何使用 Jquery 在模态中将 html 转换为 pdf?
【发布时间】:2018-11-18 10:11:28
【问题描述】:

我有一个任务要做,我必须在没有下载选项的情况下以引导模式显示 pdf。该 pdf 将具有输入字段,用户可以在其中上传其签名,当用户单击“确定”时,它将保存相应的 pdf 并填充信息.我尝试这样做,使用 Bootstrap 模态,我可以在模态弹出窗口中显示带有输入选项的表单,但我希望无论我在模态中写什么,它都应该显示为 pdf,并且没有 doanload 选项。

我在 Jquery 中搜索答案,我看到了 jsPDF,但所有教程都展示了如何下载 html2pdf。我只想让用户先阅读它而不是下载。

<div class="modal fade" id="Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <div style="text-align: center;" class="test">
            <div>
                <hr><h2>Contract</h2><hr>
                <h3>Hereby,i buy LaLaLa Land in the exchange of 2 cookies.</h3>
            </div>

            <div class="signature">
                <hr><h2>Signature Page</h2><hr>

                    <img class="profile-pic" />
                    <div class="upload-button"></div>
                    <input class="file-upload" type="file" accept="image/*"/>

                <h4>Document ID:dlfkhahdf4565n5jbnjk45h</h4>
                <h4>User Name:Donald Trump</h4>
                <h4>User ID:456jkn45</h4>
            </div>
</div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary">Okay</button>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

  • 为什么要投反对票!你可以解释而不是仅仅投反对票!
  • 我看不出这个问题有什么问题。它是具体且可回答的;除了几个错别字,没关系。

标签: javascript jquery bootstrap-modal


【解决方案1】:

只需在iframe 中显示创建的PDF 的data: URI,或使用PDF.js 显示:

// Set up jsPDF
var pdf = new jsPDF();

// Set up PDF.js
//pdfjsLib.GlobalWorkerOptions.workerSrc =
//  'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.943/pdf.worker.min.js';

function render_example() {
  var source = document.getElementById("convert_me_to_pdf");
  pdf.fromHTML(source, function (dispose) {
    var pdf_uri = pdf.output("datauristring");
    document.getElementById("show_pdf_here").src = pdf_uri;

    var pdf_array = pdf.output("arraybuffer");
    // PDF.js here!
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.debug.js" integrity="sha384-THVO/sM0mFD9h7dfSndI6TS0PgAGavwKvB5hAxRRvc0o9cPLohB0wb/PTA7LdUHs" crossorigin="anonymous"></script>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js" crossorigin="anonymous"></script>

<button onclick="render_example()">Render</button>

<h1>Output</h1>
<h2><code>&lt;iframe&gt;</code></h2>
<iframe id="show_pdf_here" src="//www.example.org"></iframe>
<h2>PDF.js</h2>
Requires CORS to be disabled; cannot write. See <a href="https://stackoverflow.com/a/32634740/5223757">here</a> for an implementation.
<iframe src="https://mozilla.github.io/pdf.js/web/viewer.html"></iframe>

<h1>Input</h1>
<div id="convert_me_to_pdf">
  <p>
    Example...
  </p>
  <textarea></textarea>
</div>

Erm...这应该工作...我不知道为什么它不起作用。无论如何我都会发布它以防万一。

【讨论】:

  • 所以,该渲染应该下载 pdf 或仅在浏览器中显示 pdf。
  • @ChidanandaNayak 它应该在浏览器中显示它。基本上,不要使用pdf.save,而是使用pdf.output("datauristring"),然后将iframe 的src 设置为此。
  • 所以这里 document.getElementById("show_pdf_here").src = pdf_uri; 而不是 show_pdf_here 我应该给我的模态类或 id?
  • @ChidanandaNayak 如果模态框是您要在其中显示 PDF 的 &lt;iframe&gt;,则可以。但是这个例子实际上并不起作用;改用您的大部分工作但下载而不是显示版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
相关资源
最近更新 更多