python split pdf pages

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>

using namespace std;

int main() {
    // Replace "input.pdf" with the path to your input PDF file
    string inputFileName = "input.pdf";

    // Open the input PDF file in binary mode
    ifstream inputFile(inputFileName, ios::binary);

    if (!inputFile) {
        cerr << "Error: Unable to open input file: " << inputFileName << endl;
        return 1;
    }

    // Read the entire content of the input PDF file into a vector
    vector<unsigned char> buffer((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());

    // Close the input file stream
    inputFile.close();

    // Specify the number of pages to split the PDF into
    int numPages = 3; // Change this number as needed

    // Calculate the size of each page based on the total size of the PDF
    int pageSize = buffer.size() / numPages;

    // Split the PDF into separate pages and save each page as a new PDF file
    for (int i = 0; i < numPages; ++i) {
        // Create a new output file for each page (e.g., page_1.pdf, page_2.pdf, ...)
        stringstream outputFileName;
        outputFileName << "page_" << (i + 1) << ".pdf";

        // Open the output file in binary mode
        ofstream outputFile(outputFileName.str(), ios::binary);

        if (!outputFile) {
            cerr << "Error: Unable to create output file: " << outputFileName.str() << endl;
            return 1;
        }

        // Calculate the start and end positions of the current page in the buffer
        int start = i * pageSize;
        int end = (i == numPages - 1) ? buffer.size() : start + pageSize;

        // Write the content of the current page to the output file
        copy(buffer.begin() + start, buffer.begin() + end, ostreambuf_iterator<char>(outputFile));

        // Close the output file stream
        outputFile.close();

        cout << "Page " << (i + 1) << " extracted and saved as " << outputFileName.str() << endl;
    }

    return 0;
}

This C++ code takes an input PDF file and splits it into separate pages based on the specified number of pages (numPages). It reads the entire content of the input PDF into memory, calculates the size of each page, and then creates new PDF files for each extracted page. The extracted pages are saved as individual PDF files named page_1.pdf, page_2.pdf, and so on. Adjust the numPages variable to change the number of pages each PDF is split into.