FPDF/FPDI Import Page

I have a database that is printing a medication list on to an existing PDF template. The template currently has room for 4 medications. If the user has 5-10 medications, I would like to generate a second page with the same header information and same background template PDF on the second page.

I am also having so difficulty with spacing.

PLEASE HELP!


<?php
require_once('auth.php');      //Confirm user is logged in
require_once('config.php');    //Database connection details
require_once('connect.php');   //Connect to database

$sql="SELECT * FROM $tbl_members WHERE username = '" . $_SESSION['SESS_LOGIN'] . "'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

$sqlmedications="SELECT * FROM $tbl_medications WHERE username = '" . $_SESSION['SESS_LOGIN'] . "'";
$resultmedications=mysql_query($sqlmedications);

$name = $lastname = $rows['lastname']. ", " .$rows['firstname'];
$dob = $rows['dob'];
$session = $rows['session'];
$village = $rows['village'];
$allergylist = $rows['allergylist'];
$allergy = $rows['allergy'];

require_once('pdf/fpdf.php');
require_once('pdf/fpdi.php');


// initiate FPDI
$pdf =& new FPDI();
// add a page
$pdf->AddPage('L', 'Letter');
// set the sourcefile
$pdf->setSourceFile('pdf/chart_template.pdf');
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx);

// now write some text above the imported page
$pdf->SetTextColor(0,0,255);

$pdf->SetFont('Arial', 'B');
$pdf->SetFontSize(18);

$pdf->SetXY(167, 8);
$pdf->Cell(100,5,$name,1,0,'R');

$pdf->SetXY(7, 8);
$pdf->Cell(100,5,$village,1,0,'L');

$pdf->SetFont('Arial');
$pdf->SetFontSize(14);

$pdf->SetXY(7, 15);
$pdf->Cell(100,5,$session,1,0,'L');

$pdf->SetFontSize(12);
$pdf->SetXY(167, 16);
if ($allergy == "no") {
$pdf->Cell(100,5,'No known allergies',1,0,'R'); }
$pdf->SetXY(167, 16);
$pdf->Cell(100,5,$allergylist,1,0,'R');

$pdf->SetXY(7, 26);
$pdf->SetFontSize(11);
while($rowsmedications=mysql_fetch_array($resultmedications)){
	$pdf->SetFont('Arial', 'B');
	$pdf->Cell(115,5,$rowsmedications['med']. " (" .$rowsmedications['strength']. ")",1,0,'L');

	$pdf->SetFont('Arial');
	$pdf->SetFontSize(9);
	$pdf->SetX(127);
	$pdf->Cell(140,5,$rowsmedications['info'],1,0,'R');
		
	$pdf->Ln(10);
	$pdf->SetX(7);
	$pdf->MultiCell(28,3,$rowsmedications['dose'],1,'C');
	$pdf->SetFontSize(8);
	$pdf->Ln(2);

$sum = $rowsmedications['breakfast'] + $rowsmedications['lunch'] + $rowsmedications['dinner'] + $rowsmedications['bedtime'] + $rowsmedications['prn'];
if ($rowsmedications['breakfast'] == 1) {$breakfast = "Breakfast\\r\
"; }
if ($rowsmedications['lunch'] == 1) {$lunch = "Lunch\\r\
"; }
if ($rowsmedications['dinner'] == 1) {$dinner = "Dinner\\r\
"; }
if ($rowsmedications['bedtime'] == 1) {$bedtime = "Bedtime\\r\
"; }
if ($rowsmedications['prn'] == 1) {$prn = "As Needed\\r\
"; }

$timestogive = $breakfast.$lunch.$dinner.$bedtime.$prn;
if ($sum == 1) { $timestogive = $timestogive."\\r\
\\r\
\\r\
\\r\
\\r\
"; }
if ($sum == 2) { $timestogive = $timestogive."\\r\
\\r\
\\r\
\\r\
"; }
if ($sum == 3) { $timestogive = $timestogive."\\r\
\\r\
\\r\
"; }
if ($sum == 4) { $timestogive = $timestogive."\\r\
\\r\
"; }
if ($sum == 5) { $timestogive = $timestogive."\\r\
"; }

$pdf->Write(3,$timestogive);

$breakfast = "";
$lunch = "";
$dinner = "";
$bedtime = "";
$prn = "";
$timestogive = "";

	
	$pdf->Ln(11.5);
	$pdf->SetFontSize(9);
	$pdf->SetX(7);
}


$pdf->Output('Health Form.pdf', 'I');

?>

ambrosem, were you successful in doing that?. If I understood right, on the second page which you would create you want only parts of the content from the pdf that is read, is it so?.

It seems with the -xpdf package- we could read the contents from a pdf file, but manipulating/aligning the content read to fit a pdf is something I am clueless about now.