jjwilly16 / node-pdftk

A wrapper for PDF Toolkit with streams and promises.
MIT License
141 stars 34 forks source link

How to split the pdf into pages #39

Open shivarajnaidu opened 4 years ago

shivarajnaidu commented 4 years ago

Any sample code snippet to use this package to split the pdf into separate pages ? It will be very useful if we can get one ..

Thank you !

jjwilly16 commented 4 years ago

This method is pretty straightforward:

pdftk
    .input('./multiPage.pdf')
    .burst()
    .then(() => {
        // There is no output - files will be created in working directory
        // You'll have to use 'fs' to read them here
    })

Unfortunately, this method has no output - it just writes the files in the working directory. For now, you'll have to read the files manually after creation.

In the future, I'd like to enhance this method to automatically retrieve the new files and return them but don't have the time right now. Pull requests are always welcome, though.

gustawdaniel commented 4 years ago

I really need this feature. I am working on ArrayBuffers.

I will try to extend it but can't promise success.

gustawdaniel commented 4 years ago

Ok. I can't handle with this problem, but at least updated dependencies.

gustawdaniel commented 4 years ago

How burst works:

$ ls
document1.pdf
$ pdftk document1.pdf burst
$ ls
doc_data.txt  document1.pdf  pg_0001.pdf  pg_0002.pdf  pg_0003.pdf  pg_0004.pdf  pg_0005.pdf

but command

pdftk
    .input('./multiPage.pdf')
    .burst()
    .then(() => {
        // There is no output - files will be created in working directory
        // You'll have to use 'fs' to read them here
    })

creates files in main directory where server is running.

gustawdaniel commented 4 years ago

There is proposed info how to change location of output

https://stackoverflow.com/questions/6598937/set-output-location-for-pdftk-sample-pdf-burst

pdftk your_file.pdf burst output your_directory/page_%02d.pdf

But in our package both

returns promise, when at least one of them should return this object.

Or we can add arguments to burst that will redirect these files to directory selected by user. Also wee need option that allow to receive array of buffers in promise resolved by burst.

jjwilly16 commented 4 years ago

@gustawdaniel Thanks for pointing that out. You can actually pass the output argument to the burst method:

pdftk
      .input(pdf)
      .burst('./your/file/path/page_%02d.pdf')
      .then(()=> {

      })
gustawdaniel commented 4 years ago

Hi @jjwilly16

I fixed it now! I will write docs and send pull request to check for you.

gustawdaniel commented 4 years ago

I done it exactly like you commented and extended burst by second argument with saved backward compatibility.

gustawdaniel commented 4 years ago

https://github.com/jjwilly16/node-pdftk/pull/40

Please for review.