I have the below file that opens a pdf and puts some text at the top. This is working fine. But what i want to do now is replace mypdf.pdf with the results of a mysql query. Similar to this:
$pageCount = $pdf->setSourceFile('../folder/folder/$mypdf');
and
$pdf->Write(0, '$sampletext');

Each person going to this page could receive a different pdf, hence the need to query the database to get the pdf they need.
Here is my code page:

<?php
//  brings the id from previous page to use in database query
$id = $_GET['id'];
// trying to query database errors the page


use setasign\Fpdi\Fpdi;
require_once 'fpdi/src/autoload.php';
require_once('fpdf/fpdf.php');

// initiate FPDI
$pdf = new Fpdi();

// get the page count
$pageCount = $pdf->setSourceFile('../folder/folder/mypdf.pdf');
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    // import a page
    $templateId = $pdf->importPage($pageNo);

    $pdf->AddPage();
    // use the imported page and adjust the page size
    $pdf->useTemplate($templateId, ['adjustPageSize' => true]);

    // now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 10);
$pdf->Write(0, 'This is just a simple text');
}

// Output the new PDF
$pdf->Output();
?>

How can i query the database and put in my variables without erroring the page?

I hope this makes sense. If someone can point me in the right direction it would be appreciate.

Thanks.

I presume that you have a name saved for the user in your database table. Now return the string and add that to your "SetSourceFile" path -

// get the page count
$pageCount = $pdf->setSourceFile('../folder/folder/mypdf.pdf');

//Becomes...
$mypdf = "Whatever_name_was_returned_from_table";
$pageCount = $pdf->setSourceFile('../folder/folder/') & $mypdf & (.pdf');
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.