Pdfmake: Can we print to PDF in loop using pdfmake

Created on 9 Mar 2015  Â·  5Comments  Â·  Source: bpampuch/pdfmake

Hi ,

I have a scenario where in I need to create the doc definition inside loop . The problem is once the doc definition is created there is no way you can append it. How can this be achieved ? Every time I loop I am getting a json object which needs to be added to the PDF file.

Regards,
Nishant

answered

Most helpful comment

Uhm, why don't you create a content = [] and push your content everytime your condition is true ...
(note: use strict comparisons === instead of ==)

if (true) {
content.push({text: "hello world"});
}

Then you end-up something like:

content = [{text: "hello world"},{"more text"}];

Then finally you can add content to docDefinition like:

var docDefinition = {
                    content: content,
                    styles: {
                        header: {
                            fontSize: 12,
                            bold: true
                        }
                    }
                }
pdfMake.createPdf(docDefinition).download();

All 5 comments

Please find the code snippet.

for (var i = 0; i < checkedValues.length; i++) {

            {{#questionList}}
            if((checkedValues[i]).localeCompare({{questionId}}) == 0 ){

                var docDefinition = {
                    content: [
                        { 
                            text: 'Problem statement', 
                            style: 'header' 
                        },
                        '{{questionString}}',
                        {
                            text: 'Detailed description',
                            style: 'header'
                        },
                        '{{detailedDescription}}',
                        {
                            text: 'Community',
                            style: 'header' 
                        },
                        '{{community}}',
                        { 
                            text: 'Category',
                            style: 'header' 
                        },
                        '{{category}}',
                        { 
                            text: 'Comments',
                            style: 'header' 
                        },
                        {
                            ol: [   
                                {{#commentList}}
                                    '{{commentString}}',
                                {{/commentList}}
                            ]
                        }
                    ],
                    styles: {
                        header: {
                            fontSize: 12,
                            bold: true
                        }
                    }
                }
            }
            {{/questionList}}
        }
        pdfMake.createPdf(docDefinition).download();

Why don't you just add to the content afterwards?

I have an if condition which needs to hold true before I add it to the content . I cannot have the condition executed in between the content.

Von meinem iPhone gesendet

Am 09-Mar-2015 um 18:38 schrieb Johannes Thönes [email protected]:

Why don't you just add to the content afterwards?

—
Reply to this email directly or view it on GitHub.

Uhm, why don't you create a content = [] and push your content everytime your condition is true ...
(note: use strict comparisons === instead of ==)

if (true) {
content.push({text: "hello world"});
}

Then you end-up something like:

content = [{text: "hello world"},{"more text"}];

Then finally you can add content to docDefinition like:

var docDefinition = {
                    content: content,
                    styles: {
                        header: {
                            fontSize: 12,
                            bold: true
                        }
                    }
                }
pdfMake.createPdf(docDefinition).download();

@Wabbala WOW!! IT WORKS!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MathLavallee picture MathLavallee  Â·  3Comments

michaelqiji picture michaelqiji  Â·  3Comments

dmatesic picture dmatesic  Â·  3Comments

sayjeyhi picture sayjeyhi  Â·  3Comments

jokris1 picture jokris1  Â·  3Comments