julianhille / MuhammaraJS

Muhammara a node module with c/cpp bindings to modify PDF with js for node or electron (based/replacement on/of galkhana/hummusjs)
Other
205 stars 43 forks source link

Extract Pages from Password PDF that is in a Buffer #289

Closed blankstar85 closed 1 year ago

blankstar85 commented 1 year ago

Hello,

I'm trying to get the code to work from the linked issue #284.

I have a password pdf I need to split parts of the file. How can I use the readerfrom buffer to generate the xobject?

Trying to avoid the file saved to disk and it doesn't look like the createform can take a password.

Jacob

julianhille commented 1 year ago

Please attach a sample "base64 file". Thank you. Actually I think this would not work but maybe I find a way around it.

blankstar85 commented 1 year ago

Well in my application I get the file as a base64 string. So I carry the file as a string stored in a variable. I create a buffer from it with Buffer.from(variable, 'base64'). I can save the original pdf by fs.writefile from the buffer

        fs.writeFile('./testfile.pdf', pdfAt.contentBytes, 'base64', () => {
            console.log('done');
        });. 

So I'm trying to pull the first two pages of the document ( its the irs 1040 instructions just as a sample file to test with ) password is test to open the pdf. base64String.txt

julianhille commented 1 year ago

The password test is not working. So im trying with a sample file of my own.

julianhille commented 1 year ago
    const muhammara = require('muhammara')
    const fs = require('fs')

    // reader
    let mypassword = 'password'
    let file = './password.b64.pdf'
    let content = fs.readFileSync(file, {encoding: 'ascii'})

    let decodedContent = Buffer.from(content, 'base64')
    let reader = new muhammara.PDFRStreamForBuffer(decodedContent)

    // writer
    let destination = './output.pdf'
    let writer = muhammara.createWriter(destination)

    // copy
    let stream = reader.getParserStream()
    stream.setPosition(0)
    writer.appendPDFPagesFromPDF(stream, {password: mypassword})
    writer.end()

You may look for the options of appendPDFPagesFromPDF to set specific range of pages.