2017-02-22 153 views
0

我目前正在開發一個發票腳本,該腳本目前從MySql中獲取信息,然後將該信息轉換爲PDF並使用PHPmailer通過電子郵件發送。FPDF&While循環

我堅持的部分是如何將其轉換爲PDF。我希望能夠將orderid發佈到隨後的頁面,然後轉換爲PDF。

我附上了我的腳本以獲得一些幫助。

<?php 
session_start(); 
    $username = $_SESSION['username']; 
     $con = mysqli_connect('***', '***', '***', '***'); 
    if (!isset($con)) { 
     die("Connection to Aurora System failed."); 
    } 

    $orderid = $_POST['order_id']; 
    ?> 


<!doctype html> 
<html> 
<body class="body page-orderview clearfix"> 
    <div class="element"></div> 
    <p class="text text-1">SMKD Field Force</p> 
<a href="front.php"><img class="image" src="images/smkd_logo.png"></a> 
    <p class="text text-2">Welcome</p> 
    <p class="text text-3">Order <?php echo "$orderid"; ?> 


    <table> 
     <tr> 
    <th>Product Title</th> 
    <th>Nicotine Strength</th> 
    <th>Quantity</th> 
    <th>Price (inc. VAT)</th> 
    </tr> 
    <?php 

    $query = "SELECT `product`, `variant`, `quantity` FROM orders_detail WHERE order_id = '$orderid'"; 
    $result = mysqli_query($con, $query); 
    $quantitytotal = 0; 
    $quantityline = - 0; 
    while ($row = mysqli_fetch_assoc($result)) { 
     $product = $row['product']; 
     $stuff = $row['quantity']; 
     $variant = $row['variant']; 
     $linequantity = $stuff; 
     $quantitytotal += $stuff; 

     $pricequery = "SELECT product_price FROM products WHERE product_name = '$product'"; 
     $priceresult = mysqli_query($con, $pricequery); 
     $pricetag = 0; 
     $priceline = 0; 
     while ($rowprice = mysqli_fetch_assoc($priceresult)) { 
      $price = $rowprice['product_price']; 
      $pricetag += $price; 
      $priceline = $price; 
     } 
     $linetotal = $priceline * $linequantity; 
     echo '<tr><td>' . $product .' </td> ' . '<td>' . $variant . '</td>' . ' <td> ' . $linequantity . '</td>' . '<td> £' . $linetotal . '</td> </tr>'; 

    } 
    $total = $pricetag * $quantitytotal; 
     ?> 



    <tr><td>Total Ex Vat:</td><td> Total Inc Vat:</td></tr> 

    <tr><td><?php echo "£" . ($total/1.2);?></td> 
    <td><?php echo "£" . $total; ?></td></tr> 
</table> 
</p><br> 
<form method="post" action"pdfinvoice.php"> 
<input type="hidden" value="<?php echo $orderid; ?>" name="orderid"> 
</form> 
Submit button goes here 
</body> 
</html> 

我的理解,這是不可能的FPDF我還是執意不反對整個頁面轉換爲PDF但從。

Regards &非常感謝!

+0

https://github.com/mpdf/mpdf –

回答

0

只要你不會太複雜的造型和html元素,我建議看看https://github.com/dompdf/dompdf,這是一個非常強大的HTML到PDF轉換器庫,並支持相當多的HTML。

只是形式等不會PDF的內工作,所以不包括那些(他們可能會或可能不會被渲染,取決於準確的HTML標記。)

0

我其實只是幾周前完成了相同的任務。但是我的頁面不顯示PDF。完成結帳後,該頁面將顯示成功消息並將PDF發送給用戶。

一旦您下載並將FPDF庫包含在您的php文件中。它只是創建FPDF對象,添加並輸出它的問題。

require "Resources/PDF/fpdf.php"; 

    $pdf = new FPDF(); 
    $pdf->SetAutoPageBreak(true, 0); 
    $pdf->SetLeftMargin(8); 
    $pdf->AddPage(); 
    $pdf->SetFont('Arial','B',20); 
    $pdf->SetTextColor(255); 
    $pdf->SetFillColor(0,153,204); 
    $pdf->Cell(194,10,'MyCompany Inc.','LRT',0,'L', true); 
    $pdf->Ln(); 
    $pdf->SetFont('Arial','I',12); 
    $pdf->Cell(97,10,'Invoice #: '.$id.''.$cnt.'','LB',0,'L', true); 
    $pdf->Cell(97,10, "Date: ".date("d-m-Y")." ",'RB',0,'R', true); 
    $pdf->Ln(); 
    $pdf->SetTextColor(0,153,204); 
    $pdf->SetFont('Arial','B',14); 
    $pdf->Cell(40,10,'Ship to:',0,4); 
    $pdf->SetTextColor(0,0,0); 
    $pdf->SetFont('Arial','I',12); 
    $pdf->Cell(40,5, $_POST['shippingName'],0,5); 
    $pdf->Cell(40,5, $COMP,0,6); 
    $pdf->Cell(40,5, $EMAIL,0,6); 
    $pdf->Cell(40,5, $_POST['shippingPhoneNumber'],0,7); 
    $pdf->Cell(40,5, $_POST['shippingAddress'],0,8); 
    $pdf->Cell(40,5, $_POST['shippingPostalCode'],0,9); 
    $pdf->Cell(40,5, $_POST['shippingCity'],0,10); 
    $pdf->Cell(40,5, $_POST['shippingProvince'],0,11); 
    $pdf->Cell(40,5, $_POST['shippingCountry'],0,12); 
    $pdf->Output(); //should display the pdf to the page 

輸出(「filepath」);可以將pdf保存到您可以通過電子郵件發送文件的目錄。 以上將創建一個標題並列出通過$ _POST變量傳入的用戶信息。

FPDF文檔有時可能會有所幫助。 http://www.fpdf.org/