user3168144
user3168144

Reputation: 11

Using PhP to control what is Printing

Good day everyone

I have a printing problem so I'm going to explain the problem and share some basic code but the coding is not the problem, the problem is that I'm using the wrong method there must be a better method.

My web page is generated by using PhP to fetch information from mySQL and display each set of information as a table. so I may have 3 tables on 1 page or 100 tables. the problem is I really need to print this but when I do google puts the page breaks straight through my table.

the tables vary in height depending on how much information I need to display. I use javascript to set the print area.

below you will see I try anticipate the page break with table 3 but when table 1 and 2 take more than a page, it puts the break through table 2 then the rest of page 2 is empty and table 3 is only found on page 3. In this case i would need it to put the break before table 2 so the whole table prints on page 2.

<div id='print_area'>
  <table>
    this is table 1
  </table>

  <table>
    this is table 2
  </table>

  <table style='page-break-before:always'>
    this is table 3
  </table>

</div>

Sorry I need to leave for the day but I'm really hoping some one will be able to help with this problem. I will be checking back tomorrow.

Thank you in advance!

Upvotes: 0

Views: 226

Answers (2)

Guntram Blohm
Guntram Blohm

Reputation: 9819

Count the number of rows in each table before you actually print them. Assume a number of rows that fit on a page, and space you need for table headers/space between tables.

So for example

$rows_between_tables=3
$rows_needed_for_header=2
$rows_used_on_this_page=0
$rows_per_page=72  # (depends on font size etc.)

while (there_is_another_table_to_print()) {
    $rows_after_this_table = $rows_used_on_this_page +
        $rows_needed_for_header + $rows_between_tables +
        rows_for_next_table()
    if ($rows_after_this_table > $rows_per_page) {
        $rows_used_on_this_page=$rows_needed_for_header + rows_for_next_table()
        print_next_table_with_page_break_in_front_of_it();
    }
    else {
        $rows_used_on_this_page=$rows_after_this_table;
        print_next_table_no_page_break();
    }
}

Upvotes: 1

Rob Forrest
Rob Forrest

Reputation: 7450

You'll never really be able to guarantee page layout just using PHP and HTML. Have you considered creating a PDF instead of a printable web page? There are very simple but powerful PDF libraries available for PHP, my preferred one is FPDF.

Upvotes: 1

Related Questions