如何使用打字稿使json数据转换为pdf格式

时间:2019-04-24 05:47:18

标签: javascript angular pdf jspdf

我有json数据,我需要在UI中单击PDF按钮时将其转换为pdf格式。 我希望pdf格式以这种模板格式显示。我已经做了几件事,但是我没有得到如何将响应绑定到pdf函数的方法。 在这里,我要匹配2种情况,在"actualExpenses"数组中,我需要显示块中的所有值,并在每个数组中具有"expenses"数组 对象的“ actualExpenses”数组,我希望对象的"expenses"数组位于“ actualExpenses”特定对象的上方。

演示:DEMO

模板:

Image

TS:

captureScreen() {
    this.displayTable = true;
    let grossItems = [];
    grossItems.push(this.grossItems);
    console.log(grossItems)
    var doc = new jsPDF();
        var col = [
          "Actual",
          "Based Amount",
          "Fixed %",
          "80% GU ",
          "85% GU",
          "90% GU",
          "95% GU",
          "100% GU"
        ];
        var col1 = [
          "Estimate",
           "Based Amount",
          "Fixed %",
          "80% GU ",
          "85% GU",
          "90% GU",
          "95% GU",
          "100% GU"
        ];
        var rows = [];
        for (let i = 0; i < grossItems[0].actualExpenses.length; i++) {
          var temp = [];
          for (var key in grossItems[0].actualExpenses[i]) {
            temp.push(grossItems.actualExpenses[i][key]);
          }
          rows.push(temp);
          console.log(temp, "temp");
        }
        doc.setFont("Times New Roman");
        doc.setFontSize(10);
        doc.text(20, 10, grossItems.propertyName);
        doc.text(20, 20, "Property Name Goes Here");
        doc.text(20, 30, "Budgeted Year:");
        doc.setTextColor(0, 0, 0);
        doc.text(20, 40, "Actual Occupancy:");
        doc.autoTable(col, rows, {
          theme: "plain",
          startY: 45,
          margin: {
            top: 45
          },
          drawHeaderRow: (head, data) => {
            data.doc.setLineWidth(0.7);
            data.doc.setDrawColor(0, 0, 0);
            data.doc.line(
              data.cursor.x,
              data.cursor.y,
              data.cursor.x + data.table.width,
              data.cursor.y
            );
            data.doc.line(
              data.cursor.x,
              data.cursor.y + head.height,
              data.cursor.x + data.table.width,
              data.cursor.y + head.height
            );
          }
        });
        doc.autoTable(col1, rows, {
          theme: "plain",
          startY: 100,
          margin: {
            top: 100
          },

          drawHeaderRow: (head, data) => {
            data.doc.setLineWidth(0.7);
            data.doc.setDrawColor(0, 0, 0);

            data.doc.line(
              data.cursor.x,
              data.cursor.y,
              data.cursor.x + data.table.width,
              data.cursor.y
            );

            data.doc.line(
              data.cursor.x,
              data.cursor.y + head.height,
              data.cursor.x + data.table.width,
              data.cursor.y + head.height
            );
          }
        });
        document
          .getElementById("convertToPdf")
          .setAttribute("src", doc.output("datauri"));
 }

任何人都可以帮助我绑定值并使我的pdf像模板一样显示,我希望布局很好,现在唯一的问题是将值绑定到模板。

1 个答案:

答案 0 :(得分:0)

您的captureScreen()应该看起来像

captureScreen() {
this.displayTable = true;
let grossItems = [];
grossItems.push(this.grossItems);
console.log(grossItems)
var doc = new jsPDF();
    var col = [
      "Actual",
      "Based Amount",
      "Fixed %",
      "80% GU ",
      "85% GU",
      "90% GU",
      "95% GU",
      "100% GU"
    ];
    var col1 = [
      "Estimate",
       "Based Amount",
      "Fixed %",
      "80% GU ",
      "85% GU",
      "90% GU",
      "95% GU",
      "100% GU"
    ];
    var rows = [];
    for (let i = 0; i < grossItems[0].actualExpenses.length; i++) {
      var temp = [];
      temp.push(grossItems[0].actualExpenses[i].category); // ADDED
      for (var key in grossItems[0].actualExpenses[i]) {
        temp.push(grossItems[0].actualExpenses[i].baseAmount); // CHANGED HERE
      }
      rows.push(temp);
      console.log(temp, "temp");
    }
    doc.setFont("Times New Roman");
    doc.setFontSize(10);
    doc.text(20, 10, grossItems[0].propertyName);
    doc.text(20, 20, "Property Name Goes Here");
    doc.text(20, 30, "Budgeted Year:");
    //  doc.text(50, 30, grossItems.owner.company);
    doc.setTextColor(0, 0, 0);
    doc.text(20, 40, "Actual Occupancy:"); 
    //  doc.text(55, 40, grossItems.owner.actual);
    doc.autoTable(col, rows, {
      theme: "plain",
      startY: 45,
      margin: {
        top: 45
      },
      drawHeaderRow: (head, data) => {
        data.doc.setLineWidth(0.7);
        data.doc.setDrawColor(0, 0, 0); // draw black lines
        // Write the line at the top of header
        data.doc.line(
          data.cursor.x,
          data.cursor.y,
          data.cursor.x + data.table.width,
          data.cursor.y
        );
        // Write the line at the bottom of header
        data.doc.line(
          data.cursor.x,
          data.cursor.y + head.height,
          data.cursor.x + data.table.width,
          data.cursor.y + head.height
        );
      }
    });
    doc.autoTable(col1, rows, {
      theme: "plain",
      startY: 100,
      margin: {
        top: 100
      },

      drawHeaderRow: (head, data) => {
        data.doc.setLineWidth(0.7);
        data.doc.setDrawColor(0, 0, 0); // draw black lines
        // Write the line at the top of header
        data.doc.line(
          data.cursor.x,
          data.cursor.y,
          data.cursor.x + data.table.width,
          data.cursor.y
        );
        // Write the line at the bottom of header
        data.doc.line(
          data.cursor.x,
          data.cursor.y + head.height,
          data.cursor.x + data.table.width,
          data.cursor.y + head.height
        );
      }
    });
    document
      .getElementById("convertToPdf")
      .setAttribute("src", doc.output("datauri"));
 }
}