Xml-pdf npm,使用mustache模板和xml创建pdf文件和nodeJs

lymnna71  于 2023-10-17  发布在  Node.js
关注(0)|答案(1)|浏览(141)

我创建胡子模板和我访问XML中的所有数据,我将此模板转换为PDF与XML-PDF,但我不能添加自动页码(分页)。如果页面已满,它会自动添加一个新页面,那么我可以检测到这个新页面并给予特定的布局吗??或者我可以给予一个特定的布局页面的所有网页与CSS或JS包括在模板中。小胡子?有什么办法吗?谢谢

Mustache template:


<!DOCTYPE html>
<html>
<head>
  <title>XML-PDF</title>
  <style type="text/css">
          table {
        border-collapse: collapse;
        border-spacing: 0; }
      td,
      th {
        padding: 0; }
      table, th, td {
        border: none; }
      table {
        width: 100%;
        display: table; }
        table.bordered > thead > tr,
        table.bordered > tbody > tr {
          border-bottom: 1px solid #d0d0d0; }
        table.striped > tbody > tr:nth-child(odd) {
          background-color: #f2f2f2; }
        table.striped > tbody > tr > td {
          border-radius: 0; }
        table.highlight > tbody > tr {
          transition: background-color .25s ease; }
          table.highlight > tbody > tr:hover {
            background-color: #f2f2f2; }
        table.centered thead tr th, table.centered tbody tr td {
          text-align: center; }
      thead {
        border-bottom: 1px solid #d0d0d0; }
      td, th {
        padding: 15px 5px;
        display: table-cell;
        text-align: left;
        vertical-align: middle;
        border-radius: 2px; }
  </style>
</head>
<body>
  <table class="striped">
      <thead>
          <tr>
              <th>COMMON</th>
              <th>BOTANICAL</th>
              <th>ZONE</th>
              <th>LIGHT</th>
              <th>PRICE</th>
              <th>AVAILABILITY</th>
          </tr>
      </thead>
      <tbody>
      {{#CATALOG.PLANT}}
          <tr>
            <td>{{COMMON}}</td>
            <td>{{BOTANICAL}}</td>
            <td>{{ZONE}}</td>
            <td>{{LIGHT}}</td>
            <td>{{PRICE}}</td>
            <td>{{AVAILABILITY}}</td>
          </tr>
        {{/CATALOG.PLANT}}
        </tbody>
  </table>

<section
      style="margin-left: 12%; page-break-before: always;margin-top:2em"
    >
{{#CATALOG.PLANT}}
  <ul>
      <li>{{COMMON}}</li>
        <li>{{BOTANICAL}}</li>
        <li>{{ZONE}}</li>
        <li>{{LIGHT}}</li>
        <li>{{PRICE}}</li>
        <li>{{AVAILABILITY}}</li>
  </ul>
{{/CATALOG.PLANT}}
</section>
</body>
</html>
XML file :

<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
  <PLANT>
    <COMMON>Bloodroot</COMMON>
    <BOTANICAL>Sanguinaria canadensis</BOTANICAL>
    <ZONE>4</ZONE>
    <LIGHT>Mostly Shady</LIGHT>
    <PRICE>$2.44</PRICE>
    <AVAILABILITY>031599</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Columbine</COMMON>
    <BOTANICAL>Aquilegia canadensis</BOTANICAL>
    <ZONE>3</ZONE>
    <LIGHT>Mostly Shady</LIGHT>
    <PRICE>$9.37</PRICE>
    <AVAILABILITY>030699</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Marsh Marigold</COMMON>
    <BOTANICAL>Caltha palustris</BOTANICAL>
    <ZONE>4</ZONE>
    <LIGHT>Mostly Sunny</LIGHT>
    <PRICE>$6.81</PRICE>
    <AVAILABILITY>051799</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Cowslip</COMMON>
    <BOTANICAL>Caltha palustris</BOTANICAL>
    <ZONE>4</ZONE>
    <LIGHT>Mostly Shady</LIGHT>
    <PRICE>$9.90</PRICE>
    <AVAILABILITY>030699</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Dutchman's-Breeches</COMMON>
    <BOTANICAL>Dicentra cucullaria</BOTANICAL>
    <ZONE>3</ZONE>
    <LIGHT>Mostly Shady</LIGHT>
    <PRICE>$6.44</PRICE>
    <AVAILABILITY>012099</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Ginger, Wild</COMMON>
    <BOTANICAL>Asarum canadense</BOTANICAL>
    <ZONE>3</ZONE>
    <LIGHT>Mostly Shady</LIGHT>
    <PRICE>$9.03</PRICE>
    <AVAILABILITY>041899</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Hepatica</COMMON>
    <BOTANICAL>Hepatica americana</BOTANICAL>
    <ZONE>4</ZONE>
    <LIGHT>Mostly Shady</LIGHT>
    <PRICE>$4.45</PRICE>
    <AVAILABILITY>012699</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Liverleaf</COMMON>
    <BOTANICAL>Hepatica americana</BOTANICAL>
    <ZONE>4</ZONE>
    <LIGHT>Mostly Shady</LIGHT>
    <PRICE>$3.99</PRICE>
    <AVAILABILITY>010299</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Jack-In-The-Pulpit</COMMON>
    <BOTANICAL>Arisaema triphyllum</BOTANICAL>
    <ZONE>4</ZONE>
    <LIGHT>Mostly Shady</LIGHT>
    <PRICE>$3.23</PRICE>
    <AVAILABILITY>020199</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Mayapple</COMMON>
    <BOTANICAL>Podophyllum peltatum</BOTANICAL>
    <ZONE>3</ZONE>
    <LIGHT>Mostly Shady</LIGHT>
    <PRICE>$2.98</PRICE>
    <AVAILABILITY>060599</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Phlox, Woodland</COMMON>
    <BOTANICAL>Phlox divaricata</BOTANICAL>
    <ZONE>3</ZONE>
    <LIGHT>Sun or Shade</LIGHT>
    <PRICE>$2.80</PRICE>
    <AVAILABILITY>012299</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Phlox, Blue</COMMON>
    <BOTANICAL>Phlox divaricata</BOTANICAL>
    <ZONE>3</ZONE>
    <LIGHT>Sun or Shade</LIGHT>
    <PRICE>$5.59</PRICE>
    <AVAILABILITY>021699</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Spring-Beauty</COMMON>
    <BOTANICAL>Claytonia Virginica</BOTANICAL>
    <ZONE>7</ZONE>
    <LIGHT>Mostly Shady</LIGHT>
    <PRICE>$6.59</PRICE>
    <AVAILABILITY>020199</AVAILABILITY>
  </PLANT>
  <PLANT>
    <COMMON>Trillium</COMMON>
    <BOTANICAL>Trillium grandiflorum</BOTANICAL>
    <ZONE>5</ZONE>
    <LIGHT>Sun or Shade</LIGHT>
    <PRICE>$3.90</PRICE>
    <AVAILABILITY>042999</AVAILABILITY>
  </PLANT>
  
 

  
  

</CATALOG>
index.js


const xml = require("xml-pdf");
var options = { "-h": "500m" };

xml.xmlpdf(
  "./in.xml",
  "./output.pdf",
  "./template.mustache",
  options,
  function (error, response) {
    if (error) {
      console.log(error);
    } else {
      console.log(response);
    }
  }
);

输出pdf示例:
[

]

6fe3ivhb

6fe3ivhb1#

尝试

var options = { "-h": "500m", "pdf": {"border": "10mm" /* whatever space you need */ };

相关问题