gpt4 book ai didi

javascript - 如何使用 typescript 将json数据转换为pdf格式

转载 作者:太空狗 更新时间:2023-10-29 19:31:44 24 4
gpt4 key购买 nike

我有 json 数据,我需要在 UI 中单击 PDF 按钮将其转换为 pdf 格式。我希望pdf格式出现在这个模板格式中。我做了一些事情,但我不知道如何将响应绑定(bind)到 pdf 函数。在这里我想要匹配 2 个场景,在 "actualExpenses" 数组中我需要显示 block 中的所有值并且在每个场景中都有 "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"));
}

谁能帮我绑定(bind)值并使我的 pdf 显示在模板中,我希望布局没问题,现在唯一的问题是将值绑定(bind)到模板。

最佳答案

你的 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"));
}
}

关于javascript - 如何使用 typescript 将json数据转换为pdf格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com