exporting database into pdf file in php - php

<?php
mysql_connect('localhost','root','');
mysql_select_db('mydatabase');
ob_start();
header('Content-Type: application/pdf');
include_once('pdf/fpdf.php');
$pdf=new FPDF();
get_class_methods($pdf);
$pdf->AddCol('firstname',30,'firstname');
$pdf->AddCol('lastname',30,'lastname');
$pdf->AddCol('address1',30,'Address');
$pdf-> AddCol('address2',30,'Address');
$pdf->AddCol('postCode',30,'Post Code');
$pdf->AddCol('towns',30,'Town');
$pdf->Table("select firstname,lastname from mytable");
$pdf->Output();
ob_end_flush();
This is my PHP code for exporting a database into a PDF document. It is showing an error as failed to open the PDF document file.

Try below code :-
<?php
require_once('fpdf17/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Write(5,"Hello, World!\n\n\n");
$pdf->Output('pdf.pdf', 'F');
?>

Related

Zip and download multiple PDFs from php

I have a php file that downloads PDFs. I call the php as GeneratePDF.php?id=123 and the php looks like this:
<?php
use setasign\Fpdi\Fpdi;
session_start();
ob_start();
require_once __DIR__ . '/vendor/autoload.php';
require_once('FPDI/autoload.php');
require_once('fpdf/fpdf.php');
require_once('FPDI/Fpdi.php');
$pdf = new \Mpdf\Mpdf();
$pdf->AddPage();
$pdf->setSourceFile('sample.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 0, 0, 297, 420, true);
$pdf->AddPage();
$tplIdx2 = $pdf->importPage(2);
$pdf->useTemplate($tplIdx2, 0, 0, 297, 420, true);
$id=$_GET['id'];
$pdf->Output("$id.pdf", "D");
ob_end_flush();
?>
I am now using another php file to download a set of GeneratePDF.php such as
GeneratePDF.php?id=123
GeneratePDF.php?id=456
GeneratePDF.php?id=789
From this php, I want to zip these 3 files and download it. I know the way to do it would be to use ZipArchive but how do I do I call all 3 GeneratePDF.php and add it to zip?
ZipArchive.php
<?php
session_start();
ob_start();
$zip = new ZipArchive;
$tmp_file = "PDF_Documents_".date('Y_m_d').".zip";
//How do I add set of pdfs from GeneratePDF.php?
} else {
echo 'Failed!';
}
Can someone help me figure this out?

Zip FPDFs files?

I have a php script where I can easily download a pdf file using fpdf library. The script takes in ID of multiples rows from DB and downloads PDFs for each ID. How do I download multiple pdfs as once with zip? That is I send ID value to another script and that script then collects all IDs and downloads it in zip file?
<?php
//include connection file
ob_start();
include_once('fpdf/fpdf.php');
$db = new PDO("mysql:host=localhost; dbname=db;charset=utf8",'root','');
class PDF extends FPDF
{
// Page header
function Header()
{
$this->SetFont('Arial','B',14);
$this->Cell(276, 5, 'Details', 0, 0, 'C');
$this->Ln();
$this->SetFont('Times', '', 12);
//$this->Cell(276, 10, 'Details of students', 0, 0, 'C');
$this->Ln(5);
}
function viewTable($db)
{
$id=$_GET['id'];
$sql = "SELECT * FROM Datas WHERE ID = '$id'";
$stmt = $db->query($sql);
}
}
$id=$_GET['id'];
$pdf = new PDF('P','mm','A4');
$pdf->AliasNbPages();
$pdf->AddPage('L','A4',0);
$pdf->viewTable($db);
$pdf->Output("D", "$id.pdf");
ob_end_flush();
?>
Got it! I created ZipArchive and sent file path to zip file.
$pdf->Output("F", "$id.pdf");
$zip = new ZipArchive;
$tmp_file = 'Documents.zip';
//if ($zip->open($tmp_file, ZipArchive::CREATE))
if($zip->open($tmp_file, ZipArchive::OVERWRITE|ZipArchive::CREATE)) {
$zip->addFile("$id.pdf", "$id.pdf");
$zip->close();
echo 'Archive created!';
header('Content-disposition: attachment; filename=Documents.zip');
header('Content-type: application/zip');
} else {
echo 'Failed!';
}
ob_end_clean();
readfile($tmp_file);

FPDF error: Some data has already been output, can't send PDF file (output started at C:\xampp\htdocs\movie\form.php:15)

I want to create a PDF from a form, but im stuck. It says
"Some data has already been output" and I know it's the $name. But any
idea how I can solve this problem?
<?php
ob_end_clean();
if(!empty($_POST['submit']))
{
$name= $_POST['name'];
$date = $_POST['date'];
$movie = $_POST['movie'];
}
require_once ('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,$name);
$pdf->Output();
ob_end_flush();
?>
There can't be any text output to page. Because of undefined variable there is this text:
Notice: Undefined variable: name in C:\xampp\htdocs\movie\form.php on line 15
Because of that error text, there is problem with exporting PDF.
You have two options.
Option 1:
Just put your FPDF code into if clause. This way it will be defined.
<?php
ob_end_clean();
if(!empty($_POST['submit'] && !empty(&_POST['name'])
{
$name= $_POST['name'];
$date = $_POST['date'];
$movie = $_POST['movie'];
require_once ('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,$name);
$pdf->Output();
ob_end_flush();
}
?>
Option 2:
Set value for $name, if not submited.
<?php
ob_end_clean();
if(!empty($_POST['submit'])
{
$name= $_POST['name'];
$date = $_POST['date'];
$movie = $_POST['movie'];
} else {
$name= '';
}
require_once ('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,$name);
$pdf->Output();
ob_end_flush();
?>

how to add more than one image in pdf file using fpdf

<?php
include 'connection/dbconnect.php';
$id = $_GET['id'];
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image('new/'.$id.'/1.png',10,10,-220);
$pdf->Output(); $pdf1->AddPage();
$pdf1->Image('new/'.$id.'/1.png',10,10,-220);
$pdf1->Output();
?>
Here I want to add more than one image in pdf file using fpdf.
For that I write this code but it displaying only one image.
Can anyone tell me what is the problem in my code.
The Solution on This Problem is
<?php
include 'connection/dbconnect.php';
$id = $_GET['id'];
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image('new/'.$id.'/1.png',10,10,-220);
$pdf->SetAutoPageBreak(true);
$pdf->AddPage();
$pdf->Image('new/'.$id.'/2.png',10,10,-220);
$pdf->Output();
?>

FPDF - PHP While loop only returns first value

I have this code:
while ($row = mysql_fetch_array($result)) {
$pdf=new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$pdf->SetFont('Arial','B',16);
$pdf->Cell(42,10, "".$row["firstname"].$row["lastname"]);
$pdf->Output();
}
It only generates the first value of the result (1 pdf page). There are supposed to be 9 results or pdf pages. In this case where the first and last name of all the players appear in 9 seperate pdf pages.
I tried the following PHP way but it did not work either.
while () {
} echo "":
Any ideas?
This should be what you are after
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->SetFont('Arial','B',16);
while ($row = mysql_fetch_array($result)) {
$pdf->AddPage();
$pdf->Cell(42, 10, $row["firstname"] . " " . $row["lastname"]);
}
$pdf->Output();

Categories