Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ $merger->addFile('bar.pdf', new Pages('1-10'));
$createdPdf = $merger->merge();
```

Merge **foo.pdf** with **bar.pdf** and add a blank page if necessary to ensure **bar.pdf** starts on the front of the page when printing in duplex:

```php
use iio\libmergepdf\Merger;
use iio\libmergepdf\Pages;

$merger = new Merger;
$merger->addFile('foo.pdf');
$merger->addFile('bar.pdf', null, true);
$createdPdf = $merger->merge();
```

Bulk add files from an iterator:

```php
Expand Down
4 changes: 4 additions & 0 deletions src/Driver/Fpdi2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function merge(SourceInterface ...$sources): string
$pageCount = $fpdi->setSourceFile(StreamReader::createByString($source->getContents()));
$pageNumbers = $source->getPages()->getPageNumbers() ?: range(1, $pageCount);

if (isset($size) && $source->getDuplex() && $fpdi->PageNo() % 2 !== 0) {
$fpdi->AddPage($size['width'] > $size['height'] ? 'L' : 'P', [$size['width'], $size['height']]);
}

foreach ($pageNumbers as $pageNr) {
$template = $fpdi->importPage($pageNr);
$size = $fpdi->getTemplateSize($template);
Expand Down
4 changes: 4 additions & 0 deletions src/Driver/TcpdiDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function merge(SourceInterface ...$sources): string
$pageCount = $tcpdi->setSourceData($source->getContents());
$pageNumbers = $source->getPages()->getPageNumbers() ?: range(1, $pageCount);

if (isset($size) && $source->getDuplex() && $tcpdi->PageNo() % 2 !== 0) {
$tcpdi->AddPage($size['w'] > $size['h'] ? 'L' : 'P', [$size['w'], $size['h']]);
}

foreach ($pageNumbers as $pageNr) {
$template = $tcpdi->importPage($pageNr);
$size = $tcpdi->getTemplateSize($template);
Expand Down
12 changes: 6 additions & 6 deletions src/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ public function __construct(DriverInterface $driver = null)
/**
* Add raw PDF from string
*/
public function addRaw(string $content, PagesInterface $pages = null): void
public function addRaw(string $content, PagesInterface $pages = null, bool $duplex = false): void
{
$this->sources[] = new RawSource($content, $pages);
$this->sources[] = new RawSource($content, $pages, $duplex);
}

/**
* Add PDF from file
*/
public function addFile(string $filename, PagesInterface $pages = null): void
public function addFile(string $filename, PagesInterface $pages = null, bool $duplex = false): void
{
$this->sources[] = new FileSource($filename, $pages);
$this->sources[] = new FileSource($filename, $pages, $duplex);
}

/**
Expand All @@ -54,10 +54,10 @@ public function addFile(string $filename, PagesInterface $pages = null): void
* @param iterable<string> $iterator Set of filenames to add
* @param PagesInterface $pages Optional pages constraint used for every added pdf
*/
public function addIterator(iterable $iterator, PagesInterface $pages = null): void
public function addIterator(iterable $iterator, PagesInterface $pages = null, bool $duplex = false): void
{
foreach ($iterator as $filename) {
$this->addFile($filename, $pages);
$this->addFile($filename, $pages, $duplex);
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/Source/FileSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ final class FileSource implements SourceInterface
*/
private $pages;

public function __construct(string $filename, PagesInterface $pages = null)
/**
* @var bool
*/
private $duplex;

public function __construct(string $filename, PagesInterface $pages = null, bool $duplex = false)
{
if (!is_file($filename) || !is_readable($filename)) {
throw new Exception("Invalid file '$filename'");
}

$this->filename = $filename;
$this->pages = $pages ?: new Pages;
$this->duplex = $duplex;
}

public function getName(): string
Expand All @@ -47,4 +53,9 @@ public function getPages(): PagesInterface
{
return $this->pages;
}

public function getDuplex(): bool
{
return $this->duplex;
}
}
13 changes: 12 additions & 1 deletion src/Source/RawSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ final class RawSource implements SourceInterface
*/
private $pages;

public function __construct(string $contents, PagesInterface $pages = null)
/**
* @var bool
*/
private $duplex;

public function __construct(string $contents, PagesInterface $pages = null, bool $duplex = false)
{
$this->contents = $contents;
$this->pages = $pages ?: new Pages;
$this->duplex = $duplex;
}

public function getName(): string
Expand All @@ -42,4 +48,9 @@ public function getPages(): PagesInterface
{
return $this->pages;
}

public function getDuplex(): bool
{
return $this->duplex;
}
}
5 changes: 5 additions & 0 deletions src/Source/SourceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public function getContents(): string;
* Get pages to fetch from source
*/
public function getPages(): PagesInterface;

/**
* Get duplex printing
*/
public function getDuplex(): bool;
}
1 change: 1 addition & 0 deletions tests/Driver/Fpdi2DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function testMerge()
$source->getName()->willReturn('');
$source->getContents()->willReturn('');
$source->getPages()->willReturn(new Pages('1, 2'));
$source->getDuplex()->willReturn(false);

$this->assertSame(
'created-pdf',
Expand Down
1 change: 1 addition & 0 deletions tests/Driver/TcpdiDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function testMerge()
$source->getName()->willReturn('');
$source->getContents()->willReturn('data');
$source->getPages()->willReturn(new Pages('1, 2'));
$source->getDuplex()->willReturn(false);

$this->assertSame(
'created-pdf',
Expand Down
8 changes: 8 additions & 0 deletions tests/Source/FileSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ public function testGetPages()
(new FileSource(__FILE__, $pages))->getPages()
);
}

public function testGetDuplex()
{
$this->assertSame(
true,
(new FileSource(__FILE__, null, true))->getDuplex()
);
}
}
8 changes: 8 additions & 0 deletions tests/Source/RawSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public function testGetPages()
(new RawSource('', $pages))->getPages()
);
}

public function testGetDuplex()
{
$this->assertSame(
true,
(new RawSource(__FILE__, null, true))->getDuplex()
);
}
}