【问题标题】:Send PDF as attachment using React and .Net Core Web Api使用 React 和 .Net Core Web Api 作为附件发送 PDF
【发布时间】:2020-10-25 14:11:53
【问题描述】:

我在网络上发现了很多过时的选项,所以只是在徘徊什么应该是转换 DOM 的最佳方法,作为 PDF 附件,然后通过电子邮件发送。 我使用 React 作为前端,使用 .Net Core Web Api 作为后端。

在此先感谢 :)

【问题讨论】:

    标签: reactjs asp.net-core asp.net-core-webapi email


    【解决方案1】:

    Github 下载 jsPDF 在下面包含这些脚本:

    jspdf.js
    jspdf.plugin.from_html.js
    jspdf.plugin.split_text_to_size.js
    jspdf.plugin.standard_fonts_metrics.js
    

    如果你想忽略某些元素,你必须用 ID,然后您可以在 jsPDF 的特殊元素处理程序中忽略它。 因此,您的 HTML 应如下所示:

    <!DOCTYPE html>
    <html>
      <body>
        <p id="ignorePDF">don't print this to pdf</p>
        <div>
          <p><font size="3" color="red">print this to pdf</font></p>
        </div>
      </body>
    </html>
    

    然后您使用以下 JavaScript 代码在 弹出窗口:

    var doc = new jsPDF();          
    var elementHandler = {
      '#ignorePDF': function (element, renderer) {
        return true;
      }
    };
    var source = window.document.getElementsByTagName("body")[0];
    doc.fromHTML(
        source,
        15,
        15,
        {
          'width': 180,'elementHandlers': elementHandler
        });
    
    doc.output("dataurlnewwindow");
    

    要补充的一件非常重要的事情是你失去了所有的风格 信息 (CSS)。幸运的是 jsPDF 能够很好地格式化 h1、h2、h3 等等,这对我的目的来说已经足够了。此外,它只会 在文本节点中打印文本,这意味着它不会打印 textareas 等的值。示例:

    <body>
      <ul>
        <!-- This is printed as the element contains a textnode -->        
        <li>Print me!</li>
      </ul>
      <div>
        <!-- This is not printed because jsPDF doesn't deal with the value attribute -->
        <input type="textarea" value="Please print me, too!">
      </div>
    </body>
    

    link的帮助下附加pdf并发送电子邮件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2018-06-05
      • 1970-01-01
      • 2017-06-16
      • 2020-12-05
      • 2020-10-21
      • 2011-09-03
      相关资源
      最近更新 更多