Search code examples
phpmysqlsqlpdffpdf

PHP script to output individual PDF for each SQL row - FPDF


I am in learnig process of php hoping to find some directions here on stack.

I am tring to write a simpe script that can output an individual pdf for all the rows of a specific table

Here is my script i get the $html variable printed on pdF but only 1 file and 1 row. Then the other problem is when i try to output the file instead of displaying it on browser I get this error.

Fatal error: Uncaught exception 'Exception' with message 'FPDF error:           Incorrect output destination: ./output/pdf_ganesh.pdf' in   C:\xampp\htdocs\crud\Classes\fpdf.php:271 Stack trace: #0 C:\xampp\htdocs\crud\Classes\fpdf.php(1028): FPDF->Error('Incorrect outpu...') #1 C:\xampp\htdocs\crud\makepdf.php(38): FPDF->Output('./output/pdf_ga...') #2 {main} thrown in C:\xampp\htdocs\crud\Classes\fpdf.php on line 271

I would like the output to be in the project in a specific folder.

Thanks ahead.

<?php

$databaseHost = 'localhost';
$databaseName = 'test';
$databaseUsername = 'root';
$databasePassword = '';
$db = mysqli_connect($databaseHost, $databaseUsername, $databasePassword,               $databaseName);

require('./Classes/fpdf.php');

$users = mysqli_query($db, "SELECT * FROM users ORDER BY id DESC"); 



foreach ($users as $user) {

 $pdf = new FPDF('P','mm','A4');

 $pdf->AddPage();


$on_id=$user['id'];
$on_name=$user['name'];
$on_age=$user['age'];
$on_email=$user['email'];
$on_safe_name=strtolower(str_replace(' ', '_', $on_name));
$on_filename="pdf_$on_safe_name.pdf";

$html = $on_name.'</br>'.$on_age.'</br>'.$on_email;
 }





 $pdf->SetFont('Arial','B',16);
 $pdf->Write(5, $html);
 $pdf->Output('./output/'.$on_filename);
 ?>    

Solution

  • The error seems to be pointing to this line

    $pdf->Output('./output/'.$on_filename);