PHP + html网页到pdf

时间:2016-07-31 14:09:03

标签: php html pdf-generation pdf-conversion

我坚持这件事并且不知道该怎么做。我已经搜索了很多次,并且了解了fpdf,html2pdf,tcpdf。

我正在创建一个发票,其中我使用了html和php代码。我想以pdf显示想要将整个页面转换为pdf并打印并保存它的数据。但我不知道该怎么做我是新手,对此并不了解。

尝试了一些代码,但我只是获取html文本作为输出也想要php数据。

这是发票的网页。 invoice php webpage

代码: 的index.php

<?php
if(!isset($_POST['submit']))
{
?>
<form method="post" action=''>
    <div id="page-wrap">

        <textarea id="header" name="text" disabled="disabled">INVOICE</textarea>

        <div id="identity">
          <textarea name="address" id="address" disabled="disabled" style="background-color:#FFF;color:#000">Address</textarea>
          <div id="logo">
          <img id="image" src="images/logo.png" alt="logo" /></div>
        </div>

        <div style="clear:both"></div>

        <div id="customer">

           <!-- <textarea id="customer-title" disabled="disabled" style="background-color:#FFF;color:#000">Widget Corp.
c/o Steve Widget</textarea>
-->
            <table id="meta">
                <tr>
                    <td class="meta-head">Invoice #</td>
                    <td><textarea>000123</textarea></td>
                </tr>
                <tr>

                    <td class="meta-head">Date</td>
                    <td><textarea id="date"></textarea></td>
                </tr>
            </table>

        </div>

        <table id="items">

          <tr>
              <th>Item</th>
              <th>Unit Cost</th>
              <th>Quantity</th>
              <th>Price</th>
          </tr>
          <?php
          include("./config.php");
          $q=0;$u=0;$t=0;$tt=0;$s=0;$v=0;
          $sql="SELECT m.order_id,m.table_id,l.item_id,l.order_quantity,i.item_name,i.item_price from order_m m,order_list l,item_list i where m.order_id=l.order_id and l.item_id=i.item_id and table_id=2";
          $result=mysqli_query($con,$sql);
          while($row=mysqli_fetch_array($result))
          {
              $q=$q+$row[3];
              $u=$u+$row[5];
              $t=$t+($row[3]*$row[5]);
          ?>
          <tr class="item-row">
              <td class="item-name"><?php echo $row[4]?></td>
              <td><?php echo $row[5];?></td>
              <td><?php echo $row[3];?></td>
              <td><?php echo $row[3]*$row[5];}?></td>
          </tr>


          <tr>
               <td>Total </td>
              <td><?php echo $u; ?></td>
              <td><?php echo $q;?></td>
              <td><?php echo $t;?></td>
          </tr>
          <?php 
          $sql1="Select * from tax";
          $res=mysqli_query($con,$sql1);
          $row1=mysqli_fetch_row($res);

          if($row1[0]!=0)
          {
              $v=($row1[0]*$t)/100;
          ?>
          <tr>
               <td> </td>
              <td></td>
              <td>VAT%</td>
              <td><?php echo $row1[0]."%";?></td>
          </tr>
          <?php
          }
          else
          {
          }
           if($row1[1]!=0)
          {
              $s=($row1[1]*$t)/100;
          ?>
          <tr>
               <td> </td>
              <td></td>
              <td>Service Tax.%</td>
              <td><?php echo $row1[1]."%";?></td>
          </tr>
          <?php
          }
          else
          {
          }

          ?>
              <tr>
               <td></td>
              <td></td>
              <td>Total</td>
              <td><?php $tt=$t+$v+$s; echo round($tt);?></td>
          </tr>
        </table>

        <div id="terms">
          <h5>Terms</h5>
          <textarea>NET 30 Days. Finance Charge of 1.5% will be made on unpaid balances after 30 days.</textarea>
        </div>

    </div>
    <input type="submit" name="submit" value="Print" />
    </form>
<?php
}
else
{
    ob_start();
    require("html2pdf.php");
    $the_file = "./index.php";
    $myfile = fopen($the_file, "r") or die("Unable to open file!!!!<br><br><br>");
    $homepage = file_get_contents($the_file);
    fclose($myfile);
    $pdf = new PDF_HTML();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',9);
    $pdf->WriteHTML($homepage);
    $pdf->Output();
    ob_end_flush();
    exit;
}
?>  

以下是转换的输出。 output of conversion

1 个答案:

答案 0 :(得分:1)

你可以使用tcpdf从html渲染pdf。它提供了更大的灵活性 https://tcpdf.org/examples/

看到这段代码,这将生成虚拟发票。

   <?php 

function generateInvoice(){
$invoice_no = 0001;

require_once('tcpdf/tcpdf.php');
class MYPDF extends TCPDF 
{
    public function Header() {
    }


    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 12);
    }

}

// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(0, 0, 0);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(True, 0);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

//set custom font
//$fontname = TCPDF_FONTS::addTTFfont('OpenSans-Semibold.ttf', 'TrueTypeUnicode', '',12);
// use the font
$pdf->SetFont($fontname, '', 12, '', false);

// add a page
$pdf->AddPage();

$pdf->setFontSize(40);

$pdf->Write(0, 'Company', '', 0, 'C', true, 0, false, false, 0);


$pdf->setFontSize(12);

$y = $pdf->getY();
$pdf->writeHTMLCell(100, 90, '18', $y+10, '<table cellspacing="0" style="background:#000;" cellpadding="1">
        <tr>
            <td align="left" columnspan="1" style="font-size:15px" width="80" > Name</td> 
            <td align="left" style="font-size:15px" width="">name surname</td> 
        </tr>
       <tr>
            <td align="left" columnspan="1" style="font-size:15px" width="80" > Address</td> 
            <td align="left" style="font-size:15px" width="">India</td> 
        </tr>
<tr>
            <td align="left" columnspan="1" style="font-size:15px" width="80" > Mob.</td> 
            <td align="left" style="font-size:15px" width="">0000000</td> 
        </tr>
<tr>
            <td align="left" columnspan="1" style="font-size:15px" width="80" > Email</td> 
            <td align="left" style="font-size:15px" width="">johnday@johnday.johnday</td> 
        </tr>
      </table>', 0, 0, 0, false, 'J', true);

$y = $pdf->getY();
$pdf->writeHTMLCell(100, 30, 114, $y, '<table cellspacing="0" style="background:#000;" cellpadding="1">
        <tr>
            <td align="left" columnspan="1" style="font-size:15px" width="100" > Invoice # :</td> 
            <td align="left" style="font-size:15px" width="">1000</td> 
        </tr>
       <tr>
            <td align="left" columnspan="1" style="font-size:15px" width="100" > Invoice Date</td> 
            <td align="left" style="font-size:15px" width="">22/02/2016</td> 
        </tr>
      </table>', 0, 0, 0, false, 'J', true);

$y = $pdf->getY();

//$pdf->writeHTMLCell('',10,'',$y+5,$html, 0, 1, 0, true, 'C', true);
$pdf->writeHTML($html, true, false, false, false, '');

$y = $pdf->getY();

$data ='<table cellspacing="0" style="background:#000;"  border="1" cellpadding="2">
<thead>
        <tr  style="background-color:#f2f2f2;">
            <th align="center" columnspan="1" style="font-size:15px" width="140" > item</th>
            <th align="center" columnspan="1" style="font-size:15px" width="140" > price</th> 
            <th align="center" columnspan="1" style="font-size:15px" width="140" > quantity</th> 
            <th align="center" columnspan="1" style="font-size:15px" width="140" > subtotal</th> 
            <th align="center" columnspan="1" style="font-size:15px" width="140" > total</th> 
        </tr>
    </thead>
<tbody>
        <tr style="background-color:#fff;">
            <td align="center" columnspan="1" style="font-size:15px" width="140" >book</td> 
            <td align="center" columnspan="1" style="font-size:15px" width="140" >2</td> 
            <td align="center" columnspan="1" style="font-size:15px" width="140" >2</td> 
            <td align="center" columnspan="1" style="font-size:15px" width="140" >4</td> 
            <td align="center" columnspan="1" style="font-size:15px" width="140" >4</td> 

        </tr>
<tr><td></td>
<td></td>

<td></td>

            <td align="left" columnspan="1" style="font-size:15px" width="140" > Subtotal</td> 
            <td align="left" style="font-size:13px" width="140">$'. number_format((float) (121), 2, '.', '').'</td> 
</tr>
        <tr style="background-color:#f2f2f2;">
<td></td>
<td></td>

<td></td>
            <td align="left" columnspan="1" style="font-size:15px" width="140" > GST</td> 
            <td align="left" style="font-size:13px" width="140">included</td> 
        </tr>
        <tr>
<td></td>
<td></td>

<td></td>
            <td align="left" style="font-size:15px" columnspan="1" width="140"> Discount</td> 
            <td align="left" style="font-size:13px" width="140">$'.number_format((float) (12121.212), 2, '.', '') .'</td> 
        </tr>

        <tr style="background-color:#f2f2f2;">
<td></td>
<td></td>

<td></td>
            <td align="left" style="font-size:15px" columnspan="1" width="140" > Amount Paid</td> 
            <td align="left" style="font-size:13px" width="140">$'.number_format((float) (1212), 2, '.', '').'</td> 
        </tr>
        <tr>
<td></td>
<td></td>

<td></td>
            <td align="left" style="font-size:15px" columnspan="1" width="140" > Amount Due</td> 
            <td align="left" style="font-size:13px" width="140">$'.number_format((float) (1221), 2, '.', '').'</td> 
        </tr>
    </table>
</td>
</tr>

</tbody>
    </table>';

//$pdf->writeHTMLCell('', '','',$y+8,$data, 0, 1, 0, true, 'C', true);

$pdf->writeHTML($data, true, false, false, false, '');

$y = $pdf->getY();
$pdf->writeHTMLCell('', '', '4',$y+4, '<table cellspacing="0" style="background:#000;" cellpadding="2">   
    <tr style="background-color:#21B67B;">
        <td align="center" style="font-size:20px;color:#fff;" columnspan="0" width="390">PAYMENT METHODS WE ACCEPT</td> 
        </tr>
        <tr>
        <td align="left" style="font-size:15px;" width="440">We accept payment by cash, credit card or bank deposit<br></td> 
        </tr>
        <tr>
        <td align="left" style="font-size:15px;" width="400">Our Bank Details: </td> 
        </tr>
        <tr>
        <td align="left" style="font-size:15px;" width="400">Bank name: demo Bank </td> 
        </tr><tr>
        <td align="left" style="font-size:15px;" width="400">BSB Number: 000000 </td> 
        </tr><tr>
        <td align="left" style="font-size:15px;" width="400">Account Number: 00000 </td> 
        </tr>
        <tr>
        <td align="left" style="font-size:15px;" width="400">Account Name: demo demo Ltd </td> 
        </tr>
        <tr>
        <td align="left" style="font-size:15px;" width="400"></td> 
        </tr>
        <tr>
        <td align="left" style="font-size:15px;" width="400">To make a payment via credit card please call our office on (000) 0000 0000. Cash can be paid on delivery.</td> 
        </tr>
        <tr>
        <td align="left" style="font-size:15px;" width="400"></td> 
        </tr>
        <tr>
        <td align="center" style="font-size:24px;color:#21B67B;" width="400">Thank you for your business!</td> 
        </tr>


    </table><br<br>', 0, 0, 0, false, 'J', true);
    $pdf->lastPage();
$pdf->Output($invoice_no.'.pdf', 'I');
return $invoice_no.'.pdf';
}
generateInvoice();

?>