class="pageNumber" and class="totalPages" in parallel report generationg with pdf-utils

  • Hi, I am using handlebars and chrome-pdf to generate a report. We have some pretty big pdf files that takes forever to be generated so I decided to give it a try to the pdf-utils parallel generation.
    So far so good, with reducing some necessary big css assets it went down from 90 seconds to 40 seconds.
    The problem is that I am using the option "display header/footer". where in the footer the html is

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

    The values of pageNumber and totalPages are not kept through the different parallel generations. One stream generates 14 pages. So report of 70 pages will have 5 times count - 1/14, 2/14 - 14/14 and the again 1/14, 2/14 instead of 1/70, 2/70

    here is my code

    async function afterRender(req, res) {
            const pages = Math.ceil([0].values.length /;
            const renderPromises = [];
            let reqData;
            for (let i = 0; i < pages; i += 1) {
                 // calculations to split data per page
                 template: { 
                    name: 'reportTemplate'
                data: reqData
           const results = await Promise.all(renderPromises);
           for (let i = 0; i < results.length; i++) {
                if (i == 0) {
                    res.content = results[i].content; // the main report is blank template, so override it
                else {
                     res.content = await jsreport.pdfUtils.append(res.content, (results[i].content));

    I tried also with other way around from i = results.length - 1; to prepend templates, but the result is still the same.

    What do I have to fix to keep the paging correct throughout the whole report?


  • administrators

    hi! in this case the best would be that you also do the header, footer part using pdf-utils, you need this because the native chrome header/footer has no way to be aware that you are doing a parallel generation, you need to do the header, footer using pdf-utils because with that you will be able to take into account some logic to print the correct numbers, basically, you need to also render the header/footer in your after render script and merge it passing the correct page number and total.

    if you have some doubts about how to do it, you can always prepare a basic demo (with some very basic data that just replicate producing the same pages count) on the playground and then share it here, then we can update it.. checking real code is the best way we can guide you.

  • Hi, I tried what you suggested. From the start I didn't want to go in that direction, cause before we got header/footer in pdf utils, but because of the merge to every page with Header-Footer util the generation time was twice bigger than with the usage of "Display header/footer" option.
    Anyway, I tried now with pdf-utils header and footer. Now my problem is that the Header-Footer template is merged to the first page only. From the api I see three params only, seems like merge whole documents is not available, or I don't know how to do it.
    Here is a sample playground that imitates what I am trying to achieve with our reports

    Thanks for the help

  • Here it is fixed

    There were two problems...

    1. The flow needs to be the following. The first render in parallel all the content pages. Append it together. Parse the pdf so you get the number of pages and the informations you need for your headers. Render the headers template. Merge the two pdfs with content and headers together.
    2. When you have a header template and content template. Both should have the same size and orientation. Also the content template should use a margin settings to provide space for headers or footers.

  • Thanks for the replay. It worked perfectly, with this implementation report of 80 pdf pages was generated for 40 seconds, while before it was taking 100 seconds which is huge improving. Thanks

  • How to add TOC to it?

Log in to reply

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