Dompdf error: "Line # 4 cannot be found" when converting a PHP file to PDF

I am trying to create a dynamic PDF file from a PHP page and this gives me this error:

Fatal error: throw exception "Dompdf \ Exception" with the message "The line number 4 was not found, please report a problem in the tray HTML code 'in C: \ xampp \ htdocs \ Gokujou \ dompdf \ src \ Cellmap.php: 417 Trace stack: # 0 C: \ XAMPP \ HTDOCS \ Gokujou \ DOMPDF \ SRC \ FrameReflower \ TableRow.php (62): DOMPDF \ Cellmap-> get_frame_height (Object (DOMPDF \ FrameDecorator \ TableRow)) # 1 C: \ xampp \ htd \ Gokujou \ dompdf \ src \ FrameDecorator \ AbstractFrameDecorator.php (893): Dompdf \ FrameReflower \ TableRow-> reflow (NULL) # 2 C: \ XAMPP \ HTDOCS \ Gokujou \ DOMPDF \ SRC \ FrameReflower \ TableRowGroup. : Dompdf \ FrameDecorator \ AbstractFrameDecorator-> reflow () # 3 C: \ XAMPP \ HTDOCS \ Gokujou \ DOMPDF \ SRC \ FrameDecorator \ AbstractFrameDecorator.php (893): Dompdf \ FrameReflower \ TableRowGroup-> reflow (NULL)\ XAMPP \ HTDOCS \ Gokujou \ DOMPDF \ SRC \ FrameReflower \ Table.php (488): Dompdf \ FrameDecorator \ AbstractFrameDecorator-> reflow () # 5 C: \ XAMPP \ HTDOCS \ Gokujou \ DOMPDF \ SRC \ FrameDecorator (893): DOMPDF \ FrameReflower \ Table-> Reflow (Object (DOMPDF \ FrameDecorator \ Block)) in C: \ xampp \ htdocs \ Gokujou \ dompdf \ src \ Cellmap.php on line 417

I have a button on which he clicks "checkout", it will create a PDF page using this code:

<?php
    require_once 'dompdf/autoload.inc.php';
    use Dompdf\Dompdf;

    $dompdf = new Dompdf();
    $dompdf->loadHTML(file_get_contents('receipt.php'));
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();
    $dompdf->stream('samplepdf1');
?>

What is inside the receipt.php file:

<?php
    include 'config.php';
?>

<!DOCTYPE html>
<html>
<head>
    <title>Gokujou Japanese Restaurant</title>
    <link rel="stylesheet" type="text/css" href="CSS/receipt.css">
</head>
<body>
    <div class="receiptContainer">
        <center>
            <img src="Images/logo.png" width="175px">
            <h4>GOKUJOU JAPANESE RESTAURANT</h4>
            <p>Total Gas Station, Hibbard Ave., Looc,<br>Dumaguete City, 6200 Negros Oriental, Philippines <br>
            09985555175 | 422-1435 <br>
            <?php echo date("Y-m-d h:i:sA"); ?>
            </p>

            <table width="90%" style="text-align: center;">
                <tr>
                    <th>DESCRIPTION</th>
                    <th>QTY</th>
                    <th>PRICE</th>
                    <th>TOTAL</th>
                </tr>
                <tr>
                    <td></td>
                </tr>
                <?php
                    $query = mysqli_query($con, "SELECT * FROM orders WHERE customerID = '".$_SESSION['customer']."' AND status = 'Checked Out'");
                    while($row = mysqli_fetch_row($query)){
                ?>
                <tr>
                    <td><?php echo $row[3]; ?></td>
                    <td><?php echo $row[5]; ?></td>
                    <td><?php echo $row[4]; ?></td>
                    <td><?php echo $row[6]; ?></td>
                </tr>
                <?php
                    }
                    $total = mysqli_query($con, "SELECT SUM(total) AS grandTotal FROM orders WHERE customerID = '".$_SESSION['customer']."' AND status = 'Checked Out' GROUP BY customerID");
                    $row = mysqli_fetch_row($total);
                    $sum = $row[0];
                ?>
                <tr>
                    <!-- break space -->
                    <tr></tr><tr></tr><tr></tr><tr></tr>
                    <tr></tr><tr></tr><tr></tr><tr></tr>
                    <tr></tr><tr></tr><tr></tr><tr></tr>

                    <td colspan="1" style="text-align: left">GRAND TOTAL: <?php echo $sum; ?></td>
                    <td colspan="3"></td>
                </tr>
                <tr style="text-align: left">
                    <td colspan="1">CASH: <?php echo $_SESSION['cash']; ?></td>
                    <td colspan="3"></td>
                </tr>
                <tr style="text-align: left">
                    <td colspan="1">CHANGE: <?php echo $_SESSION['cash'] - $sum; ?></td>
                    <td colspan="3"></td>
                </tr>
            </table>
        </center>
    </div>  <!-- fullContainer -->
</body>
</html>

This works when I just put a line in the loadHTML () statement, but it returns me an error when I put a PHP file or even static HTML, but static HTML will return line # 3 instead of 4.

+5
source share
3 answers

After much testing, I came to the conclusion that DOMPDF really does not like empty tags.

Example:

<tr></tr><tr></tr><tr></tr><tr></tr>
<tr></tr><tr></tr><tr></tr><tr></tr>
<tr></tr><tr></tr><tr></tr><tr></tr>

There were some of these things in my code too (for creating a POC prototype), and when I deleted them, I got a table for rendering correctly

(I believe DomPDF chokes cannot calculate empty tag sizes)

Hope this helps

Screen caps of the problem, the fix and the result

+7
source

β„– 1 . , HTML-.

HTML ,

<table>
   <tr>Random text here</tr>
</table>

<table>
   <tr><td>Random text here</td></tr>
</table>
+1

Please make sure that you do not have empty tags or tags, or that you are not assigning any CSS class to tags.

0
source

Source: https://habr.com/ru/post/1672850/


All Articles