我使用PHP和PDFcrowd.com将html字符串转换为pdf。下面是我打电话的控制器。这将加载invoice.download.php
$date = "january";
mysqli_query($connection->dbconn, "UPDATE monthly_visitors SET '$date' = 0");
这是我的invoice_download.php页面
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);
}
我知道我不能在html字符串里面使用php。我如何在生成的pdf中显示这些变量?用户永远不会看到invoice_download页面。他们点击“下载发票”的页面将保留当前页面,发票将下载。
我还在html字符串中有一个表,我需要循环显示所有行项目。我需要能够重新创建它,因为这也是PHP代码。任何帮助深表感谢。
答案 0 :(得分:0)
我们可以在控制器本身内创建这些html,而不是在你提到用户不会看到该页面时创建一个新视图。根据我的看法,你在这里提到的视图页面上几乎没有错误。以下是更正。我仍然可能有错误,但你可能知道你犯了什么错误。您可以使用像#39;。'这样的字符串运算符。或者'。='在哪里我们需要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;
?>
所以上面会做的是如果编码为真,输出代码就像
<!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="'.@$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>
'.@$customer[0]->address.' <br>
'.@$customer[0]->city.', '.@$customer[0]->state.' '.@$customer[0]->zipCode.' <br>
<abbr title="Phone">P:</abbr> '.@$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>'.@$quotes[0]->invoiceDate.'</span> <br>
<span><strong>Invoice Due Date: </strong>'.@$quotes[0]->invoiceDate.'</span> <br>
<span><strong>PO Number: </strong>'.@$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>'.@$quotes[0]->shipTo.'</strong></div>
<td>'.@$quotes[0]->shipAddress.'</td>
<td>'.@$quotes[0]->shipCity.', '.@$quotes[0]->shipState.' '.@$quotes[0]->shipZip.'</td>
<td>'.@$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>'.@$quotes[0]->invoiceLineItems[0].'</strong></div>
<td>'.@$qty.'</td>
<td>$'.@$unitPrice .'</td>
<td>$'.@$tax .'</td>
<td>$'.@$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>$'.@$totalPrice .'</td>
</tr>
<tr>
<td><strong>TAX :</strong></td>
<td>$'.@$taxTotal .'</td>
</tr>
<tr>
<td><strong>SHIPPING :</strong></td>
<td>$'.@$shippingTotal .'</td>
</tr>
<tr>
<td><strong>TOTAL :</strong></td>
<td>$'.@$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>'.@$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;
}
?>