2016-08-06 98 views
1

我使用PHP和PDFcrowd.com將html字符串轉換爲pdf。下面是我打電話的控制器。這將加載invoice.download.php將變量從html傳遞到pdf中

function pdf() { 

    $currentUser = $this->session->userdata("login_id"); 
    $query = ParseUser::query(); 
    $query->equalTo("objectId", $currentUser); 
    $this->data['company_data'] = $query->find(); 

    try { 
     $objectUser = $query->get($currentUser); 
     $file = $objectUser->get("logo"); 
    } catch (ParseException $ex) { 

    } 
    if ($file != NULL) { 
     $imageURL = $file->getURL(); 
    } else { 
     $imageURL = "index.php/assets/images/fourcards.jpg"; 
    } 

    $objectId = $this->input->get("object_id"); 
    $query = new ParseQuery('Quotes'); 
    $query->equalTo("objectId", $objectId); 
    $this->data['quotes'] = $query->find(); 

    try { 
     $object = $query->get($objectId); 
     $customerId = $object->get("customerId"); 
     $taxRate = $object->get("tax"); 
     $shipping = $object->get("shipping"); 
     $priceString = $object->get("price"); 
     $priceNumber = preg_replace("/[\$,]/", '', $priceString); 
     $qtyArray = $object->get("qty"); 

    } catch (ParseExeption $ex) { 

    } 

    $unitPrice = floatval($priceNumber); 
    $qty = $qtyArray[0]; 
    $unitPriceTotal = $unitPrice * $qty; 

    if($taxRate == 1){ 
     $tax = 0; 
     $this->data["tax"] = $tax; 
    } else { 
     $taxDecimal = $taxRate/100; 
     $tax = $taxDecimal * $unitPriceTotal; 
    } 

    $shippingTotal = $shipping; 
    $subTotal = $unitPriceTotal; 
    $totalPrice = $unitPriceTotal; 
    $taxTotal = $tax; 
    $grandTotal = $totalPrice + $taxTotal + $shippingTotal; 

    $this->data["shippingTotal"] = number_format($shippingTotal, 2, '.', ','); 
    $this->data["totalPrice"] = number_format($totalPrice, 2, '.', ','); 
    $this->data["taxTotal"] = number_format($taxTotal, 2, '.', ','); 
    $this->data["grandTotal"] = number_format($grandTotal, 2, '.', ','); 
    $this->data["qty"] = $qty; 
    $this->data["unitPrice"] = number_format($unitPrice, 2, '.', ','); 
    $this->data["subTotal"] = number_format($subTotal, 2, '.', ','); 
    $this->data["tax"] = number_format($tax, 2, '.', ','); 
    $this->data["logo"] = $imageURL; 


    $query = new ParseQuery("Customers"); 
    $query->equalTo("userId", $this->session->userdata('login_id')); 
    $query->limit(900000); 
    $this->data['customer'] = $query->find(); 

    $this->load->library('Pdf'); 
    $this->load->view('completed_orders/invoice_download', $this->data); 
} 

這是我invoice_download.php頁

<?php 
    $orderNumber = @$quotes[0]->orderNumber; 
    $customerId = @$quotes[0]->customerId; 
    $html = ' 
<!DOCTYPE html> 
<html> 

<head> 

    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 

    <title>TheWrapApp | Invoice</title> 

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 



</head> 

<body class="white-bg"> 
         <div class="col-xs-6 pull-left"> 
           <h1> 


