i - 2012-08-05 21:14:21
I have a list of PDF files generated from a MySQL database. The user is able to select different files via check boxes. The script then needs to extract one page from each file and create a new pdf file with thumbnails of the extracted pages.
I will post my code below. This code "works" but the new generated pdf shows only the thumbnail of the last selected file, so if the user selects 3 files to be extracted I get 3 identical thumbnails on the new PDF.
I am using FPDF and FPDI
Code:
$_SESSION['clients'] = $_POST['selectclient'];
$total_clients = count($_SESSION['clients']);
foreach($_SESSION['clients'] as $key => &$value){
$client = (mysql_query("SELECT * FROM profiles WHERE clientid = $value ORDER BY profiledate ASC"));
$urls = array();
while($row = mysql_fetch_array($client)){
$destination = 'clients/clientprofiles/';
$urls[] = $row['profileurl'];
$pdf = new FPDI();
foreach($urls as &$file){
$pagecount = $pdf->setSourceFile($destination.$file);
$tplidx = $pdf->ImportPage(3);
$w = $pdf->w/2 - 15;
$h = 0;
$_x = $x = 10;
$_y = $y = 10;
$pdf->addPage();
for ($n = 1; $n <= $total_clients; $n++) {
$size = $pdf->useTemplate($tplidx, $x, $y, $w);
$pdf->Rect($x, $y, $size['w'], $size['h']);
$h = max($h, $size['h']);
if ($n % 2 == 0) {
$y += $h+10;
$x = $_x;
$h = 0;
} else {
$x += $w+10;
}
if ($n % 4 == 0 && $n != $total_clients) {
$pdf->AddPage();
$x = $_x;
$y = $_y;
}
}
}
unset($file);
}
unset($value);
}
$pdf->Output('newpdf.pdf', 'D');