1

I am trying to split a PDF into separate pages, then use a user provided organizational pattern (like how you print various pages in Word), and then merge the separate pages into separate documents and save them to disk.

My problem is that when I try to save I get this exception:

"is_file() expects parameter 1 to be a valid path, object given"

Upon further digging I think the problem is that I am providing object of type Result instead of type ResultFile to the ConvertApi::convert() method.

My current implementation looks generally like this:

$pdf = new Pdf_ConvertApi($input);
$pdf->chunk(['method'=>'pattern', 'chunkPattern'=>[1, '3-4']]);
$pdf->write();

In the constructor I split the PDF files and save the individual pages in $this->rawFiles.

public function __construct($input, $parameters = [])
{
   if (is_array($input)) $key = 'Files';
   else $key = 'File';

   $this->splitResult = ConvertApi::convert('split', [$key => $input]);
   $this->rawFiles = $this->splitResult->getFiles();
}

Then in the chunk method I use the provided pattern to assign the $this->preparedDocuments array which is a multi-level array with the top level being a separate document and the next level containing the appropriate value from $this->rawFiles.

...
$patterns = <what pages to include>
...
foreach ($patterns as $idx => $docPattern)
{
// ... reList() converts patterns like "2-5" into the list [2, 3, 4, 5]
   $document = $this->reList($docPattern, count($this->rawFiles)); 
   $newDoc = [];

   foreach ($document as $pageNumber)
   {
      $pg       = $pageNumber - 1;          // convert to zero-based
      $newDoc[] = $this->rawFiles[$pg];
   }
   $documents[$idx] = ConvertApi::convert('merge', ['Files' => $newDoc]);
}
...
$this->preparedDocuments=$documents;        }

The write method looks like this:

public function write($outputPaths=null)
{
...
   foreach($this->preparedDocuments as $idx=>$document)
   {
      $outputFile = $outputPaths[$idx];

      $mergeResult = ConvertApi::convert('merge', ['File' => $document]);
      $savedFiles[$idx]  = $mergeResult->saveFiles($outputFile);
   }
   return $savedFiles;
}

My expectation was that the constructor was essentially doing this:

$splitResult = ConvertApi::convert('split', ['File' => 'files/test.pdf']);
$files = $splitResult->getFiles();

The chunk() method was doing this:

$firstPage = $files[0];
$lastPage = end($files);
$firstAndLast = [$firstPage, $lastPage];

And the write() method was doing this:

$mergeResult = ConvertApi::convert('merge', ['Files' => $firstAndLast]);
$savedFiles = $mergeResult->saveFiles($dir);

However the values in the array of "firstAndLast" values seem to hold Result objects instead of the expected ResultFile objects.

This is my first try using ConvertAPI, so any insight or suggestions is appreciated.

Bemuzzled
  • 11
  • 1
  • 2

1 Answers1

0

Your question is quite complex, but in general you are doing split and merge, just like in this example:

https://github.com/ConvertAPI/convertapi-php/blob/master/examples/split_and_merge_pdf.php

This example is working and merge accepts file results from the split output.

I suggest you to implement your script in smaller steps - first modify this example to work with multiple chunks and then refactor it into the class.

Laurynas
  • 3,829
  • 2
  • 32
  • 22