Count number of PDF pages with Javascript

Asked

Viewed 1,309 times

8

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

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

The report is being generated in a View of the Laravel so:

    @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 to use Javascript to count how many pages are generated within the div .page-break but I have no idea how to make that count

I also thought about counting the total pages and dividing by the number of reports but I don’t know how to do it either. Note: all reports have the same number of pages.

  • What do you use in PHP to create the document? TCPDF?

1 answer

1

I believe that solves it. I’m guessing that the for in $relatorio be to list the pages, each being $ra 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;
  • The foreach not to list pages, but to list each report. The report may have a variable number of pages

  • I understood, but then put as Oce creates or reads the pages, why is there no one here to know what is inside the variable $r, looking at the code you passed may be a very complex text or object, and how it is @foreach($relatorio in the singular thought that $relatorio already be the array with pages.

  • But she made it clear in the question that each report could have more than one page, because of the size of the text.

  • @Neuberoliveira I believe that the content of the report will not make a difference in the page count.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.