2017-02-22 36 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/