TCPDF适合页面(缩小)

时间:2017-10-13 08:30:32

标签: php tcpdf

我正在为我的项目使用TCPDF(第一次)。 我在过去2天内完成了PDF创建和保存工作。

我现在有一个问题。数据是动态生成的,高度会有所不同。有没有办法适应页面"像MS office中用于缩放的选项

这是PHP代码

function generateProformaContent($user,$quote,$datas)
{
    $datas=json_decode($datas);
    foreach($datas as $data)
    {
        foreach ($quote->products as $product)
        {
            if($data->productName==$product->name)
            {
                $product->totalPacks=$data->packs;
            }
        }
    }
    unset ($datas);
    $content=
<<<EDO
    <table border="1" cellpadding="10" cellspacing="0">
        <thead>
            <tr >
                <td align="center"><b>Proforma Invoice</b></td>
            </tr>
        </thead>
    </table>
    <table border="1" cellpadding="10" cellspacing="0">
        <thead>
            <tr style="background-color:#004269;color:#FFFFFF;">
                <td align="center">
                    <b>Consignor Address</b>
                </td>
                <td align="center">
                    <b>Consignor Details</b>
                </td>
                <td align="center">
                    <b>Proforma Details</b>
                </td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td vertical-align="top">
                    <div>Company Name</div>
                    <div>Address</div>
                    <div>Phone Number</div>
                    <div>Website</div>
                </td>
                <td vertical-align="top">
                    <div>GST :</div>
                    <div>CST :</div>
                    <div>ECC :</div>
                </td>
                <td vertical-align="top">
                    <div>Date</div>
                    <div>PI No.: #NO.</div>
                </td>
            </tr>
        </tbody>
    </table>
    <table border="1" cellpadding="10" cellspacing="0">
        <thead>
            <tr style="background-color:#004269;color:#FFFFFF;">
                <td align="center">
                    <b>Consignee Address</b>
                </td>
                <td align="center">
                    <b>Consignee Details</b>
                </td>
                <td align="center">
                    <b>Shipment Details</b>
                </td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td vertical-align="top">
                    <div>Company Name</div>
                    <div>Address</div>
                </td>
                <td vertical-align="top">
                    <div>GST :</div>
                    <div>CST :</div>
                    <div>ECC :</div>
                </td>
                <td vertical-align="top">
                    <div>No Of Packs :</div>
                    <div>Gross Weight:</div>
                    <div>Freight Type:</div>
                    <div>Est. Date Of Shipping:</div>
                </td>
            </tr>
        </tbody>
    </table>
    <table border="1" cellpadding="10" cellspacing="0">
        <thead>
            <tr style="background-color:#004269;color:#FFFFFF;">
                <td align="center"><b>Product Summary</b></td>
            </tr>
        </thead>
    </table>
    <table border="1" cellpadding="5" cellspacing="0">
        <thead>
            <tr>
            <td width="40">No.</td>
            <td width="100">Name</td>
            <td width="50" align="right">Rate</td>
            <td width="50" align="center">Unit</td>
            <td width="68" align="right">Quantity</td>
            <td width="100" align="right">Subtotal</td>
            <td width="100" align="center">Tax Rate</td>
            <td width="100" align="right">Tax Amt.</td>
            <td width="100" align="right">Total</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td width="40">No.</td>
                <td width="100">Name</td>
                <td width="50" align="right">Rate</td>
                <td width="50" align="center">Unit</td>
                <td width="68" align="right">Quantity</td>
                <td width="100" align="right">Subtotal</td>
                <td width="100" align="center">Tax Rate</td>
                <td width="100" align="right">Tax Amt.</td>
                <td width="100" align="right">Total</td>
            </tr>
        </tbody>
    </table>
    <table border="1" cellpadding="10" cellspacing="0">
        <tbody>
            <tr>
                <td width="408" vertical-align="top">
                    <div>Payment Terms : </div>
                    <div>Freight : </div>
                    <div>Our Bank Details</div><br>
                    <table border="1" cellpadding="4" cellspacing="0">
                        <tr>
                            <td width="100">Bank</td>
                            <td width="200"></td>
                        </tr>
                        <tr>
                            <td width="100">Account Holder</td>
                            <td width="200"></td>
                        </tr>
                        <tr>
                            <td width="100">Account No.</td>
                            <td width="200"></td>
                        </tr>
                        <tr>
                            <td width="100">IFSC Code</td>
                            <td width="200"></td>
                        </tr>
                        <tr>
                            <td width="100">Branch Code</td>
                            <td width="200"></td>
                        </tr>
                    </table>
                </td>
                <td width="300" vertical-align="top">
                    <br>
                    <table border="1" cellpadding="10" cellspacing="0">
                        <tr>
                            <td width="125">Total Taxable Amt.</td>
                            <td width="150"></td>
                        </tr>
                        <tr>
                            <td width="125">Total Tax</td>
                            <td width="150"></td>
                        </tr>
                        <tr>
                            <td width="125">Grand Total</td>
                            <td width="150"></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
    <table border="1" cellpadding="10" cellspacing="0">
        <tbody>
            <tr>
                <td width="408">Disclaimer: This is a digitally generated document. Signature is not required.</td>
                <td width="300">Date:</td>
            </tr>
        </tbody>
    </table>
EDO;
    return $content;

}
function createProformaPFD()
{
    require_once('tcpdf/tcpdf.php');
    //print_r($_POST);
    //getting UserName
    $pdo=database::connect();
    $sql=$pdo->prepare("SELECT DatabaseName,InternalID FROM users WHERE ID=:id");
    $sql->bindParam("id",$_POST["user"]);
    $sql->execute();
    $sql=$sql->fetch(PDO::FETCH_OBJ);
    $database=$sql->DatabaseName;
    $userID=$sql->InternalID;
    $pdo=database::disconnect();
    $pdo=database::connect_db($database);
    $quote=new Quote($_POST["quote"],$database);
    $sql=$pdo->prepare("SELECT * FROM users WHERE ID=:id");
    $sql->bindParam("id",$userID);
    $sql->execute();
    $user=$sql->fetchObject();
    $pdo=database::disconnect();
    $content=generateProformaContent($user,$quote,$_POST["products"],$database);
    //echo $content;
    // create new PDF document
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Our Code World');
    $pdf->SetTitle('Example Write Html');

    // remove default header/footer
    $pdf->setPrintHeader(false);
    $pdf->setPrintFooter(false);
    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // set margins
    $pdf->SetMargins(5, 10, 5);

    // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    // set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

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

    // set font
    $pdf->SetFont('times', '', 9);

    // add a page
    $pdf->AddPage();

    $pdf->writeHTML($content, true, false, true, false, '');


    // reset pointer to the last page
    $pdf->lastPage();
    //Check Folder For PI
    if (!file_exists("$database/proforma/"))
        mkdir("$database/proforma/", 0777);
    //Close and output PDF document
    $pdf->Output($_SERVER['DOCUMENT_ROOT']."EasyQuotes/$database/proforma/proforma.pdf", 'F');
}

jQuery代码

$(document).on("click","#generateProforma",function(){
            var products=collectdata(noOfProducts);
            var data={user:$("#userID").val(),quote:$("#quoteID").val(),products:JSON.stringify(products)};
            console.log(data);
            ajax_PDF(data,"createProformaPFD");
    });

此致 Bhavil Jain。

0 个答案:

没有答案