首页 > 解决方案 > TCPDF writeHTML 边距/填充样式不起作用

问题描述

我已经在我的 Magento 2 应用程序中实现了 TCPDF。我所做的是创建一个 HTML 文件并将其加载到我的控制器中的 TCPDF 实例中。我在我的 HTML 标记中添加了所有样式作为内联样式。但是在加载样式时无法识别。不知道其他样式(例如背景颜色和字体大小)是否可以正常工作。但是当我尝试在我的 Div 标签中添加边距或填充时,它仍然没有生效。知道如何加载我的 HTML 并加载 css 吗?下面是我的 HTML 标记和我的控制器中的 tcpdf 代码

HTML 标记

    $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, '');   

现在我尝试在我的 HTML 标签中添加内联样式,例如添加填充或边距,但它没有生效。我也尝试在隐身模式中查看它,但无济于事。关于如何解决这个问题的任何想法?

标签: phphtmlmagento2tcpdfmagento-2.3

解决方案


改用 SetMargin

例子 :

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

推荐阅读