3

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?

MadzQuestioning
  • 3,341
  • 8
  • 45
  • 76
  • 1
    I am having the same issue, Did you find a solution? – Disapamok Feb 07 '22 at 11:01
  • I just created a template and loaded that html. Then I made sure that I always clear my cache and do a static content deploy. I added an inpage styling. TBH I was not sure what happen but when I moved it to separate html file it worked – MadzQuestioning Feb 07 '22 at 11:37
  • https://stackoverflow.com/questions/3404095/tcpdf-not-render-all-css-properties – MadzQuestioning Feb 07 '22 at 11:38
  • https://hotexamples.com/examples/-/TCPDF/SetMargins/php-tcpdf-setmargins-method-examples.html – MadzQuestioning Feb 07 '22 at 11:40
  • 1
    I'm using a separate php file and included it in the function, Tried using inline and internal CSS but padding or margin do not work. Tried clearing the cache too. – Disapamok Feb 08 '22 at 03:12

1 Answers1

-3

use SetMargin instead

example :

$pdf->SetMargins(10, 20, 10, true);
$pdf->AddPage();
Vel
  • 9,027
  • 6
  • 34
  • 66
  • This only applie to the page margin. I want to add margin to every element like the Div tags and all sort – MadzQuestioning Mar 13 '20 at 11:54
  • 1
    Having a look at the source code, we can see there's a protected method called TCPDF::getHtmlDomArray() that, among other things, parses CSS declarations. I can see stuff like font-family, list-style-type or text-indent but there's no margin or padding as far as I can see and, definitively, there's no float at all. TCPDF, you can use CSS for some basic formatting. If you need to convert from HTML to PDF, it's the wrong tool. (If that's the case, may I suggest wkhtmltopdf?) – John Paulo Cruz Apr 07 '20 at 20:08