Hi,
I need to create multiple pdf files with php, I’ve achieved it with the following code using mpdf library. The only problem is that the PDF files are created after user sibmit a form and if is a lot of files (I’ve tested it with 50) then the scripts takes some time (2min) and I believe this can cause problem with server execution time and also for the user that has to wait such a long time.
Do you have any suggestion how I can solve the problem? I did find this link https://medium.com/@Jessicawlm/how-to-generate-a-large-pdf-file-with-php-ed9ee7aab41b where they suggest to use wkhtmltopdf but not sure if I can install it on shared server. I was also thinking maybe I can show the content of each PDF file into a html page and then the user can download (I can use javascript for it)
This is my code
// Query to database to insert user
$query= "INSERT INTO km_users (
km_user_first_name,
km_user_last_name,
km_user_fiscalcode,
km_user_document,
km_user_document_number,
km_user_document_exp,
km_user_birth_place,
km_user_birth_date,
km_user_telephone,
km_user_mobile,
km_user_address,
km_user_postcode,
km_user_city,
km_user_city_prov,
km_user_username,
km_user_password,
km_user_role,
km_user_email,
km_user_website,
km_user_active,
km_user_activation_code)
VALUES (?,?,'','','',NULL,'',NULL,'','','','','','',?,?,'4',?,'','1',NULL)";
if($stmt = mysqli_prepare($db_user_conn, $query)){
$time_start = microtime(true);
foreach($kmg_owner_firstname as $index => $value) {
// Create a random username if admin decide to create a online profile for the user
$km_random_username = ($kmg_create_owner_profile == 1) ? random_int(100000, 999999) : NULL;
// Set default password value to null
$km_random_password = NULL;
// Create a random password if admin decide to create a online profile for the user
if($kmg_create_owner_profile == 1){
$km_generate_random_password = km_random_password();
$km_random_password = password_hash($km_generate_random_password, PASSWORD_DEFAULT);
}
mysqli_stmt_bind_param($stmt, 'sssss', $kmg_owner_firstname[$index], $kmg_owner_lastname[$index], $km_random_username, $km_random_password, $kmg_owner_email[$index]);
mysqli_stmt_execute($stmt);
// Get last user id for each user imported into the database
$user_id= filter_var(mysqli_insert_id($db_user_conn), FILTER_SANITIZE_NUMBER_INT);
// Push user id into the array
array_push($user_ids, $user_id);
// Check if admin wants to creade pdf letters
if($km_create_owner_letter == 1){
$filename = time().'Performance_review.pdf';
sleep(2);
$mpdf = new \Mpdf\Mpdf();
// Render the view to create the html letter
$html= $twig->render('/km-letters/km-letter-new-user.twig', array(
// Render user details
'user' => array(
'firstName' => $kmg_owner_firstname[$index],
'lastName' => $kmg_owner_lastname[$index],
'userName' => $km_random_username,
'userPassword' => $km_generate_random_password
)
));
$mpdf->WriteHTML($html);
$get_pdf = $mpdf->Output($km_files_path.KM_DS.$filename,'F');
}
}
}else{
throw new Exception('Si è verificato un errore nel tentativo di interrogare il database! Per favore riprova più tardi!', KM_ERROR_CODE);
}
// Get real path for our folder
$rootPath = realpath($km_files_path);
// Initialize archive object
$zip = new ZipArchive();
$zip->open($km_folder_path.'file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Initialize empty "delete list"
$filesToDelete = array();
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file){
// Skip directories (they would be added automatically)
if (!$file->isDir()){
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
// Delete pdf files form directory
$filesToDelete[] = $filePath;
}
}
// Zip archive will be created only after closing object
$zip->close();
// Delete all files from "delete list"
foreach ($filesToDelete as $file){
unlink($file);
}