MuhammaraJS icon indicating copy to clipboard operation
MuhammaraJS copied to clipboard

unable to append page, make sure it's fine

Open coolhand79 opened this issue 8 months ago • 6 comments

We're still seeing this issue. Attached is a sample PDF that fails when attempting to merge. blank-bad.pdf

Our merge code looks like this:

export async function merge(
    inputPath: string,
    coversheetPath: string,
    outputPath: string
): Promise<void> {

    const outStream = new memoryStreams.WritableStream();

    const coversheetPDFBuffer = fs.readFileSync(coversheetPath);
    const reportPDFBuffer = fs.readFileSync(inputPath);

    try {
        const firstPDFStream = new muhammara.PDFRStreamForBuffer(coversheetPDFBuffer);
        const secondPDFStream = new muhammara.PDFRStreamForBuffer(reportPDFBuffer);

        const pdfWriter = muhammara.createWriterToModify(
            firstPDFStream,
            new muhammara.PDFStreamForResponse(outStream)
        );
        pdfWriter.appendPDFPagesFromPDF(secondPDFStream);
        pdfWriter.end();
        const newBuffer = outStream.toBuffer();
        outStream.end();

        fs.writeFileSync(outputPath, newBuffer);
    } catch (e: any) {
        outStream.end();
        throw new Error('Error during PDF combination: ' + e.message);
    }
}

When attempting to merge with this file blank_coversheet.pdf the error returned will be unable to append page, make sure it's fine

Originally posted by @coolhand79 in https://github.com/julianhille/MuhammaraJS/issues/309#issuecomment-2164017368

coolhand79 avatar Jun 13 '24 15:06 coolhand79