@ index always 0 in jsreport api



  • Hi,

    I'm using the jsreport api to generate reports. I have a problem with the number of page. I print on the header content the 0 value, and if I open the jsReport on my machine, that works, but when I send the header content to jsReport api, when the report is generated the @ index is always 0.

    How can I resolve this?

    Thanks in advance.



  • Please elaborate more... Primarily please share the minimal code you send in the request.



  • Here the json cose that I send as request:

    {
        "template": {
            "helpers": "function test(){}",
            "chrome": {
                "marginTop": "3cm"
            },
            "engine": "handlebars",
            "pdfOperations": [
                {
                    "template": {
                        "helpers": "function getPageNumber(pageIndex) {\r\n    if (pageIndex == null) {\r\n        return ''\r\n    }\r\n    pageIndex = parseInt(pageIndex);\r\n    const pageNumber = pageIndex + 1\r\n    return pageNumber\r\n}\r\n\r\nfunction getTotalPages(pages) {\r\n    if (!pages) {\r\n        return ''\r\n    }\r\n    return pages.length\r\n}\r\n\r\nfunction stringIsNullOrEmptyWithDefault(str) {\r\n    if (!str || 0 === str.toString().trim().length) {\r\n        return '-';\r\n    } else {\r\n        return str;\r\n    }\r\n}\r\n\r\nfunction stringIsNotNullOrEmpty(str) {\r\n    return !(!str || 0 === str.toString().trim().length);\r\n}",
                        "engine": "handlebars",
                        "recipe": "chrome-pdf",
                        "content": "<html>\r\n\r\n<head>\r\n    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\r\n    <style>\r\n        * {\r\n            box-sizing: border-box;\r\n        }\r\n\r\n        html,\r\n        body {\r\n            color: #252525;\r\n            font-family: arial, sans-serif;\r\n            font-size: 13px;\r\n            margin: 0px;\r\n            padding: 0px;\r\n            margin: 0px 30px 0px 30px !important;\r\n        }\r\n\r\n        .main {\r\n            display: flex;\r\n            flex-direction: column;\r\n            justify-content: space-between;\r\n            width: 100%;\r\n            height: 100%;\r\n        }\r\n\r\n        .header {\r\n            width: 100%;\r\n            padding-top: 30px;\r\n        }\r\n\r\n        .footer {\r\n            width: 100%;\r\n            padding-bottom: 30px;\r\n        }\r\n\r\n        table {\r\n            font-size: 13px;\r\n            border-collapse: collapse;\r\n            width: 100%;\r\n        }\r\n\r\n        td {\r\n            border: 1px solid #252525;\r\n            padding: 5px;\r\n        }\r\n\r\n        .td-non-border {\r\n            border: 0px;\r\n            padding: 2px;\r\n        }\r\n\r\n        .left {\r\n            text-align: left;\r\n        }\r\n\r\n        .right {\r\n            text-align: right;\r\n        }\r\n\r\n        .center {\r\n            text-align: center;\r\n        }\r\n\r\n        .bold {\r\n            font-weight: bold;\r\n        }\r\n\r\n        .text-wrap-2 {\r\n            display: -webkit-box;\r\n            -webkit-line-clamp: 2;\r\n            -webkit-box-orient: vertical;\r\n            overflow: hidden;\r\n        }\r\n\r\n        .text-vertical-align-top {\r\n            vertical-align: text-top;\r\n        }\r\n\r\n        .page-break-before-always {\r\n            page-break-before: always;\r\n        }\r\n\r\n        .margin-0px {\r\n            margin: 0px;\r\n        }\r\n\r\n        .text-align-center {\r\n            text-align: center;\r\n        }\r\n\r\n        .height-60px {\r\n            height: 60px;\r\n        }\r\n\r\n        .text-vertically-right {\r\n            position: absolute;\r\n            width: 300px;\r\n            height: 200px;\r\n            z-index: 99999999999;\r\n            left: 0%;\r\n            margin: 280px 0px 0px -80px;\r\n            font-size: 6px;\r\n            float: right;\r\n            -moz-transform: rotate(270deg);\r\n            /* FF3.5+ */\r\n            -o-transform: rotate(270deg);\r\n            /* Opera 10.5 */\r\n            -webkit-transform: rotate(-90deg);\r\n            /* Saf3.1+, Chrome */\r\n            -moz-transform-origin: 100% 100%;\r\n            -o-transform-origin: 100% 100%;\r\n            -webkit-transform-origin: 100% 100%;\r\n        }\r\n\r\n        .font-16{\r\n            font-size: 16px;\r\n        }\r\n    <\/style>\r\n<\/head>\r\n\r\n<body>\r\n\r\n    {{#each $pdf.pages}} {{#if 2}}\r\n    <div class=\"page-break-before-always\"><\/div>\r\n    {{/if}}\r\n    <main class=\"main\">\r\n        <header class=\"header\">\r\n  \r\n        <\/header>\r\n        <footer class=\"footer\">\r\n            <table>\r\n                <tbody>\r\n                    <tr>\r\n                        <td class=\"td-non-border text-align-center\" width=\"20%;\">\r\n                            {{#if (stringIsNotNullOrEmpty @root.$info.code)}}\r\n                            <img class=\"height-60px\" alt=\"Barcoded value {{@root.$info.code}}\" src=\"{{@root.$info.barcode}}\">\r\n                            <br/> {{/if}}\r\n                            <b>{{@root.$info.code}}<\/b>\r\n                        <\/td>\r\n                        {{2}}\r\n                        <td class=\"right td-non-border \">\r\n                            pagina {{getPageNumber 2}} di {{getTotalPages ../$pdf.pages}}\r\n                        <\/td>\r\n                    <\/tr>\r\n                <\/tbody>\r\n            <\/table>\r\n        <\/footer>\r\n    <\/main>\r\n    {{/each}}\r\n<\/body>\r\n\r\n<\/html>"
                    },
                    "type": "merge"
                }
            ],
            "recipe": "chrome-pdf",
            "content": "<div style=\"padding-bottom:2000px\">\r\n    Test \r\n<\/div>"
        },
        "data": {
        },
        "options": {
            "reports": {
                "save": true
            }
        }
    }
    


  • Minimal means no styles and irrelevant things :)

    So your request includes no data?



  • Yes, I send no data



  • "when the report is generated the 0 is always 0."

    I apologize but it doesn't make sense to me, neither your original question.



  • Sorry, it is that if I use @ index without spaces in this text, it is converted in to a number



  • There is a single page so it looks right the 7 inside the pages array is 0.

    Note you should include mergeWholeDocument in the request

    ...
    "pdfOperations": [
     {
      "type": "merge",
      "mergeWholeDocument": true,
       ...
    }
    


  • Was that the problem, the missing "mergeWholeDocument", thanks.



  • Same issue, and yes adding mergeWholeDocument": true to the API call fixed it.
    {{ 9 }} correctly reflecting current page in the {{#each $pdf.pages}} loop of the template, once this option was added to the merge operation.


Log in to reply
 

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