0

I'm trying to create a pdf that receives data via POST,I know that the data is being received because i tested using "var_dump($_POST)".

Result:

array (size=9)
 'orcCar' => string 'S' (length=1)
 'contItem' => 
  array (size=1)
  0 => string '1' (length=1)
 'codProduto' => 
  array (size=1)
  0 => string '000zxxxxxxx' (length=14)
 'qtdProduto' => 
  array (size=1)
  0 => string '20' (length=2)
'prcuProduto' => 
array (size=1)
  0 => string '4.28' (length=4)
'prctProduto' => 
array (size=1)
  0 => string '85.60' (length=5)
'descProduto' => 
array (size=1)
  0 => string 'sdsudhudud' (length=33)
'countNitens' => string '2' (length=1)
'codClientecopia' => string '' (length=0)

But when i try to use it in the middle of the html code or in a loop it wont work.

This is part of the code:

  for($i=0; $i < count($_POST["codProduto"]); $i++)
  {
     if ($_POST["prcuProduto"][$i]=="")
     {
     $_POST["prcuProduto"][$i] = '0';
     }
  $contador=$_POST["contItem"][$i];

  // Set some content to print

  $html.="<tr>
  <td style='width:5%;'><input type='number' name='contItem[]' 
  style='width:100%'id='contItem' readonly='readonly' value=".$contador." 
  maxlength='5'></td>

  <td style='width:20%;'><input type='text' name='codProduto[]'  
  style='width:100%'id='codProduto' readonly='readonly'  maxlength='20' 
  value=". $_POST['codProduto'][$i]."></td>";

   }
   // Print text using writeHTMLCell()
  $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);`enter code here`

It won't enter the loop because of

count($_POST["codProduto"])

when changed with value it works, but it still won't show any values or the "td".I also tried creating variables with the values from the post but it still didin't work.

Could someone help me how to use velues recived from post in tcpdf?

cthulhu
  • 169
  • 14
  • you're not closing a `"` there after `` and no `;` - is this the original code? – Jeff Sep 06 '18 at 20:13
  • _"I tested using var_dump($_POST)"_ - what was the result/ouput? Please show that! – Jeff Sep 06 '18 at 20:15
  • @jeff sorry i forgot to close when i cut and pasted the code, but in the original code it is corretly closed. – cthulhu Sep 06 '18 at 20:24
  • I suppose you have a `$html="";` before the loop? – Jeff Sep 07 '18 at 10:46
  • The code should produce one table row, indeed. Have you tries echoing $html? Same result? – Jeff Sep 07 '18 at 10:48
  • _sidenote_: you shouldn't have multiple inputs with the same id. – Jeff Sep 07 '18 at 10:49
  • i have tried echoing it, there was no input with the same id, and there was no $htm='' "; but it stil printed parts that didnt had the post. i tried adding that but didi'nt change unfortunally. – cthulhu Sep 09 '18 at 22:56

1 Answers1

1

I recreated your POST object and it enters the loop just fine for me. There's a couple of things to note here.

First, input is not a supported tag for the TCPDF HTML parser. If you're just wanting to add boxes around the field values, then add a border to the td instead.

Secondly, TCPDF's HTML parser is pretty fragile. You need to make sure you include all the necessary HTML tags. For example, in your code the contents of $html are not wrapped in a table tag, and there's no </tr> tag for the rows. TCPDF also needs all HTML attributes to be wrapped in double quotes.

In my tests with TCPDF 6.2.17, the following snippet works:

$html = '<table cellpadding="2">';
//I'm adding a border on the cells, and TCPDF doesn't support CSS padding
//so we'll use table's cellpadding attribute. Not strictly required, but
//I thought it looked nice.
for($i=0; $i < count($_POST["codProduto"]); $i++)
{
     if ($_POST["prcuProduto"][$i]=="")
     {
     $_POST["prcuProduto"][$i] = '0';
     }
     $contador=$_POST["contItem"][$i];

  // Set some content to print

  $html.="<tr>
  <td style=\"width:5%; border: 1px solid black; \">$contador</td>

  <td style=\"width:20%; border: 1px solid black; \">{$_POST['codProduto'][$i]}</td></tr>";
  //Make sure we have our ending </tr> tag and wrap the style attributes in double
  //quotes so TCPDF will actually pay attention to them.
   }
  $html .= '</table>';
  // End our table and print text using writeHTMLCell()
  $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
EPB
  • 3,939
  • 1
  • 24
  • 26