different header/footer examples not working within my node.js testing environment



  • Hi folks,
    since days I am trying to get a working environment of different headers and/or footers test reports listed in playground. The main problem are empty or null header/footer values for handlebar entries. Also page counters (by 0 of $pdf.pages) will set to 1 of 3 on all pages instead of 1 of 3, 2 of 3, 3 of 3). I am also unable to find really useful examples.
    I tried a full server installation with all modules and a smaller one with only some of them (assets, chrome-pdf, cli, core, docx, handlebars, pdf-util, scripts). Here some code, but I also tried pdfOperations and additional template entries. The header file is also an example of the playground and very simple ('''{{#each $pdf.pages}}<span>Seite: {{testPageDummy 'page Dummy'}}</span>{{#if 0}})... '''). Does anyone have an working example of node.js code with an header, chrome-pdf and handlebar?

    jsreport.render({
        template: {
          content: fs.readFileSync('./templates/sample/template-main.html').toString('utf8'),
          engine: 'handlebars',
          recipe: 'chrome-pdf',
    	  chrome: {
    		displayHeaderFooter: true,
    		headerTemplate: fs.readFileSync('./templates/sample/template-header.html').toString('utf8'),
    		footerTemplate: '',
    		marginTop: '100px',
            marginBottom:'0px',
    	  },		  	  
    	  helpers: fs.readFileSync('./templates/sample/template-helpers.js').toString('utf8'),	  
    	  scripts: [{
              content: fs.readFileSync('./templates/sample/template-script.js').toString('utf8'),
          }],	  
        }
      })```
    
    Thanks a lot for every small info and/or code example.


  • Here is a node example

      const jsreport = require('jsreport')()
      const fs = require('fs').promises
    
      await jsreport.init()
      const response = await jsreport.render({
        template: {
          content: `
            Hello from main page 1
            <div style='page-break-before: always;'></div>
            Hello from page 2
          `,
          engine: 'handlebars',
          recipe: 'chrome-pdf',
          chrome: {
            marginTop: '2cm'
          },
          pdfOperations: [{
            type: 'merge',
            mergeWholeDocument: true,
            template: {
              engine: 'handlebars',
              recipe: 'chrome-pdf',
              content: `
                {{#each $pdf.pages}}
                {{#if 1}}
                  <div style="page-break-before: always;"></div>
                {{/if}}           
                <span>Page {{getPageNumber 1}} of {{getTotalPages ../$pdf.pages}}</span>           
              {{/each}}
              `,
              helpers: `          
              function getPageNumber (pageIndex) {
                if (pageIndex == null) {
                    return ''
                }
                
                const pageNumber = pageIndex + 1
                
                return pageNumber
              }
    
              function getTotalPages (pages) {
                if (!pages) {
                    return ''
                }
                
                return pages.length
              }
              `
              
            }
          }]
        }
      })
    
      await fs.writeFile('out.pdf', response.content)
      await jsreport.close()
    


  • Thanks a lot @jan_blaha Works like a charm and saved me a lot of time.


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.