简体   繁体   中英

HTML2Pdf content is overflowing out of page

I am trying to generate a PDF from an HTML File using the HTML2PDF library.

My HTML :

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
    <td align="center" valign="top">
        <table width="968" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
                <td align="center" valign="top">
                    <strong>Invoice </strong>
                </td>
            </tr>
            <tr>
                <td align="right" valign="top">
                    <span style="text-align:right;">#24638 </span>
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    <table width="968" border="0" align="center" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="50%" align="left" valign="top">
                                <table width="500" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                        <td align="left" valign="top">To</td>
                                    </tr>
                                    <tr>
                                        <td align="left" valign="top">test user
                                            <br />
                                             Some Company22
                                            <br />
                                            My Address
                                            <br />
                                             Line 2
                                            <br />
                                            City, 487030
                                            <br />
                                            State, Country
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td width="50%" align="left" valign="top">&nbsp;</td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    <table width="968" border="1" cellspacing="1" cellpadding="1">
                        <tr>
                            <td width="17%" align="center" valign="top">Transaction Date</td>
                            <td width="6%" align="center" valign="top">User ID</td>
                            <td width="26%" align="center" valign="top">UserName</td>
                            <td width="9%" align="center" valign="top">Order</td>
                            <td width="21%" align="center" valign="top">Tax </td>
                            <td width="21%" align="center" valign="top">Tax Description</td>
                        </tr>
                        <tr>
                            <td align="center" valign="top">04-11-2015</td>
                            <td align="center" valign="top">137</td>
                            <td align="center" valign="top">saurabh.pande@dfordomains.com</td>
                            <td align="center" valign="top">24727</td>
                            <td align="center" valign="top">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</td>
                            <td align="center" valign="top">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Mauris placerat </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    <table width="968" border="1" align="left" cellpadding="1" cellspacing="1">
                        <tr>
                            <td width="49%" align="center" valign="top">Description</td>
                            <td width="27%" align="center" valign="top">Invoice Amount</td>
                            <td width="24%" align="center" valign="top">Pending Amount</td>
                        </tr>
                        <tr>
                            <td align="center" valign="top">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo</td>
                            <td align="center" valign="top">USD 5.98 </td>
                            <td align="center" valign="top">USD 0 </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td align="left" valign="top">
                    <table width="500" border="1" cellspacing="1" cellpadding="1">
                        <tr>
                            <td width="51%" align="left" valign="top">Foreign Exchange Rate when Invoice was paid (EUR To USD) </td>
                            <td width="49%" align="left" valign="top">Mauris placerat </td>
                        </tr>
                        <tr>
                            <td align="left" valign="top">Total Amount</td>
                            <td align="left" valign="top">USD 5.98</td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td align="left" valign="top">&nbsp;</td>
            </tr>
            <tr>
                <td align="left" valign="top">&nbsp;</td>
            </tr>
        </table>
    </td>
</tr>

I use this code to gerate the PDF :

$pdfdata = myHtml() // returns my generated HTML
require_once(__DIR__.'path/to/html2pdf/html2pdf.class.php');
try{
    $html2pdf = new HTML2PDF('P','A4','en');
    $html2pdf->WriteHTML($pdfdata);
    $html2pdf->Output('transaction.pdf');
}catch(Exception $e){
    echo $e->getMessage();
}

However in the Output the table width exceeds the page width and half the content is not visible. 在此处输入图片说明

you can use the <page> it is bordered to your pdf pages

<page backtop="30mm" backbottom="15mm" backleft="5mm" backright="10mm" pagegroup="new" style="font-family: freeserif;">     
    <page_header>                 
    </page_header>
    <page_footer style="text-align: center;">
    </page_footer>
</page>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM