SailsJS Generate Report :
1. Install NPM Module
Official NPM Link : https://www.npmjs.com/package/pdfmake
npm install pdfmake
2. Copy Font Folder to Your DirectoryDownload it from here.. Official Repository
https://github.com/bpampuch/pdfmake/tree/master/examples
3. Include the Fonts and Pdfmake Module.
The sample code is given bellow.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fonts = { | |
Roboto: { | |
normal: 'fonts/Roboto-Regular.ttf', | |
bold: 'fonts/Roboto-Medium.ttf', | |
italics: 'fonts/Roboto-Italic.ttf', | |
bolditalics: 'fonts/Roboto-MediumItalic.ttf' | |
} | |
}; | |
var PdfPrinter = require('pdfmake'); | |
var printer = new PdfPrinter(fonts); | |
var fs = require('fs'); | |
module.exports = { | |
friendlyName: 'Get by month', | |
description: '', | |
inputs: { | |
year : { | |
type : "number", | |
required : false | |
}, | |
bid : { | |
type : "number", | |
required : false | |
}, | |
month : { | |
type : "number", | |
required : false | |
} | |
}, | |
exits: { | |
}, | |
fn: async function (inputs, exits) { | |
this.res.type('application/pdf'); | |
//var REPORT_MONTH_SQL = 'call sp_report_getMonthly( $1, $2, $3, $4 )'; | |
//var rawResult = await sails.sendNativeQuery(REPORT_MONTH_SQL, [ inputs.month||0, inputs.year||0, 0, inputs.bid||0 ]); | |
//var rptlst =rawResult.rows[0]; | |
var rptlst = [{"MonthName":"Jan","JoinDate":"01-01-2019","PaidAmount":"15","BalanceAmount":"10"},{"MonthName":"Feb","JoinDate":"01-02-2018","PaidAmount":"150","BalanceAmount":"30"}] | |
var bodydata = []; | |
bodydata.push(["S.No","Month Name", "Joined Date", "Paid Amount", "Balance Amount" ]) | |
if(rptlst.length>0){ | |
for(var i=0;i<rptlst.length;i++){ | |
bodydata.push([i+1, rptlst[i].MonthName, rptlst[i].JoinDate, rptlst[i].PaidAmount, rptlst[i].BalanceAmount]); | |
} | |
} | |
var tabledata = { | |
table: { | |
headerRows: 1, | |
widths: [ '*', 'auto', 100, '*' , 'auto'], | |
body: bodydata, | |
} | |
}; | |
var docDefinition = { | |
info: { | |
title: 'Report', | |
author: 'Thirumurthy', | |
subject: 'Report', | |
keywords: 'Report', | |
}, | |
pageSize: 'A4', | |
pageMargins: [ 40, 60, 40, 60 ], | |
header: { text:'\n\n\nMonthy Report' , alignment: 'center'}, | |
footer: { | |
columns: [ | |
'Sample Report', | |
{ text: '2018', alignment: 'right' } | |
] | |
}, | |
content: [ | |
tabledata | |
] | |
}; | |
var pdfDoc = printer.createPdfKitDocument(docDefinition); | |
pdfDoc.pipe(this.res ); | |
pdfDoc.end(); | |
} | |
}; |
No comments:
Post a Comment