Powered By Blogger

Search Here!

Tuesday, October 8, 2024

Get Pdf Page Counts

 

const pdfParse = require("pdf-parse"); 
* Retrieves the total number of pages in a specified or latest
downloaded PDF file.
 *
 * @param {string|null} [fileName=null] - The name of the PDF file.
*If null, the latest downloaded file will be used.
 * @returns {Promise<number>} - The total number of pages in the
*PDF file.
 */
async function getPdfPageCount(fileName = null) {
    // Define the download directory path
    const downloadDir = CONSTANTS.FILE.FILE_PATH;

    let pdfFilePath;

    if (fileName !== null) {
        // Use the provided filename to construct the file path
        pdfFilePath = `${downloadDir}/${fileName}`;
        console.log(`Getting page count from the specified file:
                        ${fileName}`);
    } else {
        // Get the latest downloaded file name
        const latestDownloadedFileName = await
                        this.getLatestDownloadedFileName();

        // Construct the full path of the latest downloaded PDF file
        pdfFilePath = `${downloadDir}/${latestDownloadedFileName}`;
        console.log(
            `Getting page count from the latest downloaded file:
                            ${latestDownloadedFileName}`
        );
    }

    // Read the PDF file
    const dataBuffer = fs.readFileSync(pdfFilePath);

    // Parse the PDF and extract data
    const data = await pdfParse(dataBuffer);

    // Return the total number of pages
    return data.numpages; // This will return the number of pages
    // starting from 1
}

No comments:

Post a Comment