PHP使用mpdf生成PDF文件

mpdf/mpdf 6.1.0 GitHub https://github.com/mpdf/mpdf

<?php

// 导出PDF

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \mPDF('utf-8');

// 支持中文
$mpdf->useAdobeCJK = true;
$mpdf->autoScriptToLang = true;
$mpdf->autoLangToFont = true;

$html = '<h1>你好 Cuber</h1>';

$html_page = '
<html>
<head>
<style>
    @page {
        size: auto;
        odd-header-name: html_myHeader1;
        even-header-name: html_myHeader2;
        odd-footer-name: html_myFooter1;
        even-footer-name: html_myFooter2;
    }
    @page chapter2 {
        odd-header-name: html_Chapter2HeaderOdd;
        even-header-name: html_Chapter2HeaderEven;
        odd-footer-name: html_Chapter2FooterOdd;
        even-footer-name: html_Chapter2FooterEven;
    }
    @page noheader {
        odd-header-name: _blank;
        even-header-name: _blank;
        odd-footer-name: _blank;
        even-footer-name: _blank;
    }
    div.chapter2 {
        page-break-before: right;
        page: chapter2;
    }
    div.noheader {
        page-break-before: right;
        page: noheader;
    }
</style>
</head>

<body>

    <htmlpageheader name="myHeader1" style="display:none">
        <div style="text-align: right; border-bottom: 1px solid #000000; font-weight: bold; font-size: 10pt;">
            My document
        </div>
    </htmlpageheader>

    <htmlpageheader name="myHeader2" style="display:none">
        <div style="border-bottom: 1px solid #000000; font-weight: bold;  font-size: 10pt;">
            My document
        </div>
    </htmlpageheader>

    <htmlpagefooter name="myFooter1" style="display:none">
        <table width="100%">
            <tr>
                <td width="33%">
                    <span style="font-weight: bold; font-style: italic;">{DATE j-m-Y}</span>
                </td>
                <td width="33%" align="center" style="font-weight: bold; font-style: italic;">
                    {PAGENO}/{nbpg}
                </td>
                <td width="33%" style="text-align: right;">
                    My document
                </td>
            </tr>
        </table>
    </htmlpagefooter>

    <htmlpagefooter name="myFooter2" style="display:none">
        <table width="100%">
            <tr>
                <td width="33%">My document</td>
                <td width="33%" align="center">{PAGENO}/{nbpg}</td>
                <td width="33%" style="text-align: right;">{DATE j-m-Y}</td>
            </tr>
        </table>
    </htmlpagefooter>

    <htmlpageheader name="Chapter2HeaderOdd" style="display:none">
        <div style="text-align: right;">Chapter 2</div>
    </htmlpageheader>

    <htmlpageheader name="Chapter2HeaderEven" style="display:none">
        <div>Chapter 2</div>
    </htmlpageheader>

    <htmlpagefooter name="Chapter2FooterOdd" style="display:none">
        <div style="text-align: right;">Chapter 2 Footer</div>
    </htmlpagefooter>

    <htmlpagefooter name="Chapter2FooterEven" style="display:none">
        <div>Chapter 2 Footer</div>
    </htmlpagefooter>

    Hello World

    <div class="chapter2">Text of Chapter 2</div>

    <div class="noheader">No-Header page</div>

</body>
</html>
';

$mpdf->WriteHTML($html);
$mpdf->AddPage();
$mpdf->WriteHTML($html);

