Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dompdf Error: "The Row #4 could not be found" when converting PHP file to PDF

Tags:

php

mysql

dompdf

I am trying to generate a dynamic PDF out of a PHP page and it gives me this error:

Fatal error: Uncaught exception 'Dompdf\Exception' with message 'The row #4 could not be found, please file an issue in the tracker with the HTML code' in C:\xampp\htdocs\Gokujou\dompdf\src\Cellmap.php:417 Stack trace: #0 C:\xampp\htdocs\Gokujou\dompdf\src\FrameReflower\TableRow.php(62): Dompdf\Cellmap->get_frame_height(Object(Dompdf\FrameDecorator\TableRow))  #1 C:\xampp\htdocs\Gokujou\dompdf\src\FrameDecorator\AbstractFrameDecorator.php(893): Dompdf\FrameReflower\TableRow->reflow(NULL) #2 C:\xampp\htdocs\Gokujou\dompdf\src\FrameReflower\TableRowGroup.php(51): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow() #3 C:\xampp\htdocs\Gokujou\dompdf\src\FrameDecorator\AbstractFrameDecorator.php(893): Dompdf\FrameReflower\TableRowGroup->reflow(NULL) #4 C:\xampp\htdocs\Gokujou\dompdf\src\FrameReflower\Table.php(488): Dompdf\FrameDecorator\AbstractFrameDecorator->reflow() #5 C:\xampp\htdocs\Gokujou\dompdf\src\FrameDecorator\AbstractFrameDecorator.php(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 where it presses "checkout", which would generate a PDF page:

<?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');
?>

The receipt.php file is:

<?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>

It works when I just put a string in the loadHTML() statement, but it returns an error when I put a PHP file or even a static HTML, but static HTML would return row #3 instead of #4.

like image 448
phpnewbs Avatar asked Mar 21 '17 17:03

phpnewbs


3 Answers

After much testing, I came to the conclusion that DOMPDF really dislikes empty <tr> 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>

My code had some of these things too (for prototyping a POC) and when I removed them I got the table to render correctly.

(I am guessing DomPDF chokes when it can't calculate the dimensions of the empty <tr> tags).

Screen caps of the problem, the fix and the result

like image 162
David Lundquist Avatar answered Sep 22 '22 00:09

David Lundquist


This error occurs because of HTML, the reason may be different like missing closing tags, or conditional rending where HTML is generated conditionally. In my case, I was rendering HTML conditionally using if where my tag was before if and closing tag was in if, so it seemed to be ok initially but when HTML was generated conditionally it left unclosed tags which were causing the error in DOM PDF. Here is my code that was throwing an exception.

<tr>
@if (isset($some_condition))                    
<td><b>15</b></td>
<td><b>FIRST AID KIT</b></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'NA' : false) checked @endif></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'Yes' : false) checked @endif></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'NEEDSIMP' : false) checked @endif></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'No' : false) checked @endif></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'GC' : false) checked @endif></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'other' : false) checked @endif></td>
<td colspan="2"> {{ isset($obj->first_aid_kit) ?$safetyLog->first_aid_kit['details'] : '' }}</td>
</tr>
@endif

I just put tag inside if, and it fixed it, here is the code after debugging.

@if (isset($some_condition))
<tr>
<td><b>15</b></td>
<td><b>FIRST AID KIT</b></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'NA' : false) checked @endif></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'Yes' : false) checked @endif></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'NEEDSIMP' : false) checked @endif></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'No' : false) checked @endif></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'GC' : false) checked @endif></td>
<td><input type="checkbox" @if(isset($obj->first_aid_kit) ? $obj->first_aid_kit['options'] == 'other' : false) checked @endif></td>
<td colspan="2"> {{ isset($obj->first_aid_kit) ?$safetyLog->first_aid_kit['details'] : '' }}</td>
</tr>
@endif

Hope my answer would help this community.

like image 30
Muhammad Binyameen Malik Avatar answered Sep 20 '22 00:09

Muhammad Binyameen Malik


The row #1 could not be found, please file an issue in the tracker with the HTML code..

The HTML error was that i had:

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

What fixed it is:

<table>
   <tr><td>Random text here</td></tr>
</table>
like image 28
Rayan Avatar answered Sep 21 '22 00:09

Rayan