【问题标题】:Passing variables into pdf from html将变量从html传递到pdf
【发布时间】:2016-08-06 00:28:53
【问题描述】:

我正在使用 PHP 和 PDFcrowd.com 将 html 字符串转换为 pdf。下面是我调用的控制器。这将加载 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);
}

这是我的 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;
}
?>

我知道我不能在 html 字符串中使用 php。我将如何让这些变量显示在生成的 pdf 中?用户永远不会看到 invoice_download 页面。他们点击“下载发票”的页面将保持当前页面并下载发票。

我在 html 字符串中还有一个表格,我需要循环显示所有行项目。我也需要能够重新创建它,因为这也是 php 代码。任何帮助深表感谢。

【问题讨论】:

    标签: php html codeigniter pdf


    【解决方案1】:

    我们可以在控制器本身内创建这些 html,而不是创建一个新视图,因为您提到用户不会看到该页面。而且根据我所见,您在此处提到的查看页面上也几乎没有错误。下面是修正。我仍然可能有错误,但是您可能会知道自己犯了什么错误。您可以使用字符串运算符,例如 '.'或 '.=' 我们需要的地方例如

    如果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;
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 2021-12-29
      • 2019-01-06
      • 2017-06-12
      • 2017-06-30
      • 1970-01-01
      • 2012-07-25
      • 2018-03-23
      • 2018-09-10
      相关资源
      最近更新 更多