Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I access Page Data on footer / header function ? #1441

Closed
amrutraj opened this issue Jul 17, 2018 · 2 comments
Closed

Can I access Page Data on footer / header function ? #1441

amrutraj opened this issue Jul 17, 2018 · 2 comments

Comments

@amrutraj
Copy link

amrutraj commented Jul 17, 2018

Can I know in advance (before the actual pdf is generated), the contents of the page.

e.g. I have 100 lines of data in a table. first 60 are printed on page 1 & rest on page 2. Is there a way to get the summary data of any page on that particular footer.

For page footers,

footer: function(currentPage, pageCount, pageSize) {
        return { text: currentPage.toString() + ' of ' + pageCount, alignment: 'center' }
}, 

Can I do something like,

footer: function(currentPage, pageCount, pageSize, pageData) {
      // calculations on pageData.
        return { text: "PAGE Summary = "+ someSummaryStaistics }
}, 
@naxmefy
Copy link

naxmefy commented Jul 25, 2018

hmm i dont think so - but can't you get summaries before you start creating the pdf?

u may also access the definition object inside the callback function. E.g.:

use the following in the playground

// playground requires you to assign document definition to a variable called dd

var tableData = tableDataGenerator(365)
tableData.unshift(['#', 'Date', 'Income'])

var dd = {
	content: [
	    {
	        layout: 'lightHorizontalLines',
	        table: {
				widths: [50, 100, '*'],
				headerRows: 1,
				body: tableData
			}
	    }
	],
	footer: function (current, count) {
	    // console.log(current, count)
	    // seems there is currently no sepecfic selector
	    // may use of style value? here via index
	    var tableObject = dd.content[0]; 
	    
	    // console.log(tableObject.table)
	    // you have a positions object now
	    // console.log(tableObject.positions)
	    // this position is also available inside the table body
	    
	    // select the body and interate over columns 
	    // (dont forget exlcude the headerRow)
	    var pageSummary = 0;
	    for(var index = 1; index < tableObject.table.body.length; index++) {
	        var row = tableObject.table.body[index];
	        var col = row[2]
	        
	        // possible positon
	        // var position = tableObject.positions[index]
	        // we take it from col
	        console.log(col) 
	        if (current == col.positions[0].pageNumber) {
	            pageSummary += parseInt(col.text)
	        }
	    }
	    
	    return {text: 'Page Summary: ' + pageSummary}  
	}
}

function tableDataGenerator(days) {
    var data = []
    var date = new Date('2018-01-01')
    var income = 1
    var incomeIncrement = 0
    
    for(var i = 0; i < days; i++) {
        data.push([i+1, date.toLocaleDateString(), income])
        date.setDate(date.getDate() + 1)
        income += incomeIncrement
    }
    
    return data
}

UPDATE
you have to verify that you got the correct summary if the row breaks over the page

@liborm85
Copy link
Collaborator

Feature request is here: #1199

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants