1

I am using TCPDF Library to get PDF Downloadable. I have to pass retrieved data through loops to single vaiable as:

$tcpdf->writeHTML($html, true, false, true, false, '');

I have to pass these values to $html varibable:

$html = '<h1>'.'Order Number:    '.$OrderId.'<br />Customer Name:    '.$customerName.'<br />Item Name:    '.$itemName.'</h1>';

but I have multiple records and have to pass this information many time through loops.

This is the table I want to pass from template to $html variable(PDF object):

<h2 align ="center" bgcolor ="#75777a">Order Pick List</h2>
<table>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
<?php $Orders= $block->getOrders();
    foreach ($Orders as $order) {
        $Colone = $order->getID();
        $Coltwo = $order->getName();
        $items = $order->getitems();?>

        <tr>
            <td><?php echo $Colone; ?></td>
            <td><?php echo $Coltwo; ?></td>

            <?php foreach ($items as $item ) {
                $itemName = $item->getitemName();?>

                <td>
                    <?php echo $itemName; ?>
                </td>

            <?php } ?>
        </tr>

    <?php}
 ?>

Any help should be appreciated.

1

Try to append your next iteration in $html variable by using dot ( . )

$html='';
foreach(-------------)
{
$html . = <table>      
<?php if (is_array($t->a)) foreach($t->a as $k => $v) { ?><tr>
<td>k is <?php echo htmlspecialchars($t->k); ?>, and v is <?php echo 
htmlspecialchars($t->v); ?></td>
</tr><?php } ?>
</table>
}
1
  • Thank you @sheraz. The idea worked! but I am unable to use php code in here as it is .php file
    – Ajwad Syed
    Oct 15 '18 at 11:45
0

just create one .phtml file your all HTML content in your module view file. Render you html as you want in your custom made phtml file.

Just pass your collection or your data to phtml file like below to process from your controller or anywhere you call this block.

$this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::test.phtml")->setYourArrayData($arrayData)->toHtml();

you can get that param to process in your phtml like

   $ArrayofMineData = $block->getYourArrayData();
foreach ($ArrayofMineData as $data){
/* generate your view here */
}

Now you have full HTML to pass it to your $pdf objcet

    $html='';
$html = $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::test.phtml")->setYourArrayData($arrayData)->toHtml();
$tcpdf->writeHTML($html, true, false, true, false, '');
5
  • Thank you for reply,@Zaheerabbs when I followed this approach an empty pdf file is genarating. Can you explain a bit more ? I think there is some problem with template code..I am trying to pass a table and generating its rows by foreach loop.
    – Ajwad Syed
    Oct 15 '18 at 13:24
  • 1
    can you please paste your template file code here.Also you can debug in your controller just dump your return HTML in your controller are you getting HTML or its null or empty.if anything break up in html like any tag that missing closing element cause the issue. TCPDF will generate empty in this case. Oct 15 '18 at 14:29
  • I have updated the template table which I want to pass to controller as $php object. Its function names are dummy names !
    – Ajwad Syed
    Oct 16 '18 at 4:06
  • it seems table has no ending tag. can you please check this and try.also td count not match with th defined in header. Oct 16 '18 at 6:24
  • after reviewing template, PDF file is still opened as blank document! I followed this link webkul.com/blog/create-block-controller-magento-2 and got some garbage data on pdf file! but I am getting my desired pdf output file by directly using same html code in a controller function and passing it to $pdf Object without adding block.
    – Ajwad Syed
    Oct 16 '18 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.