Count number of PDF pages with JavaScript

9

I am generating a PDF file with reports generated through PHP.

In the PDF file, an x number of reports is generated and each report has a number of pages. I need to know if this number y is even or odd, if it's odd I need to add a blank page.

The report is being generated in a Laravel view like this:

    @foreach($relatorio as $r)

    <div class="page-break">

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
    Mauris vel metus dui. 
    Etiam aliquam convallis nisi eu hendrerit. 
    Fusce tincidunt molestie aliquam. 
    Pellentesque porta nunc sit amet est accumsan luctus. 
    Cras fringilla pellentesque nulla at convallis...

    </div>

    @endforeach

I'm doing the page break at the end of each report with the CSS class:

.page-break{
   page-break-after:always;
}

I thought about using JavaScript to count how many pages are generated within div .page-break but I have no idea how to do this count

I also thought about counting the total number of pages and dividing by the amount of reports but I also do not know how to do it. Note: All reports have the same number of pages.

    
asked by anonymous 25.08.2016 / 14:34

1 answer

0

I believe this will resolve. I'm assuming that for in $relatorio is to list the pages, each $r being a page.

@foreach($relatorio as $i=>$r)
    @if( $i==count($relatorio)-1 && ($i % 2)==1)
        <div class="page-break">
            pagina vazia vai aqui, ou seja la como for a pagina vazia.
        </div>
    @endif
@endforeach;
    
25.08.2016 / 14:46