bensquire/php-pdftk-toolkit

setInputFile filters don't work if it is called only once

adamzimmermann opened this issue · 5 comments

First of all, thanks for your work on this. I searched all over for a solid php pdf manipulator and love this one.

My issue is that I want to explode a multipage pdf into separate pdf files. So I have a simple loop and I'm using setInputFile() and I thought I would be good. However, the start_page and end_page options that I send it are only working when I use setInputFile() a second time, however I don't want to add a second input file.

I've poked around a bit in your code and couldn't immediately figure it out, so I'm hoping I'm doing something wrong, or you could point me to what I need to alter to get this working.

Oh, and can your class count the pages in the pdf? I didn't see anything about it in the examples or the codebase. It seems that the underlying pdftk library supports it with the numpages property, so maybe it just needs to be implemented here?

Thanks,
Adam

Hi @adamzimmermann

Thanks for the kind words and positive feedback. Thank to your comment and a previous pull request I'm going to spend a few hours going over the code and modernising this library. In the meantime, can you show me an example of your simple loop.

At the moment there isn't anything to count the page, but it shouldn't be anything too complex to implement. I'll have a look.

Cheers
Ben

@bensquire

Thanks for the quick reply!

I'm using this with Drupal, so there is a ton of Drupal specific code mixed in there, but you get the idea. Also, this code is half baked, since we are still nailing down the project requirements, so don't judge my code! haha.

Let me know if I can help in any way.

function generate_pages(&$node, $file) {

  // Include the main TCPDF library (search for installation path).
  require_once('pdftk.php');

  $original_path = drupal_realpath($file->uri);
  $pages = 2;

  // Delete the old files.
  foreach ($node->field_file_pages[LANGUAGE_NONE] as $key => $value) {
    $file = file_load($value['fid']);
    file_delete($file, TRUE);
  }

  for ($i=0; $i < $pages; $i++) {

    $page = $i + 1;
    $filename = sprintf('%s_%d.pdf', $node->nid, $page);
    $filepath = sprintf('/tmp/%s', $filename);

    $options = array(
      "filename" => $original_path,
      'start_page' => $page,
      'end_page' => $page,
      'rotation' => 0,
    );

    $options2 = array(
      "filename" => '/tmp/example.pdf',
      'start_page' => 1,
      'end_page' => 1,
      'rotation' => 90,
    );

    $oPdftk = new pdftk();
    $oPdftk ->setInputFile($options)
            // ->setInputFile($options2)
            ->setOutputFile($filepath);
    $oPdftk->_renderPdf();


    // Create managed File object and associate with Image field.
    $file = (object) array(
      'uid' => 1,
      'uri' => $filepath,
      'filemime' => $file->filemime,
      'status' => 1,
      'display' => 1,
    );


    // We save the file to the root of the files directory.
    $file = file_copy($file, 's3://PUBLIC');

    $node->field_file_pages[LANGUAGE_NONE][$i] = (array)$file;
  }

}

Okay doke, I've setup my own test and I see your issue. I'll see what I can do...

Hi @adamzimmermann ,

I worked out the issue you were having and have pushed a fix to the master branch (or tag v1.0.1).

In addition example7.php demonstrates the exact thing you are attempting.

Kind Regards
Ben

@bensquire Awesome. Thank you! I will try this out first thing tomorrow morning.