i have a query that retrieves let's say 10 records. the output should be 10 pdf files. so 1 pdf per records. it can be saved on user's computer. im using fpdf by the way. just wondering if i'm going to develop it through web using PHP will it be easier.

dont have any idea how to do it.

thanks.

Dani AI

Generated

Short answer: a single HTTP response cannot reliably push 10 separate files to the client. Two practical, common approaches are (A) generate each PDF server-side and bundle them into a ZIP for one download, or (B) generate PDFs on demand and trigger separate downloads (one request per file) from the browser. was pointing toward the on-demand model — that works well for single documents but can run into browser download blockers or UX problems if triggered 10 times in one go.

Server-side bundle (recommended): create each PDF with FPDF, save to a safe temp folder, add them to a ZipArchive, send the zip with proper headers, then delete temp files. Minimal example:

// inside loop for each DB row
$pdf->AddPage();
// ... build pdf ...
$path = sys_get_temp_dir() . '/doc_' . $row['id'] . '.pdf';
$pdf->Output('F', $path);
$files[] = $path;

// after loop
$zipPath = sys_get_temp_dir() . '/docs_' . time() . '.zip';
$zip = new ZipArchive();
if ($zip->open($zipPath, ZipArchive::CREATE) === TRUE) {
  foreach ($files as $f) $zip->addFile($f, basename($f));
  $zip->close();
  header('Content-Type: application/zip');
  header('Content-Disposition: attachment; filename="documents.zip"');
  header('Content-Length: ' . filesize($zipPath));
  readfile($zipPath);
}
// cleanup temp files

Notes and troubleshooting: ensure ZipArchive is available, write permissions for the temp folder, and enough memory/time (use set_time_limit(0) for big batches). Prefer sys_get_temp_dir() or a non-web-accessible folder. If real-time per-file downloads are required, use AJAX to request each PDF separately or generate pre-signed URLs (S3) and present a list of links — beware modern browsers blocking multiple automatic downloads. Finally, always clean up temp files, use unique filenames, and handle errors so partial jobs are removed.

Recommended Answers

All 2 Replies

I am using mpdf for example and it works just great.
My aproach is that every time a customer request a document it's get generated on the fly and it's pushed to download via a header function.

thanks for the idea. i'm using fpdf by the way. i tried to search for samples. unfortunately ,found nothing. it wil be a great help for me if you have sample codes.

thanks.

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.