简体   繁体   English

使用 Java Script 以 HTML 格式导出 DIV 区域内容

[英]Exporting DIV area content in HTML using Java Script

How can export DIV area content from an HTML Page to PDF?.如何将 DIV 区域内容从 HTML 页面导出为 PDF?。

Is it possible using without third-party tool, using JavaScript?.是否可以在没有第三方工具的情况下使用 JavaScript?

Please suggest请建议

That is what I get: https://stackoverflow.com/a/24825130/3810453 when I ask to great-magician-google and clicked on FIRST link.这就是我得到的: https://stackoverflow.com/a/24825130/3810453当我询问 great-magician-google 并点击 FIRST 链接时。

jsPDF is able to use plugins. jsPDF 能够使用插件。 In order to enable it to print HTML, you have to include certain plugins and therefore have to do the following:为了使其能够打印 HTML,您必须包含某些插件,因此必须执行以下操作:

Go to https://github.com/MrRio/jsPDF and download the latest Version.https://github.com/MrRio/jsPDF下载最新版本。 Include the following Scripts in your project:在您的项目中包含以下脚本:

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

If you want to ignore certain elements, you have to mark them with an ID, which you can then ignore in a special element handler of jsPDF.如果你想忽略某些元素,你必须用一个 ID 标记它们,然后你可以在 jsPDF 的特殊元素处理程序中忽略它。 Therefore your HTML should look like this:因此,您的 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> 

Then you use the following JavaScript code to open the created PDF in a PopUp:然后使用以下 JavaScript 代码在弹出窗口中打开创建的 PDF:

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");

For me this created a nice and tidy PDF that only included the line 'print this to pdf'.对我来说,这创建了一个漂亮整洁的 PDF,其中仅包含“将其打印为 pdf”这一行。

Please note that the special element handlers only deal with IDs in the current version, which is also stated in a GitHub Issue.请注意,特殊元素处理程序仅处理当前版本中的 ID,这也在 GitHub 问题中说明。 It states:它指出:

Because the matching is done against every element in the node tree, my desire was to make it as fast as possible.因为匹配是针对节点树中的每个元素进行的,所以我希望尽可能快地进行匹配。 In that case, it meant "Only element IDs are matched" The element IDs are still done in jQuery style "#id", but it does not mean that all jQuery selectors are supported.在那种情况下,这意味着“仅匹配元素 ID” 元素 ID 仍然以 jQuery 样式“#id”完成,但这并不意味着支持所有 jQuery 选择器。

Therefore replacing '#ignorePDF' with class selectors like '.ignorePDF' did not work for me.因此,用“.ignorePDF”之类的类选择器替换“#ignorePDF”对我不起作用。 Instead you will have to add the same handler for each and every element, which you want to ignore like:相反,您必须为每个要忽略的元素添加相同的处理程序,例如:

var elementHandler = {
  '#ignoreElement': function (element, renderer) {
    return true;
  },
  '#anotherIdToBeIgnored': function (element, renderer) {
    return true;
  }
};

One very important thing to add is that you lose all your style information (CSS).要添加的一件非常重要的事情是您丢失了所有样式信息 (CSS)。 Luckily jsPDF is able to nicely format h1, h2, h3 etc., which was enough for my purposes.幸运的是 jsPDF 能够很好地格式化 h1、h2、h3 等,这对我的目的来说已经足够了。 Additionalyl it will only print text within text nodes, which means that it will not print the values of textareas and the like.此外,它只会打印文本节点内的文本,这意味着它不会打印 textarea 等的值。 Example:例子:

<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>

and no, you cant print without third-party-tool不,没有第三方工具你不能打印

如何到达<span>内部的 html 标签</span><div>使用 Java 脚本?</div><div id="text_translate"><p> 我有一个放置在div内的span标签,如下所示:</p><pre> &lt;div onclick="doActiveCheckBox('color1')" id="sss" class="test form-check form-option form-check-inline mb-2"&gt; &lt;input class="test form-check-input" type="radio" name="color" id="color1" data-bs-label="colorOption" value="/تاریک" checked=""&gt; &lt;label class="form-option-label rounded-circle" for="color1"&gt;&lt;span style="border:inherit; border-block-color:purple;" class="form-option-color rounded-circle" style="background-image: url(/img/ProductColors/green1.jpg)"&gt;&lt;/span&gt;&lt;/label&gt; &lt;/div&gt;</pre><p> 我想在单击input标签时为span标签设置border ,并且也不想在span中使用任何id 。 我尝试了以下方法:</p><p> 1:</p><pre> var d=document.getElementById("sss").getElementsByTagName("span"); d.style.border = "thick solid #0000FF";</pre><p> 2:</p><pre> var d=document.getElementById("sss").getElementsByClassName(); for (var i = 1; i &lt;= d.length; i++) { d[1].style.border = "thick solid #0000FF"; }</pre><p> 但他们都没有正常工作? 那么有人会帮忙吗?</p></div> - How to reach a html <span> tag inside a <div> using Java Script?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击按钮将HTML div内容导出为pdf - Exporting HTML div content as pdf with button click 使用Java脚本导出html表excel。 找到“#”字样时不导出 - Exporting html table excel using java script. not exporting when it find '#' chracter 将 div 中的内容连同样式组件一起导出到 text/html 文件 - Exporting content within div along with styled components to text/html file 将html div导出到字符串 - Exporting html div to string 如何使用HTML或声明为div的Java脚本单击按钮 - How to click a button using html or java script which is declared as div 如何到达<span>内部的 html 标签</span><div>使用 Java 脚本?</div><div id="text_translate"><p> 我有一个放置在div内的span标签,如下所示:</p><pre> &lt;div onclick="doActiveCheckBox('color1')" id="sss" class="test form-check form-option form-check-inline mb-2"&gt; &lt;input class="test form-check-input" type="radio" name="color" id="color1" data-bs-label="colorOption" value="/تاریک" checked=""&gt; &lt;label class="form-option-label rounded-circle" for="color1"&gt;&lt;span style="border:inherit; border-block-color:purple;" class="form-option-color rounded-circle" style="background-image: url(/img/ProductColors/green1.jpg)"&gt;&lt;/span&gt;&lt;/label&gt; &lt;/div&gt;</pre><p> 我想在单击input标签时为span标签设置border ,并且也不想在span中使用任何id 。 我尝试了以下方法:</p><p> 1:</p><pre> var d=document.getElementById("sss").getElementsByTagName("span"); d.style.border = "thick solid #0000FF";</pre><p> 2:</p><pre> var d=document.getElementById("sss").getElementsByClassName(); for (var i = 1; i &lt;= d.length; i++) { d[1].style.border = "thick solid #0000FF"; }</pre><p> 但他们都没有正常工作? 那么有人会帮忙吗?</p></div> - How to reach a html <span> tag inside a <div> using Java Script? html div 的内容使用 jquery - Content of html div using jquery java 脚本:如何读取 html 文件并将其内容加载到 div - java script: How can I read html file and load its content to a div 将div的内容导出为PDF - Exporting a div's content to PDF 使用Java将使用DXL导出器的javascript脚本库导出到文件 - Exporting javascript script library using DXL exporter to a file using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM