在使用dompdf将PHP HTML表单转换为PDF时出现的错误消息。

huangapple go评论71阅读模式
英文:

error message in coverting php html form in pdf using dompdf

问题

我尝试在单击提交按钮后使用dompdf将我的PHP/HTML表单转换为PDF。一切都正常工作,但当我使用for循环时,出现以下错误:

解析错误:语法错误,意外的 'servproname' (T_STRING),期望 ',' 或 ')' 在 C:\xampp\htdocs\abc\invoice.php

这是我的PHP代码

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

if(isset($_POST['submit']))
{
    $dompdf = new Dompdf(); 
    $dompdf->loadHtml('
        <html>
            for ($i=0;$i<count($_POST['servproname']);$i++) {
                <table>
                    <tr>
                        <td style="text-align:left; border:1px solid #cccccc;">'.$_POST['servproname'][$i].'</td>   
                        <td style="text-align:left; border:1px solid #cccccc;">'.$_POST['docname'][$i].'</td>
                        <td style="text-align:center; border:1px solid #cccccc;">'.$_POST['price1'][$i].'</td>
                        <td style="text-align:center; border:1px solid #cccccc;">'.$_POST['qty'][$i].'</td>
                        <td style="text-align:center; border:1px solid #cccccc;">'.$_POST['tot4'][$i].'</td>
                    </tr>
                </table>
            }
        </html>');
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();
    $dompdf->stream("",array("Attachment" => false));
    exit(0);
}
?>
英文:

i am trying to convert my PHP/HTML form into pdf by using dompdf after clicking submit button. Everything was working fine but when I used for loop then following error occuered :

> Parse error: syntax error, unexpected 'servproname' (T_STRING), expecting ',' or ')' in C:\xampp\htdocs\abc\invoice.php

here is my php code

&lt;?php
                       namespace Dompdf;
                    require_once &#39;dompdf/autoload.inc.php&#39;;

              if(isset($_POST[&#39;submit&#39;]))
                {
               $dompdf = new Dompdf(); 
                $dompdf-&gt;loadHtml(&#39;
                   &lt;html&gt;
                    for ($i=0;$i&lt;count($_POST[&#39;servproname&#39;]);$i++) {
                  &lt;table&gt;
                &lt;tr&gt;
           &lt;td style = &quot;text-align:left; border:1px solid #cccccc;&quot;&gt;&#39;.$_POST[&#39;servproname&#39;][$i].&#39;&lt;/td&gt;   
          &lt;td style = &quot;text-align:left; border:1px solid #cccccc;&quot;&gt;&#39;.$_POST[&#39;docname&#39;][$i].&#39;&lt;/td&gt;
          &lt;td style = &quot;text-align:center; border:1px solid #cccccc;&quot;&gt;&#39;.$_POST[&#39;price1&#39;][$i].&#39;&lt;/td&gt;
         &lt;td style = &quot;text-align:center; border:1px solid #cccccc;&quot;&gt;&#39;.$_POST[&#39;qty&#39;][$i].&#39;&lt;/td&gt;
          &lt;td style = &quot;text-align:center; border:1px solid #cccccc;&quot;&gt;&#39;.$_POST[&#39;tot4&#39;][$i].&#39;&lt;/td&gt;
                   &lt;/tr&gt;
                          }
                      &lt;/table&gt;
                         &#39;);
                  $dompdf-&gt;setPaper(&#39;A4&#39;, &#39;landscape&#39;);
                   $dompdf-&gt;render();
                    $dompdf-&gt;stream(&quot;&quot;,array(&quot;Attachment&quot; =&gt; false));
                  exit(0);
                }
             ?&gt;

答案1

得分: 3

你的语法使用方式完全错误,将PHP嵌入字符串中?当你使用单引号(')或双引号(")时,PHP会将其视为字符串而不是要解释的代码。
你应该这样做:

namespace Dompdf;
require_once 'dompdf/autoload.inc.php';

if (isset($_POST['submit'])) {
    $dompdf = new Dompdf();
    $tables = '';

    for ($i = 0; $i < count($_POST['servproname']); $i++) {
        $tables .= "<table>
            <tr>
                <td style=\"text-align:left; border:1px solid #cccccc;\">{$_POST['servproname'][$i]}</td>   
                <td style=\"text-align:left; border:1px solid #cccccc;\">{$_POST['docname'][$i]}</td>
                <td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['price1'][$i]}</td>
                <td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['qty'][$i]}</td>
                <td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['tot4'][$i]}</td>
            </tr>
        </table>";
    }
    $dompdf->loadHtml("<html> $tables </html>");
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();
    $dompdf->stream("", array("Attachment" => false));
    exit(0);
}

正如你所看到的,我创建了一个名为**$tables**的变量,它是你想要生成的字符串。

英文:

The way you are using syntax is totally wrong, using PHP in strings? when you open a ' or ", PHP recognizes that as a string not a code to interpret.
You should do something like this:

namespace Dompdf;
require_once &#39;dompdf/autoload.inc.php&#39;;

if (isset($_POST[&#39;submit&#39;])) {
   $dompdf = new Dompdf();
   $tables = &#39;&#39;;

   for ($i = 0; $i &lt; count($_POST[&#39;servproname&#39;]); $i++) {
       $tables .= &quot; &lt;table&gt;
           &lt;tr&gt;
               &lt;td style=\&quot;text-align:left; border:1px solid #cccccc;\&quot;&gt;{$_POST[&#39;servproname&#39;][$i]}&lt;/td&gt;   
               &lt;td style=\&quot;text-align:left; border:1px solid #cccccc;\&quot;&gt;{$_POST[&#39;docname&#39;][$i]}&lt;/td&gt;
               &lt;td style=\&quot;text-align:center; border:1px solid #cccccc;\&quot;&gt;{$_POST[&#39;price1&#39;][$i]}&lt;/td&gt;
               &lt;td style=\&quot;text-align:center; border:1px solid #cccccc;\&quot;&gt;{$_POST[&#39;qty&#39;][$i]}&lt;/td&gt;
               &lt;td style=\&quot;text-align:center; border:1px solid #cccccc;\&quot;&gt;{$_POST[&#39;tot4&#39;][$i]}&lt;/td&gt;
           &lt;/tr&gt;
       &lt;/table&gt;&quot;;
   }
   $dompdf-&gt;loadHtml(&quot;&lt;html&gt; $tables &lt;/html&gt;&quot;);
   $dompdf-&gt;setPaper(&#39;A4&#39;, &#39;landscape&#39;);
   $dompdf-&gt;render();
   $dompdf-&gt;stream(&quot;&quot;, array(&quot;Attachment&quot; =&gt; false));
   exit(0);
}

As you can see i made a variable called $tables which is a string of what you where trying to make.

huangapple
  • 本文由 发表于 2023年7月10日 16:21:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76651950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定