Jaffrey98
10/23/2017 - 4:14 PM

PHP and python script for no of pgs in a pdf

PHP and python script for finding no. of pgs in a pdf

# from PyPDF2 import PdfFileReader
# # pdf = PdfFileReader(open("C:/UsersJaffrey Joy/Desktop/name.pdf",'rb'))
# pdf = PdfFileReader(open("file.pdf",'rb'))
# print(pdf.getNumPages())

#To install PyPDF2 package :- pip install PyPDF2

import sys
pdfname=sys.argv[1]

from PyPDF2 import PdfFileReader

pdf = PdfFileReader(open(pdfname,'rb'))
print(pdf.getNumPages())

/*
First function requires an executable called pdfinfo.exe
Alternative function using python script needs PyPDF2 package to be installed
*/


<?php
// Make a function for convenience 
function getPDFPages($document)
{
    // $cmd = "/path/to/pdfinfo";           // Linux
    // $cmd = "C:\\path\\to\\pdfinfo.exe";  // Windows
    $cmd = "pdfinfo";  // Windows

    // Parse entire output
    // Surround with double quotes if file name has spaces
    exec("$cmd \"$document\"", $output);

    // Iterate through lines
    $pagecount = 0;
    foreach($output as $op)
    {
        // Extract the number
        if(preg_match("/Pages:\s*(\d+)/i", $op, $matches) === 1)
        {
            $pagecount = intval($matches[1]);
            break;
        }
    }

    return $pagecount;
}

function getPDFPages_py($document)
{
    // $cmd = "/path/to/pdfinfo";           // Linux
    // $cmd = "C:\\path\\to\\pdfinfo.exe";  // Windows
    $cmd = "python pdf.py ".$document."";  // Windows

    // Parse entire output
    // Surround with double quotes if file name has spaces
    exec("$cmd \"$document\"", $pagecount);

    return $pagecount[0];
}

// Use the function
echo getPDFPages_py("file.pdf");  // Output: 2

?>