node-pdftk icon indicating copy to clipboard operation
node-pdftk copied to clipboard

How to split the pdf into pages

Open shivarajnaidu opened this issue 4 years ago • 9 comments

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 !

shivarajnaidu avatar May 05 '20 01:05 shivarajnaidu

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.

jjwilly16 avatar May 11 '20 16:05 jjwilly16

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

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

gustawdaniel avatar Jun 04 '20 09:06 gustawdaniel

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

gustawdaniel avatar Jun 04 '20 10:06 gustawdaniel

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 avatar Jun 04 '20 11:06 gustawdaniel

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

  • burst
  • output

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.

gustawdaniel avatar Jun 04 '20 11:06 gustawdaniel

@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(()=> {
        
      })

jjwilly16 avatar Jun 04 '20 11:06 jjwilly16

Hi @jjwilly16

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

gustawdaniel avatar Jun 04 '20 12:06 gustawdaniel

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

gustawdaniel avatar Jun 04 '20 12:06 gustawdaniel

  • [x] new feature
  • [x] backward compatibility
  • [x] documentation
  • [x] tests

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

Please for review.

gustawdaniel avatar Jun 04 '20 13:06 gustawdaniel