spipu / html2pdf

OFFICIAL PROJECT | HTML to PDF converter written in PHP
http://html2pdf.fr/en/default
Open Software License 3.0
1.68k stars 751 forks source link

Row cell jumps to next page when line-height of previous cell goes below bottom margin #508

Open KevinVG opened 5 years ago

KevinVG commented 5 years ago

Hi,

When we create a PDF where there is a text in a TD with a line-height, the line-height triggers a new page and the next table cell is displayed on the next page at the bottom instead of the same page on the same row

<style>
* {
    font-family: Helvetica;
    font-size: 9pt;
    line-height: 20pt;
}
</style>

<page backtop="10px" backbottom="6mm" backleft="10px" backright="10px">
    <!-- Items table of the document -->
    <table cellspacing="0" style="margin-top: 940px;" >
        <thead class="bgcolor textcolor2">
        <tr>
            <th style="width: 300px">Test</th>
            <th style="width: 300px">Test2</th>
        </tr>
        </thead>
        <tbody>
            <tr class="textcolor">
                <td>
                    <p>Text</p>
                </td>
                <td>
                    Value
                </td>
            </tr>
        </tbody>
    </table>

</page>
use Spipu\Html2Pdf\Html2Pdf;

require_once('vendor/autoload.php');

$html2pdf = new HTML2PDF('P', 'A4', 'nl', true, 'UTF-8', array(12, 12, 12, 12));
$html2pdf->setTestTdInOnePage(false);

$html = file_get_contents('test.html');
$html2pdf->WriteHTML($html);
$pdfBody = $html2pdf->Output('proposal.pdf', 'I'); // I = Send document

In this example, the "Value" table cell is displayed at the bottom right of page 2 instead of page 1. How can we fix this, so that the "Value" cell is displayed next to the "Text" cell?

KevinVG commented 5 years ago

Found a possible solution in Html2Pdf.php:


    /**
     * make a break line
     *
     * @access protected
     * @param  float $h current line height
     * @param  integer $curr real current position in the text, if new line in the write of a text
     */
    protected function _makeBreakLine($h, $curr = null)
    {
        if ($h) {
            $bottomLineHeight = 0;
            if (isset($this->parsingCss->value['line-height']) && isset($this->parsingCss->value['line-height'])) {
                $bottomLineHeight = ($this->cssConverter->convertToMM($this->parsingCss->value['line-height']) - $this->cssConverter->convertToMM($this->parsingCss->value['font-size']));
            }
            if (( $this->pdf->GetY()+ $h - $bottomLineHeight < $this->pdf->getH() - $this->pdf->getbMargin()) || $this->_isInOverflow || $this->_isInFooter) {
                $this->_setNewLine($h, $curr);
            } else {
                $this->_setNewPage(null, '', null, $curr);
            }
        } else {
            $this->_setNewPositionForNewLine($curr);
        }

        $this->_maxH = 0;
        $this->_maxE = 0;
    }