php – TCPDF writeHTML margin/padding styling not working-ThrowExceptions

Exception or error:

I have implemented TCPDF in my Magento 2 application. What I did was to create an HTML file and load it into my TCPDF instance in my controller. I added all the styling as inline style in my HTML markup. But upon loading the styling is not being recognized. Not sure what but other styling like background color and font size is working just fine. But when I try to add a margin or padding in my Div tag its still not taking effect. Any idea how I can load my HTML and load the css as well? Below is my HTML markup and my tcpdf code in my controller

HTML MARKUP

    $pdf = new \TCPDF(self::PAGE_ORIENTATION, self::PAGE_UNIT, [self::PAGE_SIZE_WIDTH, self::PAGE_SIZE_HEIGHT], true, 'UTF-8', false);
    $pdf->SetCreator("Creator");
    $pdf->SetAuthor('Author');
    $pdf->SetTitle('Print');
    $pdf->SetSubject('PDF');
    $pdf->SetKeywords('pdf);

    // set some language-dependent strings (optional)
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
        require_once(dirname(__FILE__).'/lang/eng.php');
        $pdf->setLanguageArray($l);
    }

    // remove default header/footer

    $pdf->SetMargins(0, 0, 0, 0);
    $pdf->SetAutoPageBreak(TRUE, 0);
    $pdf->setCellPaddings(0,0,0,0);
    $pdf->setFooterMargin(0);
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    $pdf->AddPage();
    $html = <<<EOF
    <div id="ecard-container">
        <table cellspacing="0" cellpadding="0">
            <tr>
                <td width="333" bgcolor="#6FFF66" height="270">
                    <div>
                        <img src="{$img}" width="220" height="270"/>
                    </div>
                </td>
                <td bgcolor="#990000" height="270">
                    <table>
                        <tr>
                            <td>
                                <div class="top-container">
                                    <h1>The title</h1>
                                    <div>Bought in your name from Gifts</div>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>Lorem Ipsum</td>
                        </tr>
                    </table>                
                </td>
            </tr>
            <tr>
                <td valign="bottom" bgcolor="#FFFFF0">
                    <div class="title" style="display: block;margin: 30px;">Thank you for saving a life today by providing medicine for a sick person in the developing world</div>
                </td>
                <td bgcolor="#CCCCCC" height="70">
                    <table>
                        <tr>
                            <td style="margin: 0.3in;padding: 0.3in;">
                                <div class="message"></div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <span>Order number: 100051905</span>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div>More info at www.goodgifts.org.</div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div>Tel: 020 7794 8000</div>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    EOF;
    $pdf->writeHTML($html, true, false, true, false, '');   

Now I tried adding inline style in my HTML tags like adding padding or margin but it’s not taking effect. I also tried viewing it in Incognito but to no avail. Any idea on how to fix this?

How to solve:

use SetMargin instead

example :
$pdf->SetMargins(10, 20, 10, true);
$pdf->AddPage();

Leave a Reply

Your email address will not be published. Required fields are marked *