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]; } } // determine whether or not to show the header/footer if (invoice.is_pro) { if (key === 'header') { return function(page, pages) { return page === 1 || invoice.account.all_pages_header == '1' ? val : ''; } } else if (key === 'footer') { return function(page, pages) { return page === pages || invoice.account.all_pages_footer == '1' ? 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]}); } } pdfMake.fonts = {} fonts = window.invoiceFonts || invoice.invoice_fonts; // Add only the loaded fonts $.each(fonts, function(i,font){ addFont(font); }); function addFont(font){ if(window.ninjaFontVfs[font.folder]){ folder = 'fonts/'+font.folder; pdfMake.fonts[font.name] = { normal: folder+'/'+font.normal, italics: folder+'/'+font.italics, bold: folder+'/'+font.bold, bolditalics: folder+'/'+font.bolditalics } } } if(!dd.defaultStyle)dd.defaultStyle = {font:NINJA.bodyFont}; else if(!dd.defaultStyle.font)dd.defaultStyle.font = NINJA.bodyFont; 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), 'invoiceDocuments' : NINJA.invoiceDocuments(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': formatMoneyInvoice(invoice.balance_amount, invoice), '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, 'fontSizeSmaller': NINJA.fontSize - 1, 'bodyFont': NINJA.bodyFont, 'headerFont': NINJA.headerFont, } 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('"\\$[a-z][\\\w\\\.]*?[Value]?"', 'g'); var matches = javascript.match(regExp); if (matches) { for (var i=0; i= 0) { continue; } // legacy style had 'Value' at the end if (endsWith(match, 'Value"')) { field = match.substring(2, match.indexOf('Value')); } else { field = match.substring(2, match.length - 1); } field = toSnakeCase(field); var value = getDescendantProp(invoice, field) || ' '; value = doubleDollarSign(value); javascript = javascript.replace(match, '"'+value+'"'); } } return javascript; } NINJA.notesAndTerms = function(invoice) { var data = []; if (invoice.public_notes) { data.push({stack:[{text: invoice.public_notes, style: ['notes']}]}); data.push({text:' '}); } if (invoice.terms) { data.push({text:invoiceLabels.terms, style: ['termsLabel']}); data.push({stack:[{text: invoice.terms, style: ['terms']}]}); } return NINJA.prepareDataList(data, 'notesAndTerms'); } NINJA.invoiceColumns = function(invoice) { var account = invoice.account; var columns = []; if (invoice.has_product_key) { columns.push("15%"); } columns.push("*") if (invoice.is_pro && account.custom_invoice_item_label1) { columns.push("10%"); } if (invoice.is_pro && account.custom_invoice_item_label2) { columns.push("10%"); } var count = 3; if (account.hide_quantity == '1') { count--; } if (account.show_item_taxes == '1') { count++; } 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; }