Tuesday, October 1, 2024

Count PDF Pages in Folders and Subfolders with PHP

 Below php code provide list of all pdf files and page count under a folder or subfolder into a csv file.

Here you will required PdfParser library. We will installed this using composer.

so go to composer folder and type below command to installed PdfParser library-

C:\composer>composer require smalot/pdfparser


<?php

require 'C:\composer\vendor\autoload.php'; // Load Composer's autoloader

use Smalot\PdfParser\Parser;

function countPdfPages($pdfPath) {

    try {

        $parser = new Parser();

        $pdf = $parser->parseFile($pdfPath);

        return count($pdf->getPages());

    } catch (Exception $e) {

        echo "Error reading $pdfPath: " . $e->getMessage() . PHP_EOL;

        return 0;

    }

}


function getPdfFilesAndCounts($rootFolder) {

    $pdfInfo = [];    

    // Use RecursiveDirectoryIterator to loop through all directories and files

    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootFolder));    

    foreach ($iterator as $fileInfo) {

        if ($fileInfo->isFile() && strtolower($fileInfo->getExtension()) === 'pdf') {

            $pdfPath = $fileInfo->getPathname();

            $pageCount = countPdfPages($pdfPath);

            $pdfInfo[] = [$pdfPath, $pageCount];

        }

    }   

    return $pdfInfo;

}


function writeToCsv($pdfInfo, $outputCsv) {

    $file = fopen($outputCsv, 'w');    

    // Write the CSV header

    fputcsv($file, ['File Path', 'Page Count']);    

    // Write data

    foreach ($pdfInfo as $row) {

        fputcsv($file, $row);

    }    

    fclose($file);

}


// Set the root folder containing your PDF files

$rootFolder = 'Path of Folder'; // Change this to your folder path

$outputCsv = 'pdf_page_counts.csv'; // Output CSV file name

$pdfInfo = getPdfFilesAndCounts($rootFolder);

writeToCsv($pdfInfo, $outputCsv);

echo "Page counts for PDF files have been written to $outputCsv" . PHP_EOL;

?>


Get List All Files in a Folder with PHP Code in CSV

Below code generate a csv file that provide you name of all files exists in the folder.

<?php

function getAllFolders($path) {

    // Check if the path is a directory

    if (!is_dir($path)) {

        return "The specified path is not a directory.";

    }


    // Scan the directory

    $folders = [];

    $items = scandir($path);


    // Filter out the folders

    foreach ($items as $item) {

        if ($item != '.' && $item != '..') { // Skip the current and parent directory

            if (is_dir($path . DIRECTORY_SEPARATOR . $item)) {

                $folders[] = $item; // Add folder name to the array

            }

        }

    }

    return $folders;

}


function saveFoldersToCSV($folders, $csvFilePath) {

    // Open the CSV file for writing

    $file = fopen($csvFilePath, 'w');


    // Write the folder names to the CSV file

    foreach ($folders as $folder) {

        fputcsv($file, [$folder]);

    }


    // Close the file

    fclose($file);

}


// Specify the path to the directory and the CSV file

$path = 'Paste file path here'; // Change this to your desired path

$csvFilePath = 'folders_list.csv'; // Name of the CSV file


$folders = getAllFolders($path);


if (is_array($folders) && !empty($folders)) {

    saveFoldersToCSV($folders, $csvFilePath);

    echo "Folders have been saved to '$csvFilePath'.\n";

} else {

    echo $folders ?: "No folders found in the specified directory.\n";

}

?>