Dom pdf failed to created desired out

I have html form, upon click on submit button, i am trying to create pdf report by using dom pdf. everything is working fine but for loop section creating issue. can i have help from this prestigious forum to fix this issue ?? here is my code to convert post values of form into pdf with dompdf

``` `$dompdf = new Dompdf(); 
              $dompdf->loadHtml('
            <table class="center" border=0  width=450>
           <tr><td><b>Mrno : </td><td>'.$_POST['mrno'].'</td><td><b>Name : </td><td>'.$_POST['name1'].'</td></tr>
            <tr><td><b>Bill No : </td><td>'.$_POST['bilno'].'</td><td><b>Bill Date : </td><td>'.date('d-M-Y',               strtotime($_POST['bildate'])).'</td></tr>
           <tr><td><b>Phone No : </td><td>'.$_POST['mobno'].'</td><td><b>Mod of Payment : </td><td>'.$_POST['modpyment'].'</td>                   </tr>
           </table>
                <br>
             <table class="center" border=1  width=600>
               <?php 
               for ($i=0;$i<count($_POST['servproname']);$i++) {
             echo "<tr>";
              echo "<td>".$_POST['servproname'][$i]."</td>";
              echo "<td>".$_POST['discunt'][$i]."</td>";
              echo "<td>".$_POST['price1'][$i]."</td>";
              echo "<td>".$_POST['qty'][$i]."</td>";
              echo "<td>".$_POST['tot4'][$i]."</td>";
                 } 
                    ?>
                  </table>
                      ');
                   $dompdf->setPaper('A4', 'portrait');
                       $dompdf->render();
                   $dompdf->stream("",array("Attachment" => false));
                exit(0);
                       }
                      ?>` ```

What sort of issue is it creating?

its not displaying pdf and gave following error :slight_smile:
Parse error : syntax error, unexpected ‘servproname’ (T_STRING), expecting ‘,’ or ‘)’ in C:\xampp\htdocs\abc\Invoice.php on line 103

It’s a quoting issue, surely? You haven’t closed the string that you open for the call to loadHTML() before you open a new PHP tag and try to run a for() loop. Or do you intend that the raw PHP code will be inside your string?

The single quote at the start of that array index name is being taken as the closing quote in the loadHTML() call, to clarify. Wasn’t there something very similar on here last week? This, perhaps: Error while converting phphtml form into pdf using dompdf - #4 by droopsnoot

i thnk load html cant handle array in dompdf thats why i am getting this error

I doubt it. What’s wrong with @droopsnoot’s answer?

Edit. It doesn’t help that you haven’t told us which line is 103.

No, it’s because you don’t close the quotes before trying to execute some more PHP. I don’t know anything about loadHTML(), but I can’t imagine it will execute PHP code inside the string you’re sending it, that sounds like a terrible security flaw if it does. (ETA - if it’s the one on Github, it doesn’t execute the code.)

To clarify, this line:

<table class="center" border=1  width=600>

should have a closing single-quote and a semi-colon on the end. I also prefer to see consistency in code - if you’re surrounding the class-name with double-quotes, do the same for the border and width parameters.

Inside your loop, do you actually want to echo those values, or do you want to add them onto the end of the string that you pass into loadHTML()? If the latter, you’d be better building up the string in a variable, and then calling loadHTML() at the end of your code when it contains everything.

this is 103 line
for ($i=0;$i<count($_POST[‘servproname’]);$i++) {

can u provide example, so that i can understand clearly if u can then i will be extremly thankful to you. below is the output which i want to achieve
Capture

I’d just do something like this:

$content = " All the stuff you have in your string before the for-loop starts"
for ($i=0;$i<count($_POST['servproname']);$i++) {
   $content .= "each HTML table row that you currently echo";
   }
$content .= "</table>";
$dompdf->loadHtml($content);
... and then the rest of your code

still not working, can u write ful example of my code if possible, will be extremly thankful to you

No one is going to do your work for you. Show us the complete modified code you have and we can see where you’re going wrong.

Show us your new code, and please don’t use phrases like “still not working” - tell us exactly what is wrong, what error messages you get.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.