<img src="<?php echo @$logo; ? >"style="height:100px; ALT="Company Logo"> 
          </a> 
          </h1> 
         </div> 
         <div class="col-xs-6 text-right"> 
          <br> 
          <h1>INVOICE</h1> 
          <h1><small>Invoice # $orderNumber</small></h1> 
           <br> 
          </div> 

          <div class="row"> 
      <div class="col-lg-12"> 

          <div class="row"> 
           <div class="col-xs-5"> 
           <div class="panel panel-default"> 
            <div class="panel-heading"> 
            <h4>Bill To: <?php echo $customerId; ?></h4> 
            </div> 
            <div class="panel-body"> 
            <p> 
             <?php echo @$customer[0]->address; ?> <br> 
             <?php echo @$customer[0]->city; ?>, <?php echo @$customer[0]->state; ?> <?php echo @$customer[0]->zipCode; ?> <br> 
             <abbr title="Phone">P:</abbr> <?php echo @$customer[0]->phoneNumber; ?> <br> 
            </p> 
            </div> 
           </div> 
         </div> 
         <div class="col-xs-5 col-xs-offset-2 text-right"> 
          <div class="panel panel-default"> 
            <div class="panel-heading"> 
            <h4 <b>Invoice Info</font></b></h4> 
            </div> 
            <div class="panel-body"> 
            <p> 
             <span><strong>Date Ordered: </strong><?php echo @$quotes[0]->invoiceDate; ?></span> <br> 
             <span><strong>Invoice Due Date: </strong><?php echo @$quotes[0]->invoiceDate; ?></span> <br> 
             <span><strong>PO Number: </strong><?php echo @$quotes[0]->poNumber; ?></span> <br> 
            </p> 
            </div> 
           </div> 
         </div> 
          </div> 

          <div class="table-responsive m-t"> 
           <table class="table table-bordered"> 
            <thead> 
            <tr> 
             <th>Ship To</th> 
             <th>Address</th> 
             <th>City/State/Zip</th> 
             <th>Shipping Method</th> 
            </tr> 
            </thead> 
            <tbody> 
            <tr> 
             <td><div><strong><?php echo @$quotes[0]->shipTo; ?></strong></div> 
             <td><?php echo @$quotes[0]->shipAddress; ?></td> 
             <td><?php echo @$quotes[0]->shipCity; ?>, <?php echo @$quotes[0]->shipState; ?> <?php echo @$quotes[0]->shipZip; ?></td> 
             <td><?php echo @$quotes[0]->shipMethod; ?></td> 
            </tr> 


            </tbody> 
           </table> 
           <hr style="border-top: dotted 2px;" /> 

          <div class="table-responsive m-t"> 
           <table class="table invoice-table"> 
            <thead> 
            <tr> 
             <th>Invoice Items</th> 
             <th>Quantity</th> 
             <th>Unit Price</th> 
             <th>Tax</th> 
             <th>Total Price</th> 
            </tr> 
            </thead> 
            <tbody> 
            <tr> 
             <td><div><strong><?php echo @$quotes[0]->invoiceLineItems[0]; ?></strong></div> 
             <td><?php echo @$qty?></td> 
             <td>$<?php echo @$unitPrice ?></td> 
             <td>$<?php echo @$tax ?></td> 
             <td>$<?php echo @$subTotal ?></td> 
            </tr> 

            </tbody> 
           </table> 
          </div> 
<div class="row"> 
    <div class="col-md-8"></div> 
    <div class="col-md-4"> 

           <table class="table"> 
            <tbody> 
            <tr> 
             <td><strong>Sub Total :</strong></td> 
             <td>$<?php echo @$totalPrice ?></td> 
            </tr> 
            <tr> 
             <td><strong>TAX :</strong></td> 
             <td>$<?php echo @$taxTotal ?></td> 
            </tr> 
            <tr> 
             <td><strong>SHIPPING :</strong></td> 
             <td>$<?php echo @$shippingTotal ?></td> 
            </tr> 
            <tr> 
             <td><strong>TOTAL :</strong></td> 
             <td>$<?php echo @$grandTotal ?></td> 
            </tr> 
            </tbody> 
           </table> 
           </div> 
    </div> 
          <div class="well m-t"><strong>Notes</strong> 
           <div class="form-group"> 
         <label class="col-sm-2 col-md-2 control-label"></label><?php echo @$quotes[0]->invoiceNotes; ?> 
          </div> 

      </div> 
     </div> 


    </div> 

</body> 

</html>';?> 
<?php 
include(APPPATH.'libraries/pdfcrowd.php'); 

try 
{ 
    // create an API client instance 
    $client = new Pdfcrowd("xxxxxxxxx", "xxxxxxxxxxxxxxx"); 

    $pdf = $client->convertHtml($html); 

    // set HTTP response headers 
    header("Content-Type: application/pdf"); 
    header("Cache-Control: max-age=0"); 
    header("Accept-Ranges: none"); 
    header("Content-Disposition: attachment; filename=\"google_com.pdf\""); 

    // send the generated PDF 
    echo $pdf; 
} 
catch(PdfcrowdException $why) 
{ 
    echo "Pdfcrowd Error: " . $why; 
} 
?> 

我知道我不能我們PHP中的HTML字符串內。我將如何去讓這些變量顯示在生成的PDF文件中?用戶永遠不會看到invoice_download頁面。他們點擊「下載發票」的頁面將保持當前頁面,發票將下載。

我也有一個表格裏面的html字符串,我需要循環顯示所有的行項目。我需要能夠重新創建這個,因爲這也是PHP代碼。任何幫助深表感謝。

回答

0

我們可以在控制器本身內部創建這些html,而不是創建一個新視圖,因爲您提到用戶將看不到該頁面。而且在你提到的視圖頁面上,我看到的幾乎沒有錯誤。以下是更正。我仍然可能有錯誤,但是你可能會明白你在犯什麼錯誤。您可以使用字符串運算符,如'。'或'。='我們需要e.x.

如果PHP代碼是

<?php 
$html = '<!DOCTYPE html> 
<html> 
<head> 
<title></title> 
</head> 
<body>'; 
if($test === 'yes'){ 
$html .= '<h4>Yesy it works</h4>'; 
} 
else 
{ 
$html .= 'sorry' 
} 
$html .= '</body> 
</html>'; 
echo $html; 
?> 

所以上面會做的是,如果codition是真正的輸出代碼將是什麼樣子

<!DOCTYPE html> 
<html> 
<head> 
<title></title> 
</head> 
<body> 
<h4>Yesy it works</h4> 
</body> 
</html> 

改正的代碼按我的知識會是怎樣

<?php 
$orderNumber = @$quotes[0]->orderNumber; 
$customerId = @$quotes[0]->customerId; 
$html = ' 
<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>TheWrapApp | Invoice</title> 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
</head> 
<body class="white-bg"> 
<div class="col-xs-6 pull-left"> 
<h1> 
<img src="'[email protected]$logo.'"style="height:100px; ALT="Company Logo"> 
</a> 
</h1> 
</div> 
<div class="col-xs-6 text-right"> 
<br> 
<h1>INVOICE</h1> 
<h1><small>Invoice # $orderNumber</small></h1> 
<br> 
</div> 
<div class="row"> 
<div class="col-lg-12"> 
<div class="row"> 
<div class="col-xs-5"> 
<div class="panel panel-default"> 
<div class="panel-heading"> 
<h4>Bill To: '.$customerId.'</h4> 
</div> 
<div class="panel-body"> 
<p> 
'[email protected]$customer[0]->address.' <br> 
'[email protected]$customer[0]->city.', '[email protected]$customer[0]->state.' '[email protected]$customer[0]->zipCode.' <br> 
<abbr title="Phone">P:</abbr> '[email protected]$customer[0]->phoneNumber.' <br> 
</p> 
</div> 
</div> 
</div> 
<div class="col-xs-5 col-xs-offset-2 text-right"> 
<div class="panel panel-default"> 
<div class="panel-heading"> 
<h4 <b>Invoice Info</font></b></h4> 
</div> 
<div class="panel-body"> 
<p> 
<span><strong>Date Ordered: </strong>'[email protected]$quotes[0]->invoiceDate.'</span> <br> 
<span><strong>Invoice Due Date: </strong>'[email protected]$quotes[0]->invoiceDate.'</span> <br> 
<span><strong>PO Number: </strong>'[email protected]$quotes[0]->poNumber.'</span> <br> 
</p> 
</div> 
</div> 
</div> 
</div> 
<div class="table-responsive m-t"> 
<table class="table table-bordered"> 
<thead> 
<tr> 
<th>Ship To</th> 
<th>Address</th> 
<th>City/State/Zip</th> 
<th>Shipping Method</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
<td><div><strong>'[email protected]$quotes[0]->shipTo.'</strong></div> 
<td>'[email protected]$quotes[0]->shipAddress.'</td> 
<td>'[email protected]$quotes[0]->shipCity.', '[email protected]$quotes[0]->shipState.' '[email protected]$quotes[0]->shipZip.'</td> 
<td>'[email protected]$quotes[0]->shipMethod.'</td> 
</tr> 
</tbody> 
</table> 
<hr style="border-top: dotted 2px;" /> 
<div class="table-responsive m-t"> 
<table class="table invoice-table"> 
<thead> 
<tr> 
<th>Invoice Items</th> 
<th>Quantity</th> 
<th>Unit Price</th> 
<th>Tax</th> 
<th>Total Price</th> 
</tr> 
</thead> 
<tbody> 
<tr> 
<td><div><strong>'[email protected]$quotes[0]->invoiceLineItems[0].'</strong></div> 
<td>'[email protected]$qty.'</td> 
<td>$'[email protected]$unitPrice .'</td> 
<td>$'[email protected]$tax .'</td> 
<td>$'[email protected]$subTotal .'</td> 
</tr> 
</tbody> 
</table> 
</div> 
<div class="row"> 
<div class="col-md-8"></div> 
<div class="col-md-4"> 
<table class="table"> 
<tbody> 
<tr> 
<td><strong>Sub Total :</strong></td> 
<td>$'[email protected]$totalPrice .'</td> 
</tr> 
<tr> 
<td><strong>TAX :</strong></td> 
<td>$'[email protected]$taxTotal .'</td> 
</tr> 
<tr> 
<td><strong>SHIPPING :</strong></td> 
<td>$'[email protected]$shippingTotal .'</td> 
</tr> 
<tr> 
<td><strong>TOTAL :</strong></td> 
<td>$'[email protected]$grandTotal .'</td> 
</tr> 
</tbody> 
</table> 
</div> 
</div> 
<div class="well m-t"><strong>Notes</strong> 
<div class="form-group"> 
<label class="col-sm-2 col-md-2 control-label"></label>'[email protected]$quotes[0]->invoiceNotes.' 
</div> 
</div> 
</div> 
</div> 
</body> 
</html>'; 
include(APPPATH.'libraries/pdfcrowd.php'); 
try 
{ 
// create an API client instance 
$client = new Pdfcrowd("xxxxxxxxx", "xxxxxxxxxxxxxxx"); 
$pdf = $client->convertHtml($html); 
// set HTTP response headers 
header("Content-Type: application/pdf"); 
header("Cache-Control: max-age=0"); 
header("Accept-Ranges: none"); 
header("Content-Disposition: attachment; filename=\"google_com.pdf\""); 
// send the generated PDF 
echo $pdf; 
} 
catch(PdfcrowdException $why) 
{ 
echo "Pdfcrowd Error: " . $why; 
} 
?>