Skip to content

Commit

Permalink
update head table build
Browse files Browse the repository at this point in the history
  • Loading branch information
ardean committed Jan 2, 2017
1 parent fb0c815 commit e03841b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 131 deletions.
103 changes: 37 additions & 66 deletions dist/pdf-invoice-simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,62 +110,73 @@ return /******/ (function(modules) { // webpackBootstrap
var organizationAddressText = organizationAddress ? getFlatAddressText(organizationAddress) : "";
if (organizationAddressText) {
leftFields.push({
value: organizationAddressText,
text: organizationAddressText,
fontSize: 8,
color: "gray",
margin: [0, 0, 0, 10]
});
}, "");
}
if (billingAddress.name) {
leftFields.push(billingAddress.name);
leftFields.push({
text: billingAddress.name
}, "");
}
if (billingAddress.attn) {
leftFields.push(billingAddress.attn);
leftFields.push({
text: billingAddress.attn
}, "");
}
if (billingAddress.street) {
leftFields.push(billingAddress.street);
leftFields.push({
text: billingAddress.street
}, "");
}
var location = (billingAddress.postCode || "") + (billingAddress.city && billingAddress.postCode ? " " : "") + (billingAddress.city || "");
if (location) {
leftFields.push(location);
leftFields.push({
text: location
}, "");
}

var rightFields = [];
if (organizationAddressText) {
rightFields.push({
key: ""
});
text: ""
}, "");
}
if (date) {
rightFields.push({
key: "Datum:",
value: date.format("DD.MM.YYYY")
text: "Datum:"
}, {
text: date.format("DD.MM.YYYY")
});
}
if (dueDate) {
rightFields.push({
key: "Zahlbar bis:",
value: dueDate.format("DD.MM.YYYY")
text: "Zahlbar bis:"
}, {
text: dueDate.format("DD.MM.YYYY")
});
}
if (invoiceNumber) {
rightFields.push({
key: "Rechnungsnummer:",
value: invoiceNumber.toString()
text: "Rechnungsnummer:"
}, {
text: invoiceNumber.toString()
});
}
if (customerName) {
rightFields.push({
key: "Kunde:",
value: customerName
text: "Kunde:"
}, {
text: customerName
});
}

var oldLeftFields = leftFields;
leftFields = invertHeader ? rightFields : leftFields;
rightFields = invertHeader ? oldLeftFields : rightFields;

var headTableWidths = _headTable2.default.getWidths(leftFields, rightFields);
var headTableBody = _headTable2.default.getBody(leftFields, rightFields);

var doc = {
Expand All @@ -175,7 +186,7 @@ return /******/ (function(modules) { // webpackBootstrap
margin: [0, 30, 0, 0],
layout: "noBorders",
table: {
widths: headTableWidths,
widths: ["auto", "auto", "*", "auto", "auto"],
body: headTableBody
}
}, {
Expand Down Expand Up @@ -357,8 +368,6 @@ return /******/ (function(modules) { // webpackBootstrap
value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
Expand All @@ -369,31 +378,19 @@ return /******/ (function(modules) { // webpackBootstrap
}

_createClass(HeadTable, [{
key: "getWidths",
value: function getWidths(leftFields, rightFields) {
var left = this.getWidth(leftFields[0]);
var right = this.getWidth(rightFields[0]);
return left.concat("*", right);
}
}, {
key: "getWidth",
value: function getWidth(field) {
if ((typeof field === "undefined" ? "undefined" : _typeof(field)) === "object" && typeof field.key === "string") {
return ["auto", "auto"];
} else {
return ["auto"];
}
}
}, {
key: "getBody",
value: function getBody(leftFields, rightFields) {
var headTableBody = [];
var tableHeight = Math.max(leftFields.length, rightFields.length);

for (var i = 0; i < tableHeight; i++) {
for (var i = 0; i < tableHeight; i += 2) {
var left = this.getField(leftFields[i]);
var left2 = this.getField(leftFields[i + 1]);

var right = this.getField(rightFields[i]);
var line = left.concat("", right);
var right2 = this.getField(rightFields[i + 1]);

var line = [].concat(left, left2, "", right, right2);
headTableBody.push(line);
}

Expand All @@ -402,34 +399,8 @@ return /******/ (function(modules) { // webpackBootstrap
}, {
key: "getField",
value: function getField(field) {
if ((typeof field === "undefined" ? "undefined" : _typeof(field)) === "object" && typeof field.key === "string") {
field = field || {};

return [field.key || "", {
text: field.value || "",
alignment: "right"
}];
} else {
field = field || "";

var mapped = {
text: typeof field === "string" ? field : field.value || ""
};

if (field.fontSize) {
mapped.fontSize = field.fontSize;
}

if (field.color) {
mapped.color = field.color;
}

if (field.margin) {
mapped.margin = field.margin;
}

return [mapped];
}
if (!field) return "";
return field;
}
}]);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pdf-invoice-simple",
"description": "A simple pdf invoice template",
"version": "0.3.4",
"version": "0.3.5",
"author": "Orbin",
"keywords": [
"pdf",
Expand Down
54 changes: 8 additions & 46 deletions src/head-table.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,25 @@
class HeadTable {
getWidths(leftFields, rightFields) {
const left = this.getWidth(leftFields[0]);
const right = this.getWidth(rightFields[0]);
return left.concat("*", right);
}

getWidth(field) {
if (typeof field === "object" && typeof field.key === "string") {
return ["auto", "auto"];
} else {
return ["auto"];
}
}

getBody(leftFields, rightFields) {
const headTableBody = [];
const tableHeight = Math.max(leftFields.length, rightFields.length);

for (let i = 0; i < tableHeight; i++) {
for (let i = 0; i < tableHeight; i += 2) {
const left = this.getField(leftFields[i]);
const left2 = this.getField(leftFields[i + 1]);

const right = this.getField(rightFields[i]);
const line = left.concat("", right);
const right2 = this.getField(rightFields[i + 1]);

const line = [].concat(left, left2, "", right, right2);
headTableBody.push(line);
}

return headTableBody;
}

getField(field) {
if (typeof field === "object" && typeof field.key === "string") {
field = field || {};

return [
field.key || "", {
text: field.value || "",
alignment: "right"
}
];
} else {
field = field || "";

const mapped = {
text: typeof field === "string" ? field : field.value || ""
};

if (field.fontSize) {
mapped.fontSize = field.fontSize;
}

if (field.color) {
mapped.color = field.color;
}

if (field.margin) {
mapped.margin = field.margin;
}

return [mapped];
}
if (!field) return "";
return field;
}
}

Expand Down
47 changes: 29 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,62 +37,73 @@ function getTemplate(options) {
const organizationAddressText = organizationAddress ? getFlatAddressText(organizationAddress) : "";
if (organizationAddressText) {
leftFields.push({
value: organizationAddressText,
text: organizationAddressText,
fontSize: 8,
color: "gray",
margin: [0, 0, 0, 10]
});
}, "");
}
if (billingAddress.name) {
leftFields.push(billingAddress.name);
leftFields.push({
text: billingAddress.name
}, "");
}
if (billingAddress.attn) {
leftFields.push(billingAddress.attn);
leftFields.push({
text: billingAddress.attn
}, "");
}
if (billingAddress.street) {
leftFields.push(billingAddress.street);
leftFields.push({
text: billingAddress.street
}, "");
}
const location = (billingAddress.postCode || "") + (billingAddress.city && billingAddress.postCode ? " " : "") + (billingAddress.city || "");
if (location) {
leftFields.push(location);
leftFields.push({
text: location
}, "");
}

let rightFields = [];
if (organizationAddressText) {
rightFields.push({
key: ""
});
text: ""
}, "");
}
if (date) {
rightFields.push({
key: "Datum:",
value: date.format("DD.MM.YYYY")
text: "Datum:"
}, {
text: date.format("DD.MM.YYYY")
});
}
if (dueDate) {
rightFields.push({
key: "Zahlbar bis:",
value: dueDate.format("DD.MM.YYYY")
text: "Zahlbar bis:"
}, {
text: dueDate.format("DD.MM.YYYY")
});
}
if (invoiceNumber) {
rightFields.push({
key: "Rechnungsnummer:",
value: invoiceNumber.toString()
text: "Rechnungsnummer:"
}, {
text: invoiceNumber.toString()
});
}
if (customerName) {
rightFields.push({
key: "Kunde:",
value: customerName
text: "Kunde:"
}, {
text: customerName
});
}

const oldLeftFields = leftFields;
leftFields = invertHeader ? rightFields : leftFields;
rightFields = invertHeader ? oldLeftFields : rightFields;

const headTableWidths = headTable.getWidths(leftFields, rightFields);
const headTableBody = headTable.getBody(leftFields, rightFields);

const doc = {
Expand All @@ -102,7 +113,7 @@ function getTemplate(options) {
margin: [0, 30, 0, 0],
layout: "noBorders",
table: {
widths: headTableWidths,
widths: ["auto", "auto", "*", "auto", "auto"],
body: headTableBody
}
}, {
Expand Down

0 comments on commit e03841b

Please sign in to comment.