2017-02-22 5 views
0

私は現在、MySqlから情報を引き出し、その情報をPDFに変換してPHPmailerを使用して電子メールを送信する請求書スクリプトに取り組んでいます。FPDF&Whileループ

私が抱えている部分は、これをPDFに変換する方法です。オーダーIDを次のページに投稿して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> 

私はPDFにページ全体を変換することが、私は、これはFPDFでは不可能であることを理解するものから反対しないwouuld。

よろしくお願いいたします。&ありがとうございました!

+0

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

答えて

0

スタイリングとHTML要素に関して複雑すぎない限り、私はhttps://github.com/dompdf/dompdfを見てみることをお勧めします。これはかなり強力なHTML to 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ファイルを電子メールで保存できるディレクトリにpdfを保存することができます。 上記は$ _POST変数を通して渡されたヘッダーとリストのユーザー情報を作成します。

FPDFのドキュメントは、時々役に立ちます。 http://www.fpdf.org/