简体   繁体   English

如何使用typescript使json数据以pdf格式转换

[英]How to make the json data to convert in the pdf format using typescript

i have json data, i need to convert that to pdf format onclick of PDF button in UI. 我有json数据,我需要将其转换为PDF格式,点击UI中的PDF按钮。 I want the pdf format to appear in this template format. 我希望pdf格式以此模板格式显示。 I have done few things but i am not getting how to bind the response to the pdf function. 我做了一些事情,但我没有得到如何将响应绑定到pdf函数。 Here i want 2 scenarios to get matched, here in the "actualExpenses" array i need to display all the values in block and have "expenses" array in each of "actualExpenses" array of objects and i want this "expenses" array of objects to come just above the "actualExpenses" particular objects. 这里我想要两个场景匹配,这里在"actualExpenses"数组中,我需要显示块中的所有值,并在每个“actualExpenses”对象数组中有"expenses"数组,我想要这个"expenses"对象数组来自“actualExpenses”特定对象之上。

DEMO: DEMO 演示: 演示

Template: 模板:

图片

TS: 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"));
 }

Can anyone help me out to bind values and make my pdf appear as in template, i hope layout is fine, now the only issue is to bind values to the template. 任何人都可以帮助我绑定值并使我的pdf显示为模板,我希望布局很好,现在唯一的问题是将值绑定到模板。

your captureScreen() should look like. 你的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"));
 }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM