【问题标题】:PHP+html webpage to pdfPHP+html网页转pdf
【发布时间】:2016-07-31 14:09:02
【问题描述】:

我被这件事困住了,不知道该怎么办。我google了很多次,对fpdf、html2pdf、tcpdf有所了解。

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

已经尝试了一些代码,但我只得到了 html 文本作为输出,还想要 php 数据。

这是发票的网页。

代码: 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;
}
?>  

这是转换的输出。

【问题讨论】:

    标签: php html pdf-generation pdf-conversion


    【解决方案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();
    
    ?>
    

    【讨论】:

    • 出现错误:请设置动作属性路径。这个错误与什么有关?
    • php 不工作有没有其他东西可以写而不是写 php 的 writehtml
    • 你好,我试过 tcpdf。但是有一个小问题,只从数据库中获取并显示单个值。并没有显示任何其他内容。
    • 嘿,还有一个问题。我想在屏幕上显示 pdf,同时想保存它。我用“I”它只显示在屏幕上..用“F”它只保存文件..但我想做这两件事。你能提出一些建议吗。
    • 您可以使用“FI”。参考文档tcpdf.org/docs/source_docs/classTCPDF/…
    猜你喜欢
    • 2012-11-21
    • 2020-06-29
    • 1970-01-01
    • 2018-04-07
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 2013-08-09
    相关资源
    最近更新 更多