how to export mysql database table contents on to a PDF file. it has to be displayed in the website as "download/print data". when the customer click on that he/she has to get the PDF opened or an option to download the PDF with all the contents of the table.

help from the experts are highly appreciated

Dani AI

Generated

, a quick, modern workflow looks like this: run a query with PDO/MySQLi (the old mysql* API was removed in PHP 7.0), build either table cells or HTML, then stream the PDF with the disposition you want. To control whether the browser opens the PDF or downloads it, send a Content-Disposition header of either inline (open) or attachment (download). That keeps your "download/print data" link simple and predictable. See the PHP docs on the mysql* removal and the HTTP spec for Content-Disposition for details. PHP manual, RFC 6266.

As and @soldierflup noted, FPDF/TCPDF work well when you draw cells yourself. If you already have an HTML table, HTML-to-PDF engines save time: Dompdf and mPDF both render CSS reasonably and handle Unicode fonts. Install with Composer:

composer require dompdf/dompdf
composer require mpdf/mpdf
composer require tecnickcom/tcpdf

Minimal example using PDO + Dompdf. It streams inline by default; add ?download=1 to force download.

<?php
require __DIR__ . '/vendor/autoload.php';
use Dompdf\Dompdf;

$pdo = new PDO('mysql:host=localhost;dbname=app;charset=utf8mb4','user','pass', [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

$rows = $pdo->query('SELECT id,name,price FROM products ORDER BY id')->fetchAll();
$html = '<h2>Products</h2><table border="1" cellpadding="4" cellspacing="0"><tr><th>ID</th><th>Name</th><th>Price</th></tr>';
foreach ($rows as $r) {
  $html .= '<tr><td>'.(int)$r['id'].'</td><td>'.htmlspecialchars($r['name']).'</td><td style="text-align:right">'.number_format((float)$r['price'],2).'</td></tr>';
}
$html .= '</table>';

$pdf = new Dompdf();
$pdf->loadHtml($html);
$pdf->render();
$pdf->stream('table.pdf', ['Attachment' => isset($_GET['download'])]);

For ’s JSP/Java angle, today you would typically use iText 7 or a reporting engine like JasperReports. Start here: iText 7 and JasperReports library (GitHub).

Recommended Answers

All 10 Replies

You can use FPDF library
Download its zip file from net. I don't remember the link. If you dont find it. tell me i wll send you its zip folder. following is the sample code to generate pdf for php code. Here code is reading data from file, you can execute a query instead for your case.

require('fpdf.php');

class PDF extends FPDF
{
//Load data
function LoadData($file)
{
    //Read file lines
    $lines=file($file);
    $data=array();
    foreach($lines as $line)
        $data[]=explode(';',chop($line));
    return $data;
}

//Simple table
function BasicTable($header,$data)
{
    //Header
    foreach($header as $col)
        $this->Cell(40,7,$col,1);
    $this->Ln();
    //Data
    foreach($data as $row)
    {
        foreach($row as $col)
            $this->Cell(40,6,$col,1);
        $this->Ln();
    }
}

//Better table
function ImprovedTable($header,$data)
{
    //Column widths
    $w=array(40,35,40,45);
    //Header
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C');
    $this->Ln();
    //Data
    foreach($data as $row)
    {
        $this->Cell($w[0],6,$row[0],'LR');
        $this->Cell($w[1],6,$row[1],'LR');
        $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R');
        $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R');
        $this->Ln();
    }
    //Closure line
    $this->Cell(array_sum($w),0,'','T');
}

//Colored table
function FancyTable($header,$data)
{
    //Colors, line width and bold font
    $this->SetFillColor(255,0,0);
    $this->SetTextColor(255);
    $this->SetDrawColor(128,0,0);
    $this->SetLineWidth(.3);
    $this->SetFont('','B');
    //Header
    $w=array(40,35,40,45);
    for($i=0;$i<count($header);$i++)
        $this->Cell($w[$i],7,$header[$i],1,0,'C',1);
    $this->Ln();
    //Color and font restoration
    $this->SetFillColor(224,235,255);
    $this->SetTextColor(0);
    $this->SetFont('');
    //Data
    $fill=0;
    foreach($data as $row)
    {
        $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
        $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
        $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
        $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
        $this->Ln();
        $fill=!$fill;
    }
    $this->Cell(array_sum($w),0,'','T');
}
}

$pdf=new PDF();
//Column titles
$header=array('Country','Capital','Area (sq km)','Pop. (thousands)');
//Data loading
$data=$pdf->LoadData('countries.txt');
$pdf->SetFont('Arial','',14);
$pdf->AddPage();
$pdf->BasicTable($header,$data);
$pdf->AddPage();
$pdf->ImprovedTable($header,$data);
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();
?>

Hi,
How to get fpdf zip file for downloading.

Thank you.
Anand.k

Hi,
Can you send me the fpdf folder to .

Thank you.

Let me know whether you are able to download fpdf.zip attached with this reply.

Member Avatar for Member #445218

Here's another librabry to create pdf-files in php : TCPDF

Download here.

I used FPDF before and for me personally the library TCPDF is easier in use.

Hope you give it a try.

the above program is about php.....how about change jsp to pdf ?

iText free Java library for converting number of document types, or you can go for JasperReports (however I think this is just enhanced version of iText)

Member Avatar for Member #445218

the above program is about php.....how about change jsp to pdf ?

Googling might help. I found a free pdf library called IText.

You can find it here

Hopes this helps.

Grtz,

Googling might help. I found a free pdf library called IText.

You can find it here

Hopes this helps.

Grtz,

Congratz on discovering and reposting what was already recommended...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.