【发布时间】:2020-10-25 14:11:53
【问题描述】:
我在网络上发现了很多过时的选项,所以只是在徘徊什么应该是转换 DOM 的最佳方法,作为 PDF 附件,然后通过电子邮件发送。 我使用 React 作为前端,使用 .Net Core Web Api 作为后端。
在此先感谢 :)
【问题讨论】:
标签: reactjs asp.net-core asp.net-core-webapi email
我在网络上发现了很多过时的选项,所以只是在徘徊什么应该是转换 DOM 的最佳方法,作为 PDF 附件,然后通过电子邮件发送。 我使用 React 作为前端,使用 .Net Core Web Api 作为后端。
在此先感谢 :)
【问题讨论】:
标签: reactjs asp.net-core asp.net-core-webapi email
从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并发送电子邮件
【讨论】: