diff --git a/README.md b/README.md index 79d3419..da1a4f9 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,16 @@ # pdf-reminder-simple -A simple pdf reminder template +[![NPM Version][npm-image]][downloads-url] [![NPM Downloads][downloads-image]][downloads-url] -Example: +**A simple pdf reminder template** + +## Usage ```javascript import moment from "moment"; import simpleReminder from "pdf-reminder-simple"; simpleReminder({ + invertHeader: true, organizationAddress: { name: "Orbin", street: "Strasse 333", @@ -34,3 +37,12 @@ simpleReminder({ note: "Meine Notiz" }).print("reminder.pdf"); ``` + +## License + +[MIT](LICENSE) + +[downloads-image]: https://img.shields.io/npm/dm/pdf-reminder-simple.svg +[downloads-url]: https://npmjs.org/package/pdf-reminder-simple +[npm-image]: https://img.shields.io/npm/v/pdf-reminder-simple.svg +[npm-url]: https://npmjs.org/package/pdf-reminder-simple diff --git a/demo/index.js b/demo/index.js index 2ec6bf2..8eb9900 100644 --- a/demo/index.js +++ b/demo/index.js @@ -3,6 +3,7 @@ import moment from "moment"; import simpleReminder from "../src"; const pdf = simpleReminder({ + invertHeader: true, organizationAddress: { name: "Orbin", street: "Strasse 333", diff --git a/dist/pdf-reminder-simple.js b/dist/pdf-reminder-simple.js index a3e5332..30bdb04 100644 --- a/dist/pdf-reminder-simple.js +++ b/dist/pdf-reminder-simple.js @@ -7,7 +7,7 @@ var a = typeof exports === 'object' ? factory(require("moment"), require("pdfmake-browser"), require("roboto-base64")) : factory(root["moment"], root["pdfmake-browser"], root["roboto-base64"]); for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i]; } -})(this, function(__WEBPACK_EXTERNAL_MODULE_1__, __WEBPACK_EXTERNAL_MODULE_2__, __WEBPACK_EXTERNAL_MODULE_3__) { +})(this, function(__WEBPACK_EXTERNAL_MODULE_3__, __WEBPACK_EXTERNAL_MODULE_4__, __WEBPACK_EXTERNAL_MODULE_5__) { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; @@ -60,15 +60,21 @@ return /******/ (function(modules) { // webpackBootstrap value: true }); - var _moment = __webpack_require__(1); + var _layout = __webpack_require__(1); + + var _headTable = __webpack_require__(2); + + var _headTable2 = _interopRequireDefault(_headTable); + + var _moment = __webpack_require__(3); var _moment2 = _interopRequireDefault(_moment); - var _pdfmakeBrowser = __webpack_require__(2); + var _pdfmakeBrowser = __webpack_require__(4); var _pdfmakeBrowser2 = _interopRequireDefault(_pdfmakeBrowser); - var _robotoBase = __webpack_require__(3); + var _robotoBase = __webpack_require__(5); var _robotoBase2 = _interopRequireDefault(_robotoBase); @@ -78,48 +84,6 @@ return /******/ (function(modules) { // webpackBootstrap fontSize: 10 }; - var tableLayout = { - hLineWidth: function hLineWidth(i) { - return i === 1 ? 1 : 0; - }, - vLineWidth: function vLineWidth() { - return 0; - }, - paddingLeft: function paddingLeft() { - return 0; - }, - paddingRight: function paddingRight() { - return 0; - }, - paddingTop: function paddingTop(i) { - return i === 1 ? 15 : 5; - }, - paddingBottom: function paddingBottom() { - return 5; - } - }; - - var footerLayout = { - hLineWidth: function hLineWidth(i, node) { - return i === 0 || i === node.table.body.length || i === node.table.body.length - 1 ? 1 : 0; - }, - vLineWidth: function vLineWidth() { - return 0; - }, - paddingLeft: function paddingLeft() { - return 0; - }, - paddingRight: function paddingRight() { - return 0; - }, - paddingTop: function paddingTop(i, node) { - return i === 0 || i === node.table.body.length - 1 ? 10 : 5; - }, - paddingBottom: function paddingBottom(i, node) { - return i === node.table.body.length - 1 || i === node.table.body.length - 2 ? 10 : 5; - } - }; - exports.default = function (options) { return new _pdfmakeBrowser2.default(getTemplate(options), { Roboto: _robotoBase2.default @@ -140,61 +104,74 @@ return /******/ (function(modules) { // webpackBootstrap var total = options.total || 0; var currency = options.currency || "CHF"; var note = options.note; + var invertHeader = options.invertHeader || false; var leftFields = []; + var organizationAddressText = organizationAddress ? getFlatAddressText(organizationAddress) : ""; + if (organizationAddressText) { + leftFields.push({ + 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({ + text: "" + }, ""); + } if (date) { rightFields.push({ - key: "Datum:", - value: date.format("DD.MM.YYYY") + text: "Datum:" + }, { + text: date.format("DD.MM.YYYY") }); } if (customerName) { rightFields.push({ - key: "Kunde:", - value: customerName + text: "Kunde:" + }, { + text: customerName }); } - var headTableBody = []; - var tableHeight = Math.max(leftFields.length, rightFields.length); - for (var i = 0; i < tableHeight; i++) { - var leftValue = leftFields[i]; - var rightObject = rightFields[i] || {}; - headTableBody.push([leftValue || "", "", rightObject.key || "", { - text: rightObject.value || "", - alignment: "right" - }]); - } + var oldLeftFields = leftFields; + leftFields = invertHeader ? rightFields : leftFields; + rightFields = invertHeader ? oldLeftFields : rightFields; - var organizationAddressText = organizationAddress ? getFlatAddressText(organizationAddress) : ""; + var headTableBody = _headTable2.default.getBody(leftFields, rightFields); var doc = { defaultStyle: defaultStyle, content: [{ - text: organizationAddressText, margin: [0, 100, 0, 0], - fontSize: 8, - color: "gray" - }, { - margin: [0, 10, 0, 0], layout: "noBorders", table: { - widths: ["auto", "*", "auto", "auto"], + widths: ["auto", "auto", "*", "auto", "auto"], body: headTableBody } }, { @@ -206,7 +183,7 @@ return /******/ (function(modules) { // webpackBootstrap text: reminderText }, { margin: [0, 25, 0, 0], - layout: tableLayout, + layout: _layout.table, table: { headerRows: 1, widths: ["*", "auto"], @@ -218,7 +195,7 @@ return /******/ (function(modules) { // webpackBootstrap }] }; - doc.content[4].table.body.push([{ + doc.content[3].table.body.push([{ stack: ["Rechnung " + (invoiceNumber ? invoiceNumber.toString() : ""), { margin: [0, 2, 0, 0], text: invoiceDate ? invoiceDate.format("DD.MM.YYYY") : "", @@ -230,7 +207,7 @@ return /******/ (function(modules) { // webpackBootstrap }]); if (feeAmount) { - doc.content[4].table.body.push(["Gebühr", { + doc.content[3].table.body.push(["Gebühr", { text: feeAmount.toFixed(2), alignment: "right" }]); @@ -238,7 +215,7 @@ return /******/ (function(modules) { // webpackBootstrap doc.content.push({ margin: [0, 25, 0, 0], - layout: footerLayout, + layout: _layout.footer, table: { headerRows: 1, widths: ["*", "auto"], @@ -273,13 +250,103 @@ return /******/ (function(modules) { // webpackBootstrap /* 1 */ /***/ function(module, exports) { - module.exports = __WEBPACK_EXTERNAL_MODULE_1__; + "use strict"; + + Object.defineProperty(exports, "__esModule", { + value: true + }); + var table = exports.table = { + hLineWidth: function hLineWidth(i) { + return i === 1 ? 1 : 0; + }, + vLineWidth: function vLineWidth() { + return 0; + }, + paddingLeft: function paddingLeft() { + return 0; + }, + paddingRight: function paddingRight() { + return 0; + }, + paddingTop: function paddingTop(i) { + return i === 1 ? 15 : 5; + }, + paddingBottom: function paddingBottom() { + return 5; + } + }; + + var footer = exports.footer = { + hLineWidth: function hLineWidth(i, node) { + return i === 0 || i === node.table.body.length || i === node.table.body.length - 1 ? 1 : 0; + }, + vLineWidth: function vLineWidth() { + return 0; + }, + paddingLeft: function paddingLeft() { + return 0; + }, + paddingRight: function paddingRight() { + return 0; + }, + paddingTop: function paddingTop(i, node) { + return i === 0 || i === node.table.body.length - 1 ? 10 : 5; + }, + paddingBottom: function paddingBottom(i, node) { + return i === node.table.body.length - 1 || i === node.table.body.length - 2 ? 10 : 5; + } + }; /***/ }, /* 2 */ /***/ function(module, exports) { - module.exports = __WEBPACK_EXTERNAL_MODULE_2__; + "use strict"; + + Object.defineProperty(exports, "__esModule", { + value: true + }); + + 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"); } } + + var HeadTable = function () { + function HeadTable() { + _classCallCheck(this, HeadTable); + } + + _createClass(HeadTable, [{ + key: "getBody", + value: function getBody(leftFields, rightFields) { + var headTableBody = []; + var tableHeight = Math.max(leftFields.length, rightFields.length); + + 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 right2 = this.getField(rightFields[i + 1]); + + var line = [].concat(left, left2, "", right, right2); + headTableBody.push(line); + } + + return headTableBody; + } + }, { + key: "getField", + value: function getField(field) { + if (!field) return ""; + return field; + } + }]); + + return HeadTable; + }(); + + exports.default = new HeadTable(); /***/ }, /* 3 */ @@ -287,6 +354,18 @@ return /******/ (function(modules) { // webpackBootstrap module.exports = __WEBPACK_EXTERNAL_MODULE_3__; +/***/ }, +/* 4 */ +/***/ function(module, exports) { + + module.exports = __WEBPACK_EXTERNAL_MODULE_4__; + +/***/ }, +/* 5 */ +/***/ function(module, exports) { + + module.exports = __WEBPACK_EXTERNAL_MODULE_5__; + /***/ } /******/ ]) }); diff --git a/package.json b/package.json index 0b84367..79dbc1b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "pdf-reminder-simple", "description": "A simple pdf reminder template", - "version": "0.2.0", + "version": "0.2.1", "author": "Orbin", "keywords": [ "pdf", diff --git a/src/head-table.js b/src/head-table.js new file mode 100644 index 0000000..aa89730 --- /dev/null +++ b/src/head-table.js @@ -0,0 +1,26 @@ +class HeadTable { + getBody(leftFields, rightFields) { + const headTableBody = []; + const tableHeight = Math.max(leftFields.length, rightFields.length); + + 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 right2 = this.getField(rightFields[i + 1]); + + const line = [].concat(left, left2, "", right, right2); + headTableBody.push(line); + } + + return headTableBody; + } + + getField(field) { + if (!field) return ""; + return field; + } +} + +export default new HeadTable(); diff --git a/src/index.js b/src/index.js index e376fb8..4b95d90 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,8 @@ +import { + table as tableLayout, + footer as footerLayout +} from "./layout"; +import headTable from "./head-table"; import moment from "moment"; import Pdf from "pdfmake-browser"; import robotoFont from "roboto-base64"; @@ -6,58 +11,6 @@ const defaultStyle = { fontSize: 10 }; -const tableLayout = { - hLineWidth: (i) => { - return (i === 1) ? 1 : 0; - }, - vLineWidth: () => { - return 0; - }, - paddingLeft: () => { - return 0; - }, - paddingRight: () => { - return 0; - }, - paddingTop: (i) => { - return (i === 1) ? 15 : 5; - }, - paddingBottom: () => { - return 5; - } -}; - -const footerLayout = { - hLineWidth: (i, node) => { - return ( - i === 0 || - i === node.table.body.length || - i === node.table.body.length - 1 - ) ? 1 : 0; - }, - vLineWidth: () => { - return 0; - }, - paddingLeft: () => { - return 0; - }, - paddingRight: () => { - return 0; - }, - paddingTop: (i, node) => { - return ( - i === 0 || - i === node.table.body.length - 1 - ) ? 10 : 5; - }, - paddingBottom: (i, node) => { - return ( - i === node.table.body.length - 1 || - i === node.table.body.length - 2 - ) ? 10 : 5; - } -}; - export default (options) => { return new Pdf(getTemplate(options), { Roboto: robotoFont @@ -78,65 +31,74 @@ function getTemplate(options) { const total = options.total || 0; const currency = options.currency || "CHF"; const note = options.note; + const invertHeader = options.invertHeader || false; - const leftFields = []; + let leftFields = []; + const organizationAddressText = organizationAddress ? getFlatAddressText(organizationAddress) : ""; + if (organizationAddressText) { + leftFields.push({ + 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 + }, ""); } - const rightFields = []; + let rightFields = []; + if (organizationAddressText) { + rightFields.push({ + text: "" + }, ""); + } if (date) { rightFields.push({ - key: "Datum:", - value: date.format("DD.MM.YYYY") + text: "Datum:" + }, { + text: date.format("DD.MM.YYYY") }); } if (customerName) { rightFields.push({ - key: "Kunde:", - value: customerName + text: "Kunde:" + }, { + text: customerName }); } - const headTableBody = []; - const tableHeight = Math.max(leftFields.length, rightFields.length); - for (let i = 0; i < tableHeight; i++) { - const leftValue = leftFields[i]; - const rightObject = rightFields[i] || {}; - headTableBody.push([ - leftValue || "", - "", - rightObject.key || "", { - text: rightObject.value || "", - alignment: "right" - } - ]); - } + const oldLeftFields = leftFields; + leftFields = invertHeader ? rightFields : leftFields; + rightFields = invertHeader ? oldLeftFields : rightFields; - const organizationAddressText = organizationAddress ? getFlatAddressText(organizationAddress) : ""; + const headTableBody = headTable.getBody(leftFields, rightFields); const doc = { defaultStyle: defaultStyle, content: [{ - text: organizationAddressText, margin: [0, 100, 0, 0], - fontSize: 8, - color: "gray" - }, { - margin: [0, 10, 0, 0], layout: "noBorders", table: { - widths: ["auto", "*", "auto", "auto"], + widths: ["auto", "auto", "*", "auto", "auto"], body: headTableBody } }, { @@ -162,7 +124,7 @@ function getTemplate(options) { }] }; - doc.content[4].table.body.push([{ + doc.content[3].table.body.push([{ stack: [ "Rechnung " + (invoiceNumber ? invoiceNumber.toString() : ""), { margin: [0, 2, 0, 0], @@ -176,7 +138,7 @@ function getTemplate(options) { }]); if (feeAmount) { - doc.content[4].table.body.push([ + doc.content[3].table.body.push([ "Gebühr", { text: feeAmount.toFixed(2), alignment: "right" diff --git a/src/layout.js b/src/layout.js new file mode 100644 index 0000000..b7c3588 --- /dev/null +++ b/src/layout.js @@ -0,0 +1,51 @@ +export const table = { + hLineWidth: (i) => { + return (i === 1) ? 1 : 0; + }, + vLineWidth: () => { + return 0; + }, + paddingLeft: () => { + return 0; + }, + paddingRight: () => { + return 0; + }, + paddingTop: (i) => { + return (i === 1) ? 15 : 5; + }, + paddingBottom: () => { + return 5; + } +}; + +export const footer = { + hLineWidth: (i, node) => { + return ( + i === 0 || + i === node.table.body.length || + i === node.table.body.length - 1 + ) ? 1 : 0; + }, + vLineWidth: () => { + return 0; + }, + paddingLeft: () => { + return 0; + }, + paddingRight: () => { + return 0; + }, + paddingTop: (i, node) => { + return ( + i === 0 || + i === node.table.body.length - 1 + ) ? 10 : 5; + }, + paddingBottom: (i, node) => { + return ( + i === node.table.body.length - 1 || + i === node.table.body.length - 2 + ) ? 10 : 5; + } +};