Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Reading and following a <strong><a href="http://www.fpdf.org/en/tutorial/tuto5.htm" rel="noreferrer">tutorial</a></strong> on FPDF site might be a good start.</p> <p>Assuming that you have a table (lets call it <code>people</code>) and sample data like this</p> <pre class="lang-sql prettyprint-override"><code>CREATE TABLE People (id int, first_name varchar(5), middle_name varchar(4), last_name varchar(5), age int, email varchar(15)); INSERT INTO People (id, first_name, middle_name, last_name, age, email) VALUES (1, 'Jhon', NULL, 'Doe', 27, 'jhon@email.com'), (2, 'Mark', 'J', 'Lee', 35, 'mark@email.com'), (3, 'Helen', 'P', 'Smith', 30, 'helen@email.com'); </code></pre> <p>Here is a basic php script that do what you want. <strong>Note:</strong> code lacks any error handling for brevity's sake.</p> <pre class="lang-php prettyprint-override"><code>&lt;?php require('fpdf.php'); class People { public function all() { try { $db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'user', 'password'); $query = $db-&gt;prepare("SELECT first_name, middle_name, last_name, age, email FROM people "); $query-&gt;execute(); $people = $query-&gt;fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { //echo "Exeption: " .$e-&gt;getMessage(); $result = false; } $query = null; $db = null; return $people; } } class PeoplePDF extends FPDF { // Create basic table public function CreateTable($header, $data) { // Header $this-&gt;SetFillColor(0); $this-&gt;SetTextColor(255); $this-&gt;SetFont('','B'); foreach ($header as $col) { //Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]]) $this-&gt;Cell($col[1], 10, $col[0], 1, 0, 'L', true); } $this-&gt;Ln(); // Data $this-&gt;SetFillColor(255); $this-&gt;SetTextColor(0); $this-&gt;SetFont(''); foreach ($data as $row) { $i = 0; foreach ($row as $field) { $this-&gt;Cell($header[$i][1], 6, $field, 1, 0, 'L', true); $i++; } $this-&gt;Ln(); } } } // Column headings $header = array( array('First Name', 30), array('Middle Name', 30), array('Last Name', 30), array('Age', 12), array('Email', 47) ); // Get data $people = new People(); $data = $people-&gt;all(); $pdf = new PeoplePDF(); $pdf-&gt;SetFont('Arial', '', 12); $pdf-&gt;AddPage(); $pdf-&gt;CreateTable($header,$data); $pdf-&gt;Output(); </code></pre> <p><strong>Make sure to change connection string</strong></p> <pre><code>$db = new PDO('mysql:host=localhost;dbname=test;charset=UTF-8', 'user', 'password'); ^^^^^^^^^ ^^^^ ^^^^ ^^^^^^^^ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload