Dynamically add page-breaks to html with variable amount of sections of variable sizes

2.2k views Asked by At

I'm trying to solve this current problem which I could use some help with: From the database using php, the page must load an X number of sections, every section has some content on top, a table and some content on the bottom, the table can have 0 to any number of rows, depending on what was previously in the database. Aside from the table the rest of the content doesn't vary in size.

I cannot know beforehand what will be the size of each section because of the table, and there is usually at least 30 sections.

This page must be print friendly, and aside from the page-breaks everything is already working.

I need to find a way to dynamically add page-breaks to fit the sections into the page without cutting them between pages.

The code is about 400 lines so it wouldn't be practical to post it here, but the section structure is something like this:

<section class="row">
<section>
 <section class="col-md-2"></section>
 <section class="col-md-8 printcenter">
  <p class="docenteheader" >
   <span class="tabspaceright">NAME: <strong>name</strong></span>
   <span class="tabspaceleft">ID: <strong>number</strong></span>
  </p>
  <table>
    <tr>
      <th><strong>1:</strong></th>
   <th><strong>2:</strong></th>
   <th><strong>3:</strong></th>
   <th><strong>4:</strong></th>
    </tr>
    <tr>
   <td>1</td>
   <td>2</td>
   <td>3</td>
   <td>4</td>
    </tr>
  </table>
  <p><strong>Something1:</strong> Something</p>
  <p>
   <span><strong>Something2: number</strong></span>
   <span ><strong>Something3: number</strong></span>
  </p>
  <section class="rel_endline"></section>
 </section>
 <section class="col-md-2"></section>
</section>                       
</section>

I removed the css classes and original names and PHP code to facilitate viewing

2

There are 2 answers

0
Ricardo Melo On BEST ANSWER

I found out that my problem was caused not by the faulty page breaks, but I had negative margins in an element, that messed the entire way page breaks work, by removing that, it worked normally.

4
HC_ On

Here is sample PHP code, let me know if anything is unclear. I tried to use self-explaining variable names.

<?php

    $PAGEBREAK_LENGTH = 1000; // Add a page break every 1000 characters.
    $lengthTracker = '';

    while ($row = odbc_fetch_array($res)){
        $lengthTracker .= $row['article']; 
        $lengthSize = strlen($lengthTracker);

        if ($lengthSize > $PAGEBREAK_LENGTH) {
            echo '<div class="page-break">This is a page break</div>';
            $lengthTracker = ''; // Reset length tracker since we just added a break
        }

        echo '<div class="article">';
        echo $row['article'];
        echo '</div>';
    }

?>

If you include the if ($lengthSize > $PAGEBREAK_LENGTH) before echoing the article, you will tend to include page-breaks "more often." If you include the if statement after the article is echod, you will have page-breaks less often (so you'd have potentially bigger blocks of text).

Experiment with both ways to see which style you prefer, since you (presumably) can't include page breaks mid-article.