2016-07-31 3 views
1

Я застрял в этой вещи и не знаю, что делать. Я много раз искал Google и узнал о fpdf, html2pdf, tcpdf.PHP + html webpage to pdf

Я создаю счет-фактуру, в которой я использовал 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

Вы можете использовать tcpdf для рендеринга pdf из html. он обеспечивает большую гибкость 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="">[email protected]</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(); 

?> 
+0

будет ли он также отображаться php-код? –

+0

да, это также даст php. –

+0

получение ошибки: укажите путь к атрибуту действия. С чем связана эта ошибка? –