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

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

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代码

  1. <?php
  2. namespace Dompdf;
  3. require_once 'dompdf/autoload.inc.php';
  4. if(isset($_POST['submit']))
  5. {
  6. $dompdf = new Dompdf();
  7. $dompdf->loadHtml('
  8. <html>
  9. for ($i=0;$i<count($_POST['servproname']);$i++) {
  10. <table>
  11. <tr>
  12. <td style="text-align:left; border:1px solid #cccccc;">'.$_POST['servproname'][$i].'</td>
  13. <td style="text-align:left; border:1px solid #cccccc;">'.$_POST['docname'][$i].'</td>
  14. <td style="text-align:center; border:1px solid #cccccc;">'.$_POST['price1'][$i].'</td>
  15. <td style="text-align:center; border:1px solid #cccccc;">'.$_POST['qty'][$i].'</td>
  16. <td style="text-align:center; border:1px solid #cccccc;">'.$_POST['tot4'][$i].'</td>
  17. </tr>
  18. </table>
  19. }
  20. </html>');
  21. $dompdf->setPaper('A4', 'landscape');
  22. $dompdf->render();
  23. $dompdf->stream("",array("Attachment" => false));
  24. exit(0);
  25. }
  26. ?>
英文:

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

  1. &lt;?php
  2. namespace Dompdf;
  3. require_once &#39;dompdf/autoload.inc.php&#39;;
  4. if(isset($_POST[&#39;submit&#39;]))
  5. {
  6. $dompdf = new Dompdf();
  7. $dompdf-&gt;loadHtml(&#39;
  8. &lt;html&gt;
  9. for ($i=0;$i&lt;count($_POST[&#39;servproname&#39;]);$i++) {
  10. &lt;table&gt;
  11. &lt;tr&gt;
  12. &lt;td style = &quot;text-align:left; border:1px solid #cccccc;&quot;&gt;&#39;.$_POST[&#39;servproname&#39;][$i].&#39;&lt;/td&gt;
  13. &lt;td style = &quot;text-align:left; border:1px solid #cccccc;&quot;&gt;&#39;.$_POST[&#39;docname&#39;][$i].&#39;&lt;/td&gt;
  14. &lt;td style = &quot;text-align:center; border:1px solid #cccccc;&quot;&gt;&#39;.$_POST[&#39;price1&#39;][$i].&#39;&lt;/td&gt;
  15. &lt;td style = &quot;text-align:center; border:1px solid #cccccc;&quot;&gt;&#39;.$_POST[&#39;qty&#39;][$i].&#39;&lt;/td&gt;
  16. &lt;td style = &quot;text-align:center; border:1px solid #cccccc;&quot;&gt;&#39;.$_POST[&#39;tot4&#39;][$i].&#39;&lt;/td&gt;
  17. &lt;/tr&gt;
  18. }
  19. &lt;/table&gt;
  20. &#39;);
  21. $dompdf-&gt;setPaper(&#39;A4&#39;, &#39;landscape&#39;);
  22. $dompdf-&gt;render();
  23. $dompdf-&gt;stream(&quot;&quot;,array(&quot;Attachment&quot; =&gt; false));
  24. exit(0);
  25. }
  26. ?&gt;

答案1

得分: 3

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

  1. namespace Dompdf;
  2. require_once 'dompdf/autoload.inc.php';
  3. if (isset($_POST['submit'])) {
  4. $dompdf = new Dompdf();
  5. $tables = '';
  6. for ($i = 0; $i < count($_POST['servproname']); $i++) {
  7. $tables .= "<table>
  8. <tr>
  9. <td style=\"text-align:left; border:1px solid #cccccc;\">{$_POST['servproname'][$i]}</td>
  10. <td style=\"text-align:left; border:1px solid #cccccc;\">{$_POST['docname'][$i]}</td>
  11. <td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['price1'][$i]}</td>
  12. <td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['qty'][$i]}</td>
  13. <td style=\"text-align:center; border:1px solid #cccccc;\">{$_POST['tot4'][$i]}</td>
  14. </tr>
  15. </table>";
  16. }
  17. $dompdf->loadHtml("<html> $tables </html>");
  18. $dompdf->setPaper('A4', 'landscape');
  19. $dompdf->render();
  20. $dompdf->stream("", array("Attachment" => false));
  21. exit(0);
  22. }

正如你所看到的,我创建了一个名为**$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:

  1. namespace Dompdf;
  2. require_once &#39;dompdf/autoload.inc.php&#39;;
  3. if (isset($_POST[&#39;submit&#39;])) {
  4. $dompdf = new Dompdf();
  5. $tables = &#39;&#39;;
  6. for ($i = 0; $i &lt; count($_POST[&#39;servproname&#39;]); $i++) {
  7. $tables .= &quot; &lt;table&gt;
  8. &lt;tr&gt;
  9. &lt;td style=\&quot;text-align:left; border:1px solid #cccccc;\&quot;&gt;{$_POST[&#39;servproname&#39;][$i]}&lt;/td&gt;
  10. &lt;td style=\&quot;text-align:left; border:1px solid #cccccc;\&quot;&gt;{$_POST[&#39;docname&#39;][$i]}&lt;/td&gt;
  11. &lt;td style=\&quot;text-align:center; border:1px solid #cccccc;\&quot;&gt;{$_POST[&#39;price1&#39;][$i]}&lt;/td&gt;
  12. &lt;td style=\&quot;text-align:center; border:1px solid #cccccc;\&quot;&gt;{$_POST[&#39;qty&#39;][$i]}&lt;/td&gt;
  13. &lt;td style=\&quot;text-align:center; border:1px solid #cccccc;\&quot;&gt;{$_POST[&#39;tot4&#39;][$i]}&lt;/td&gt;
  14. &lt;/tr&gt;
  15. &lt;/table&gt;&quot;;
  16. }
  17. $dompdf-&gt;loadHtml(&quot;&lt;html&gt; $tables &lt;/html&gt;&quot;);
  18. $dompdf-&gt;setPaper(&#39;A4&#39;, &#39;landscape&#39;);
  19. $dompdf-&gt;render();
  20. $dompdf-&gt;stream(&quot;&quot;, array(&quot;Attachment&quot; =&gt; false));
  21. exit(0);
  22. }

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:

确定