var NINJA = NINJA || {}; NINJA.TEMPLATES = { CLEAN: "1", BOLD:"2", MODERN: "3", NORMAL:"4", BUSINESS:"5", CREATIVE:"6", ELEGANT:"7", HIPSTER:"8", PLAYFUL:"9", PHOTO:"10" }; function GetPdfMake(invoice, javascript, callback) { javascript = NINJA.decodeJavascript(invoice, javascript); function jsonCallBack(key, val) { // handle custom functions if (typeof val === 'string') { if (val.indexOf('$firstAndLast') === 0) { var parts = val.split(':'); return function (i, node) { return (i === 0 || i === node.table.body.length) ? parseFloat(parts[1]) : 0; }; } else if (val.indexOf('$none') === 0) { return function (i, node) { return 0; }; } else if (val.indexOf('$notFirstAndLastColumn') === 0) { var parts = val.split(':'); return function (i, node) { return (i === 0 || i === node.table.widths.length) ? 0 : parseFloat(parts[1]); }; } else if (val.indexOf('$notFirst') === 0) { var parts = val.split(':'); return function (i, node) { return i === 0 ? 0 : parseFloat(parts[1]); }; } else if (val.indexOf('$amount') === 0) { var parts = val.split(':'); return function (i, node) { return parseFloat(parts[1]); }; } else if (val.indexOf('$primaryColor') === 0) { var parts = val.split(':'); return NINJA.primaryColor || parts[1]; } else if (val.indexOf('$secondaryColor') === 0) { var parts = val.split(':'); return NINJA.secondaryColor || parts[1]; } } // only show the footer on the last page if (key === 'footer') { return function(page, pages) { return page === pages ? val : ''; } } // check for markdown if (key === 'text') { val = NINJA.parseMarkdownText(val, true); } /* if (key === 'stack') { val = NINJA.parseMarkdownStack(val); val = NINJA.parseMarkdownText(val, false); } */ return val; } // Add ninja logo to the footer var dd = JSON.parse(javascript, jsonCallBack); var designId = invoice.invoice_design_id; if (!invoice.is_pro) { if (designId == NINJA.TEMPLATES.CLEAN || designId == NINJA.TEMPLATES.NORMAL) { dd.footer.columns.push({image: logoImages.imageLogo1, alignment: 'right', width: 130, margin: [0, 0, 0, 0]}) } else if (designId == NINJA.TEMPLATES.BOLD) { dd.footer[1].columns.push({image: logoImages.imageLogo2, alignment: 'right', width: 130, margin: [0, -20, 20, 0]}) } else if (designId == NINJA.TEMPLATES.MODERN) { dd.footer[1].columns[0].stack.push({image: logoImages.imageLogo3, alignment: 'left', width: 130, margin: [40, 6, 0, 0]}); } } /* var fonts = { Roboto: { normal: 'Roboto-Regular.ttf', bold: 'Roboto-Medium.ttf', italics: 'Roboto-Italic.ttf', bolditalics: 'Roboto-Italic.ttf' }, }; */ doc = pdfMake.createPdf(dd); doc.save = function(fileName) { this.download(fileName); }; return doc; } NINJA.decodeJavascript = function(invoice, javascript) { var account = invoice.account; var blankImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII='; // search/replace variables var json = { 'accountName': account.name || ' ', 'accountLogo': window.accountLogo || blankImage, 'accountDetails': NINJA.accountDetails(invoice), 'accountAddress': NINJA.accountAddress(invoice), 'invoiceDetails': NINJA.invoiceDetails(invoice), 'invoiceDetailsHeight': (NINJA.invoiceDetails(invoice).length * 16) + 16, 'invoiceLineItems': NINJA.invoiceLines(invoice), 'invoiceLineItemColumns': NINJA.invoiceColumns(invoice), 'quantityWidth': NINJA.quantityWidth(invoice), 'taxWidth': NINJA.taxWidth(invoice), 'clientDetails': NINJA.clientDetails(invoice), 'notesAndTerms': NINJA.notesAndTerms(invoice), 'subtotals': NINJA.subtotals(invoice), 'subtotalsHeight': (NINJA.subtotals(invoice).length * 16) + 16, 'subtotalsWithoutBalance': NINJA.subtotals(invoice, true), 'subtotalsBalance': NINJA.subtotalsBalance(invoice), 'balanceDue': formatMoney(invoice.balance_amount, invoice.client.currency_id), 'invoiceFooter': NINJA.invoiceFooter(invoice), 'invoiceNumber': invoice.invoice_number || ' ', 'entityType': invoice.is_quote ? invoiceLabels.quote : invoiceLabels.invoice, 'entityTypeUC': (invoice.is_quote ? invoiceLabels.quote : invoiceLabels.invoice).toUpperCase(), 'fontSize': NINJA.fontSize, 'fontSizeLarger': NINJA.fontSize + 1, 'fontSizeLargest': NINJA.fontSize + 2, } for (var key in json) { // remove trailing commas for these fields if (['quantityWidth', 'taxWidth'].indexOf(key) >= 0) { var regExp = new RegExp('"\\$'+key+'",', 'g'); val = json[key]; } else { var regExp = new RegExp('"\\$'+key+'"', 'g'); var val = JSON.stringify(json[key]); val = doubleDollarSign(val); } javascript = javascript.replace(regExp, val); } // search/replace labels var regExp = new RegExp('"\\$\\\w*?Label(UC)?(:)?(\\\?)?"', 'g'); var matches = javascript.match(regExp); if (matches) { for (var i=0; i= 0) { label = label.toUpperCase(); } if (match.indexOf(':') >= 0) { label = label + ':'; } } else { label = ' '; } javascript = javascript.replace(match, '"'+label+'"'); } } // search/replace values var regExp = new RegExp('"\\$[\\\w\\\.]*?Value"', 'g'); var matches = javascript.match(regExp); if (matches) { for (var i=0; i= 0); var custom2InPattern = (pattern && pattern.indexOf('{$custom2}') >= 0); data = [ {text:clientName || ' ', style: ['clientName']}, {text:client.id_number}, {text:client.vat_number}, {text:client.address1}, {text:client.address2}, {text:cityStatePostal}, {text:client.country ? client.country.name : ''}, {text:clientEmail}, {text: client.custom_value1 && !custom1InPattern ? account.custom_client_label1 + ' ' + client.custom_value1 : false}, {text: client.custom_value2 && !custom2InPattern ? account.custom_client_label2 + ' ' + client.custom_value2 : false} ]; return NINJA.prepareDataList(data, 'clientDetails'); } NINJA.getPrimaryColor = function(defaultColor) { return NINJA.primaryColor ? NINJA.primaryColor : defaultColor; } NINJA.getSecondaryColor = function(defaultColor) { return NINJA.primaryColor ? NINJA.secondaryColor : defaultColor; } // remove blanks and add section style to all elements NINJA.prepareDataList = function(oldData, section) { var newData = []; for (var i=0; i 1 ? parts : val; } /* NINJA.parseMarkdownStack = function(val) { if (val.length == 1) { var item = val[0]; var line = item.hasOwnProperty('text') ? item.text : item; if (typeof line === 'string') { line = [line]; } var regExp = '^\\\* (.*[\r\n|\n|\r]?)'; var formatter = function(data) { return {"ul": [data.text]}; } val = NINJA.parseRegExp(line, regExp, formatter, false); } return val; } */ NINJA.parseRegExp = function(val, regExpStr, formatter, groupText) { var regExp = new RegExp(regExpStr, 'gm'); var parts = []; for (var i=0; i 1 ? parts : val; } NINJA.parseRegExpLine = function(line, regExp, formatter, groupText) { var parts = []; var lastIndex = 0; while (match = regExp.exec(line)) { if (match.index > lastIndex) { parts.push(line.substring(lastIndex, match.index)); } var data = {}; data.text = match[1]; data = formatter(data); parts.push(data); lastIndex = match.index + match[0].length; } if (parts.length) { if (lastIndex < line.length) { parts.push(line.substring(lastIndex)); } return parts; } return line; }