2015-03-03 53 views
0

我正在使用dompdf库并生成PDF文件。在该PDF中,我必须将来自查询的内容放入数据库中。如何使用SQL查询获取内容来生成PDF文件?

+0

嗨Saravanan。就你所知,像这样的简短问题往往会被降低和/或关闭。这是因为它的简洁隐藏了我们需要提供的可能对你有用的答案的细节(尽管如此,无论如何,两个人都勇敢地给出答案)。另外,我们喜欢在这里看到先前的努力和研究,而这两个特征都不被看作是懒惰的(在这种情况下,你可能已经做了几天的研究,但是因为我们看不到它,所以我们假设没有)。 – halfer 2015-03-03 09:05:11

+0

嗨。我运行查询并从数据库获取字段名称和值。但它需要字段名称而不考虑字段值。 mysql_num_fields()这个方法用来获取字段名和mysql_fetch_row()这个方法用来获取字段值。 – 2015-03-03 10:00:41

+0

这将是最有帮助的,在这种情况下,如果你将你的代码的相关位放入你的问题(通过编辑它)。希望你现在可以明白为什么这是至关重要的。请使用提供的格式工具。 – halfer 2015-03-03 10:01:44

回答

0

请试试这个脚本

<?php 
    $mysqli = new mysqli("localhost", "root", "", ""); 
    if ($mysqli->connect_errno) { 
     echo "Failed to Connect MySQL: (" . $mysqli-  >connect_errno . ") " . $mysqli->connect_error; 
    } 

    require_once 'dompdf-master/dompdf_config.inc.php'; 

    $result=$mysqli->query("SELECT * FROM tbl_name"); 


    while ($arr_result = $result->fetch_array()) 
    { 
     $nombre=$arr_result["nombre"]; 



    $html.= " 
     <table> 
      <tr> 
      <td> $nombre </td> 
      </tr></table> 
    "; 
    $mipdf = new DOMPDF(); 

    $mipdf ->set_paper("A4", "portrait"); 
    $mipdf ->load_html(utf8_decode($html)); 
    $mipdf ->render(); 
    $mipdf ->stream('mipdf.pdf'); 
     } 
    ?> 
0

您可以使用FPDF。 FPDF是一个PHP类,允许使用纯PHP生成PDF文件,也​​就是说不使用PDFlib库。 FPDF的F表示免费:您可以将其用于任何形式的使用,并对其进行修改以适应您的需求。

<?php 
require('fpdf.php'); 

$pdf = new FPDF(); 
$pdf->AddPage(); 
$pdf->SetFont('Arial','B',16); 
$pdf->Cell(40,10,'Hello World!'); 
$pdf->Output(); 
?> 

上面的代码会生成一个PDF格式的“Hello World!”。文字,字体,大小16和粗体。 您可以输入您的sql查询结果来代替Hello World。

有关FPDF的更多信息,请转到official site

0

FPDF

FPDF是一个PHP类,允许你创建一个基于PHP的PDF文档。 可以使用include提供数据库连接。此外,可以使用FPDF类的标准化函数给出列标题,pdf的方向。

<?php 
define('FPDF_FONTPATH', 'font/'); 
require('fpdf.php'); 

//Connect to your database 
include("conectmysql.php"); 

//Create new pdf file 
$pdf=new FPDF(); 

//Open file 
$pdf->Open(); 

//Disable automatic page break 
$pdf->SetAutoPageBreak(false); 

//Add first page 
$pdf->AddPage(); 

//set initial y axis position per page 
$y_axis_initial = 25; 

//print column titles for the actual page 
$pdf->SetFillColor(232, 232, 232); 
$pdf->SetFont('Arial', 'B', 12); 
$pdf->SetY($y_axis_initial); 
$pdf->SetX(25); 
$pdf->Cell(30, 6, 'CODE', 1, 0, 'L', 1); 
$pdf->Cell(100, 6, 'NAME', 1, 0, 'L', 1); 
$pdf->Cell(30, 6, 'PRICE', 1, 0, 'R', 1); 

$y_axis = $y_axis + $row_height; 

//Select the Products you want to show in your PDF file 
$result=mysql_query('select Code, Name, Price from Products ORDER BY Code', $link); 

//initialize counter 
$i = 0; 

//Set maximum rows per page 
$max = 25; 

//Set Row Height 
$row_height = 6; 

while($row = mysql_fetch_array($result)) 
{ 
    //If the current row is the last one, create new page and print column title 
    if ($i == $max) 
    { 
     $pdf->AddPage(); 

     //print column titles for the current page 
     $pdf->SetY($y_axis_initial); 
     $pdf->SetX(25); 
     $pdf->Cell(30, 6, 'CODE', 1, 0, 'L', 1); 
     $pdf->Cell(100, 6, 'NAME', 1, 0, 'L', 1); 
     $pdf->Cell(30, 6, 'PRICE', 1, 0, 'R', 1); 

     //Go to next row 
     $y_axis = $y_axis + $row_height; 

     //Set $i variable to 0 (first row) 
     $i = 0; 
    } 

    $code = $row['Code']; 
    $price = $row['Price']; 
    $name = $row['Code']; 

    $pdf->SetY($y_axis); 
    $pdf->SetX(25); 
    $pdf->Cell(30, 6, $code, 1, 0, 'L', 1); 
    $pdf->Cell(100, 6, $name, 1, 0, 'L', 1); 
    $pdf->Cell(30, 6, $price, 1, 0, 'R', 1); 

    //Go to next row 
    $y_axis = $y_axis + $row_height; 
    $i = $i + 1; 
} 

mysql_close($link); 

//Create file 
$pdf->Output(); 
?>