Showing posts with label php pdfparser. Show all posts
Showing posts with label php pdfparser. Show all posts

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;

?>