如何使用SQL查询获取内容生成PDF文件?

I am using dompdf library and generate the PDF file. In that PDF I have to put the content from query in database.

Please try this script

<?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');
       }
    ?>

You can use FPDF. FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say without using the PDFlib library. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.

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

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

Above code will generate a pdf with "Hello World!" text in it, with font arial, size 16 and bold style. You can enter your sql query result in place of Hello World.

For more information on FPDF, go to their official site.

fpdf

FPDF is a PHP class that allows you to create PDF documents based on PHP. The database connectivity can be provided by using include. Also, the Column headers, orientation of the pdf can be given using standardised functions of FPDF Class.

<?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();
?>