$mpdf->Output();
<div class="post-text" itemprop="text"> <p>I am trying to figure out the problem in this, but not able to. Query is proper, Result i am getting. If i echo <code>$html</code> i get result, but nothing for <code>WriteHTML();</code></p> <p>Here is my code</p> <pre><code><?php error_reporting(E_ALL); ini_set('display_errors', 1); include("connect.php"); $id = $_GET['id']; include("mpdf/mpdf.php"); $mpdf=new mPDF('utf-8', 'A4'); $mpdf->debug = true; $mpdf->useOnlyCoreFonts = true; // false is default $mpdf->SetProtection(array()); $mpdf->SetTitle("My Company"); $mpdf->SetAuthor("MeAuthor"); $mpdf->SetWatermarkText("Demo"); $mpdf->showWatermarkText = false; $mpdf->watermark_font = 'DejaVuSansCondensed'; $mpdf->watermarkTextAlpha = 0.1; $mpdf->SetDisplayMode('fullpage'); $s1 = "SELECT * FROM mytable WHERE cid=".$id.""; $sql = mysqli_query($con, $s1) or die (mysqli_error($con)); $row = mysqli_fetch_array($sql); $html = ' <html> <head> <body> <!--mpdf <htmlpageheader name="myheader"> <table width="100%"><tr> <td width="80%" style="text-align: center; font-size: 16pt;">Company Name<br> <span style="font-size: 11pt;text-align: center;">Address 123, Lorel Ipsum, sdfsdf sdsdgsdg,</span><br> <span style="font-size: 11pt;text-align:center;">sdfdsfdsf, sfsdf - 100066 </td> </tr> </table> </htmlpageheader> <sethtmlpageheader name="myheader" value="on" show-this-page="1" /> <sethtmlpagefooter name="myfooter" value="off" /> mpdf--> <table width="100%"> <tr><td colspan="2" align="right" width="40%"> Vocher No : '.$id.' </td></tr> <tr> <td width="70%">Debit A/C. : '.$row['account_no'].' </td> <td width="30%" style="text-align: right">Date: '.date("d-m-Y").'</td></tr> </table> <div style="border: 1px solid black"> <table border="0" style="white-space:nowrap; border-spacing:0;" height="100%" cellspacing="5"><thead> <tr> <th width="30%" style="border-right:1px solid #000;">Name</th> <th width="20%" style="border-right:1px solid #000;">E-Mail</th> <th width="20%" style="border-right:1px solid #000;">Mobile</th> <th width="30%" style="border-right:1px solid #000;">Particulars</th> <th width="15%" style="border-right:1px solid #000;">RS.</th> <th width="5%" style="border-right:1px solid #000;">P.</th> </tr> </thead> <tr> <td style="border-right:1px solid #000;" width="30%" height="100px;" >'.$row['name'].'</td> <td style="border-right:1px solid #000;" width="20%">'.$row['email'].'</td> <td style="border-right:1px solid #000;" width="20%">'.$row['mobile'].'</td> <td style="border-right:1px solid #000;" width="30%">'.$row['purpose'].'</td> <td style="border-right:1px solid #000;" width="15%">'.$row['amount'].'</td> <td style="border-right:1px solid #000;" width="5%">00</td> </tr> <tr><td colspan="4" style="font-weight:bold;">Total Rs.</td> '.$row['amount'].'<td></tr> </table></div> <br> <table border="0"><tr><td style="width=50%;">Mode of Payment : '.$row['mode'].' </td><td width="50%">Date : '.date('Y-m-d').'</td></tr> <tr><td colspan="2">Rupees : </td></tr> </table> <br><br><br> <table width="100%" border="0"> <tr><td width="20%"> Hon. Secretary </td> <td width="20%">Hon. Treasurer</td> <td width="20%">Hon. Trustee</td> <td width="20%">Receiver s Signature</td></tr> </table> </body> </html> '; //echo $html; echo $mpdf->WriteHTML($html); //$mpdf->Output('Receipt.pdf','D'); $mpdf->Output(); ?> </code></pre> </div>
<div class="post-text" itemprop="text"> <p>I am having a php table which contains information about billing of certain numbers. After generating this table I export it in pdf using mpdf. If the table has a certain number of rows it breaks the page in a weird way, leading the headers repeat on the next page with no rows. Here is the format of the table:</p> <p>The table contains up to 3 headers inside <code><thead></code> tags, like this:</p> <pre><code><table class="items" width="100%" style="font-size: 9pt; border-collapse: collapse;" cellpadding="8"> <thead> <tr> <td width="30.3%">Forbrug</td> <td width="23.1%">Periode</td> <td width="10.76%">Tid/MB</td> <td width="9.28%">Enheder</td> <td width="12.9%" align="right">I alt</td> <td width="13.3%" align="right">I alt+<b>moms</b></td> </tr> </thead> </code></pre> <p>rows are added in a while from the database in this way.. nothing special</p> <pre><code><tr> <td>'.$produktNameFromDatabase.'</td> <td>'.$periodeFromDatabase.'</td> <td>'.$tidMbFromDatabase.'</td> <td>'.$enhederFromDatabase.'</td> <td align="right">'.number_format((float)$produktpris, 2, ',', '.').' kr.</td> <td align="right"><b>'.number_format((float)$produktpris*(1 + (0.25 * $moms)) - $momsDiscount, 2, ',', '.').' kr.</b></td> </tr> </code></pre> <p>with the total lines at the end</p> <pre><code> <tr> <td class="blanktotal" colspan="1" rowspan="6"></td> <td class="blanktotal" colspan="1" rowspan="6"></td> <td class="totals" colspan="2">Subtotal:</td> <td class="totals" colspan="2">'.number_format((float)$pris1, 2, ',', '.').' kr.</td> </tr> <tr> <td class="totals1" colspan="2">Moms:</td> <td class="totals1" colspan="2">'.number_format((float)$pris1*(0.25 * $moms) - $momsTotal, 2, ',', '.').' kr.</td> </tr> <tr> <td class="totals1" colspan="2"><b>TOTAL:</b></td> <td class="totals1" colspan="2"><b>'.number_format((float)$pris1*(1 + (0.25 * $moms)) - $momsTotal, 2, ',', '.').' kr.</b></td> </tr> </tbody> </table> </code></pre> <p><strong>!!! EVERYTHING that you have seen by now, including the totals are a part of the table!</strong></p> <p>Here is the mpdf settings that I have:</p> <p>In PhP:</p> <pre><code>$mpdf=new mPDF('win-1252','A4','','',20,15,48,25,10,10); $mpdf->useOnlyCoreFonts = true; // false is default $mpdf->SetProtection(array('print')); $mpdf->SetTitle("Suggestive Title"); $mpdf->SetAuthor("Author"); $mpdf->SetWatermarkText("Faktura"); $mpdf->showWatermarkText = true; $mpdf->watermark_font = 'DejaVuSansCondensed'; $mpdf->watermarkTextAlpha = 0.1; $mpdf->SetDisplayMode('fullpage'); </code></pre> <p>In HTML:</p> <pre><code>$html .= '<!--mpdf <htmlpageheader name="myheader"> <table width="100%"><tr> <td width="50%" style="color:#000000;"> <span style="font-weight: bold; font-size: 14pt;"> Company Name </span> Company Information </td> <td width="50%" style="text-align: right;"> <img src="image.jpg" /> Invoice Information <br />Side: {PAGENO} af {nb} </td> </tr></table> </htmlpageheader> <htmlpagefooter name="myfooter"> <div style="border-top: 1px solid #000000; font-size: 9pt; text-align: center; padding-top: 3mm; "> Footer Stuff </div> <div style="font-size: 9pt; text-align: center;"> Other Footer Stuff </div> </htmlpagefooter> <sethtmlpageheader name="myheader" value="on" show-this-page="1" /> <sethtmlpagefooter name="myfooter" value="on" /> mpdf-->'; </code></pre> <p>Some styling that I use</p> <pre><code>body {font-family: sans-serif; font-size: 10pt; } p { margin: 0pt; } td { vertical-align: top; } .items td { border-left: 0.1mm solid #000000; border-right: 0.1mm solid #000000; } table thead td { background-color: #d4ffaa; border: 0.1mm solid #000000; } .items td.blanktotal { background-color: #FFFFFF; border: 0mm none #000000; border-top: 0.1mm solid #000000; border-right: 0.0mm solid #000000; } .items td.totals { text-align: right; border-top: 0.1mm solid #000000; border-right: 0.0mm solid #000000; border-left: 0.0mm solid #000000; } .items td.totals1 { text-align: right; border-top: 0.0mm solid #000000; border-right: 0.0mm solid #000000; border-left: 0.0mm solid #000000; } </code></pre> <p>The result looks something like this:</p> <p><img src="https://i.stack.imgur.com/gjHUM.png" alt="enter image description here"></p> <p>This issue occurs <strong><em>only</em></strong> when the number of rows <strong><em>fills the page</em></strong>. If it overflows it is no major issue, because the table headers are still there on the next page (all of them unfortunately, not only the last one) and the rest of the rows are added further. </p> <p>How can I get rid of those empty rows, <strong><em>in this special case</em></strong>?</p> <p>Thank you!</p> <p><strong>!!EDIT:</strong> <strong><em>Added more information, code and a better image of the case. Sorry for not doing it at the first time!</em></strong></p> <p><strong>!!EDIT #2:</strong> <strong><em>I just observed on other examples that had more than 1 page of rows that the <code><thead></code>s keep their position on next pages. It happens all the time. Is there a way to disable that so that other <code><thead></code>s will just continue on the next page from the top of the page without remembering the position from the previous page?</em></strong></p> </div>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页