Page Break

Aug 20, 2012 at 11:54 AM

Hi there,

Do you know how I could add a page break into the code? 

I'm trying to get it to print a report where I have an image, then html formatted text underneath the images, with each image appearing on a separate page. 

TIA

Dec 18, 2013 at 2:09 AM
Hi, did you figure out a solution to this? I'm having the same problem. Thanks!
Jan 6, 2014 at 1:42 AM
Hi,

Got the same problem and fixed it like this:

Open : \htmltodocx_converter\htmltodocx_converter.php

Find:
  case 'img':
        $image_style = array();
        if ($element->height && $element->width) {
          $state['current_style']['height'] = $element->height;
          $state['current_style']['width'] = $element->width; 
        }
        
        if (strpos($element->src, $state['base_root']) === 0) {
          // The image source is a full url, but nevertheless it is on this
          // server.
          $element_src = substr($element->src, strlen($state['base_root']));
        }
        else {
          $element_src = $element->src;
        }
        
        if (strpos($element_src, 'http://') === 0) {
          // The image url is from another site. Most probably the image won't
          // appear in the Word document.
          $src = $element_src;
        }
        elseif (strpos($element_src, '/') === 0) {
          $src = htmltodocx_doc_root() . $element_src;
        }
        else {
          $src = htmltodocx_doc_root() . $state['base_path'] . $element_src; 
        }
        
        $phpword_element->addImage($src, $state['current_style']);

      break;
Insert after "break;"
      case 'pagebreak':
        // Simply create pagebreak: 
        $state['textrun'] = $phpword_element->addPageBreak();
      break; 
U can use now the tag <pagebreak></pagebreak> in your html code ...
Coordinator
Jan 7, 2014 at 6:56 AM
That's good. Thinking about taking this further, and so people could do this without necessarily having to modify their original HTML:

The standard way of adding page breaks is using one of these CSS properties on an element: page-break-before or page-break-after. So the way to do it here could be to allow a couple of non-PHPWord style properties to be added into a htmltodocx styles array. For example:
  $styles['classes'] = 
    array (
      'my-page-break-style' => array (
        'page-break-after' => TRUE, // Possible values - TRUE, FALSE, or not set
       ),
      'another-page-break-style' => array (
        'page-break-before' => TRUE, // Possible values - TRUE, FALSE, or not set
       ),
     );
The value of this would be inspected before and after each element in htmltodocx_insert_html_recursive() with a page break being added onto the element as appropriate. Perhaps something like this:
function htmltodocx_insert_html_recursive(&$phpword_element, $html_dom_array, &$state = array()) {
   
  // Go through the html_dom_array, adding bits to go in the PHPWord element
  $allowed_children = htmltodocx_html_allowed_children($state['parents'][0]);

  if (!empty($state['current_style']['page-break-before'])) {
    $state['textrun'] = $phpword_element->addPageBreak();
  }

...

      // Reset the style back to what it was:
      $state['current_style'] = $old_style;
    }

  if (!empty($state['current_style']['page-break-after'])) {
    $state['textrun'] = $phpword_element->addPageBreak();
  }
}
We would need to check if this would function properly for all elements (e.g. table rows/cells) - excluding some of these if necessary.