jsreport

JsReport Page Number with Chrome-pdf recipe


The documents said the jsreport serves the page number and total pages function with a {#pageNum}/{#numPages}. But this only work with the phantom recipe.

I am asking on how to get these attribute in the chrome-recipe. Another document for chrome-recipe. I tried to put the following into the html template already, but still no luck since it is complaining about undefined pages in the index.js function. Any help would be appreciated.

Page&nbsp;<span class="pageNumber"></span>&nbsp;of&nbsp;<span class="totalPages"></span>

And here are my sample codes:

index.js:

...
        app.get("/test", (req, res, next) => {
          client.render({
              template: {
                content: ReadContent("test.html"),
                recipe: "chrome-pdf",
                engine: "handlebars",
                helpers: `
                function mySum() {
                  return this.$pdf.pages[this.$pdf.pageIndex].items.reduce((a,c) => a + c)
                }
                `
              }, 
              data: {}
            })
            .then((response) => {
              response.body((buffer) => {
                //prints hello world!!
                console.log(buffer.toString());
              });
              response.pipe(res);
            })
            .catch((err) => {
              next(err);
            });;
        });
    ...

test.html

<div>
        <h4>Random Text:</h4>
        <p>On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your
            document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other
            document building blocks. When you create pictures, charts, or diagrams, they also coordinate with your
            current document look. You can easily change the formatting of selected text in the document text by...

        </p>
    </div>
    <div>
         <label>enoguh Internet for today</label>
         Page&nbsp;<span class="pageNumber">{{mySum}}</span>&nbsp;of&nbsp;<span class="totalPages">{{totalPages}}</span>
    </div>

Solution

  • After one year, i found this post in my tray again. And I have found the correct way to do.

    Page&nbsp;<span class="pageNumber"></span>&nbsp;of&nbsp;<span class="totalPages"></span>
    

    Just put this line inside the html template and everything is ok.