How to create PDF with PHP?

There are many web applications that require output such as invoices, contracts or its web pages in PDF format. PHP has PDFlib library to create PDF dynamically but it’s very vast and need a lot of time to study and understand. So there are another PHP class FPDF that allows you to generate PDF files easily without using the PDFlib library.

The FPDF library is free and can be downloaded from the official website’s download section. The download package contains all necessary files, along with some tutorials on how to use it. Below is PHP example code to create PDF from MySQL table data using FPDF library. You must download and extract the FPDF package in the folder where the PHP file with the code is located to run this example.

PHP Script to create PDF from MySQL with PHP


<?php
include_once("../db_connect.php");
$sql = "SELECT id, employee_name, employee_salary, employee_age FROM employee LIMIT 10";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
while ($field_info = mysqli_fetch_field($resultset)) {
$pdf->Cell(47,12,$field_info->name,1);
}
while($rows = mysqli_fetch_assoc($resultset)) {
$pdf->SetFont('Arial','',12);
$pdf->Ln();
foreach($rows as $column) {
$pdf->Cell(47,12,$column,1);
}
}
$pdf->Output();
?>


Hope you have you have downloaded and extracted the FPDF package inside a folder on your server. Now create a new file called createpdf.php inside the same folder on your server and insert the above code. You can then access your example file createpdf.php through browser.

MySQL Table and Data


You just need to run this script for the MySQL data.
--
-- Table structure for table `employee`
--
CREATE TABLE IF NOT EXISTS `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`employee_name` varchar(255) NOT NULL COMMENT 'employee name',
`employee_salary` double NOT NULL COMMENT 'employee salary',
`employee_age` int(11) NOT NULL COMMENT 'employee age',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='datatable demo table' AUTO_INCREMENT=58 ;
--
-- Dumping data for table `employee`
--
INSERT INTO `employee` (`id`, `employee_name`, `employee_salary`, `employee_age`) VALUES
(1, 'Tiger Nixon', 320800, 61),
(2, 'Garrett Winters', 170750, 63),
(3, 'Ashton Cox', 86000, 66),
(4, 'Cedric Kelly', 433060, 22),
(5, 'Airi Satou', 162700, 33),
(6, 'Brielle Williamson', 372000, 61),
(7, 'Herrod Chandler', 137500, 59),
(8, 'Rhona Davidson', 327900, 55),
(9, 'Colleen Hurst', 205500, 39),
(10, 'Sonya Frost', 103600, 23);


Demo [sociallocker]Download[/sociallocker]

Komentar

  1. where is full code

    BalasHapus
  2. Just download demo zip file for all code. Thanks!

    BalasHapus
  3. can i have the html code of this demo?

    BalasHapus
  4. yes, you can download demo source code to get all code.

    BalasHapus
  5. where the html kod?only have php

    BalasHapus
  6. it working find but i have uncode data in my datbase
    i am not geting thata data

    BalasHapus
  7. You need to download complete source code of demo. Thanks.

    BalasHapus
  8. I think you have to handle with encode/decode. Can you send your code and data to help you?

    BalasHapus
  9. George Mutale Mulenga29 Mei 2018 pukul 15.06

    Hey, Thanks for the tutorials you share. I have learnt a lot. Is it possible to create a pdf document from data fetched from JOINS-- Data selected from multiple tables? you display it then allow the user to create a pdf?

    BalasHapus
  10. please send full source code on my mail

    BalasHapus
  11. You can download source code from download link.

    BalasHapus
  12. Yes it is possible. You can try it. Thanks!

    BalasHapus

Posting Komentar

Postingan Populer