export const printInvoice = (
account: Linode.Account,
invoice: Linode.Invoice,
items: Linode.InvoiceItem[]
) => {
try {
const itemsPerPage = 18;
const date = formatDate(invoice.date, { format: 'YYYY-MM-DD' });
const invoiceId = invoice.id;
const itemsChunks = items ? splitEvery(itemsPerPage, items) : [[]];
const tableEnd =
tableBodyStart + cellHeight * itemsChunks[itemsChunks.length - 1].length;
const doc = new jsPDF({
unit: 'px'
});
const addTable = (itemsChunk: Linode.InvoiceItem[]) => {
doc.setFontSize(10);
const header = [
{ name: 'Description', prompt: 'Description', width: 235 },
{ name: 'From', prompt: 'From', width: 72 },
{ name: 'To', prompt: 'To', width: 72 },
{ name: 'Quantity', prompt: 'Quantity', width: 52 },
{ name: 'Unit Price', prompt: 'Unit Price', width: 67 },
{ name: 'Amount', prompt: 'Amount', width: 52 }
] as any[]; // assert type 'any' because per source code this is an extended and more advanced way of usage
const itemRows = itemsChunk.map(item => {
const { label, from, to, quantity, unit_price, amount } = item;
return {
Description: formatDescription(label),
From: renderDate(from),
To: renderDate(to),
Quantity: renderQuantity(quantity),
'Unit Price': renderUnitPrice(unit_price),
Amount: '$' + amount
};
});
// Place table header
doc.table(leftPadding, 140, [], header, {
fontSize: 10,
printHeaders: true,
autoSize: false,
margins: {
left: 15,
top: 10,
width: 800,
bottom: 0
}
});
// Place table body
doc.table(leftPadding, tableBodyStart, itemRows, header, {
fontSize: 9,
printHeaders: false,
autoSize: false,
margins: {
left: leftPadding,
top: 10,
width: 800,
bottom: 0
}
});
};
const addTotalAmount = () => {
doc.setFontSize(13);
doc.setFontStyle('bold');
// Empty line
doc.cell(leftPadding, tableEnd, 412.5, 10, ' ', 1, 'left');
// "Total" cell
doc.cell(leftPadding, tableEnd + 10, 374, 20, 'Total: ', 2, 'right');
// Total value cell
doc.cell(
leftPadding + 300,
tableEnd + 10,
38.5,
20,
`$${Number(invoice.total).toFixed(2)}`,
2,
'left'
);
// reset text format
doc.setFontStyle('normal');
};
// Create a separate page for each set of invoice items
itemsChunks.forEach((itemsChunk, index) => {
doc.addImage(LinodeLogo, 'JPEG', 150, 5, 120, 50);
addLeftHeader(doc, index + 1, itemsChunks.length, date, 'Invoice');
addRightHeader(doc, account);
addTitle(doc, `Invoice: #${invoiceId}`);
addTable(itemsChunk);
addFooter(doc);
if (index < itemsChunks.length - 1) {
doc.addPage();
}
//.........这里部分代码省略.........
请发表评论