function get_pages(&$dom_tree, $max_page_height, $first_page_top) { $current_page_top = $first_page_top; $heights = array(); /** * Get list of footnotes and heights of footnote content blocks */ $footnotes = PageBreakLocator::get_footnotes_traverse($dom_tree); usort($footnotes, 'cmp_footnote_locations'); $locations = PageBreakLocator::get_break_locations($dom_tree); if (count($locations) == 0) { return array($max_page_height); } $best_location = null; foreach ($locations as $location) { if ($location->location < $current_page_top) { if (is_null($best_location)) { $best_location = $location; } $current_pos = round_units($current_page_top - $location->location); $available_page_height = round_units($max_page_height - $location->get_footnotes_height($footnotes, $current_page_top, $location->location)); if ($current_pos > $available_page_height) { /** * No more locations found on current page */ $best_location_penalty = $best_location->get_penalty($current_page_top, $max_page_height, $footnotes); if ($best_location_penalty >= MAX_PAGE_BREAK_PENALTY) { error_log('Could not find good page break location'); $heights[] = $max_page_height; $current_page_top -= $max_page_height; $best_location = null; } else { $heights[] = $current_page_top - $best_location->location; $current_page_top = $best_location->location; $best_location = null; } } else { $location_penalty = $location->get_penalty($current_page_top, $max_page_height, $footnotes); $best_penalty = $best_location->get_penalty($current_page_top, $max_page_height, $footnotes); if ($location_penalty <= $best_penalty) { /** * Better page break location found on current page */ $best_location = $location; } } if ($location->penalty < 0) { // Forced page break $heights[] = $current_page_top - $location->location; $current_page_top = $location->location; $best_location = null; } } } // Last page always will have maximal height $heights[] = $max_page_height; return $heights; }