Skip to content

Commit

Permalink
Merge pull request #12 from oguzhankrcb/master
Browse files Browse the repository at this point in the history
Add split functionality to EasyPdf class
  • Loading branch information
frkcn authored Nov 1, 2022
2 parents 647aa31 + 7b9d93c commit 304e3ad
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to `easy-pdf` will be documented in this file.

## [Unreleased]

## 2.5.0 - 2022-01-11
- `splitTo()` method added to `Parser` class.

## 2.4.0 - 2022-05-30
- `setPage()` method added for setting current page.

Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Total Downloads](https://img.shields.io/packagist/dt/tarfin-labs/easy-pdf.svg?style=flat-square)](https://packagist.org/packages/tarfin-labs/easy-pdf)

## Introduction
easy-pdf is a [tcpdf](https://tcpdf.org/) wrapper for Laravel 6.x, 7.x and 8.x.
easy-pdf is a [tcpdf](https://tcpdf.org/) wrapper for Laravel 6.x, 7.x, 8.x and 9.x.

## Installation

Expand Down Expand Up @@ -116,6 +116,27 @@ $pdf = EasyPdf::merge($files)
->content();
```

### Splitting pdf

You can split pdf file into multiple pdf files easily using easy-pdf.
``` php
// Pdf path.
// Pdf path can be path, url or blob.

$file = '/path/to/the/file.pdf';

// You can use splitTo method to get new pdf contents
// chunkSize argument determines that how many page should contain every pdf file
// For example if you want to split your pdf file as each file includes 10 page
// Then you must set chunkSize argument to 10
$pdfs = EasyPdf::parser($file)
->splitTo(10);

foreach($pdfs as $pdfContent) {
// Your code here
}
```

### Resetting the instance
If you try to generate pdf inside a Laravel queue, sometimes there might occure an error like `undefined property: TCPDF::$h`.

Expand Down
36 changes: 36 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TarfinLabs\EasyPdf;

use setasign\Fpdi\PdfParser\StreamReader;
use setasign\Fpdi\Tcpdf\Fpdi;

class Parser extends BasePdf
{
Expand Down Expand Up @@ -56,6 +57,41 @@ public function setPage(int $page)
return $this;
}

/**
* Returns splitted pdf file contents.
*
* @param int $chunkSize
* @return array
*
* @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
* @throws \setasign\Fpdi\PdfParser\Filter\FilterException
* @throws \setasign\Fpdi\PdfParser\PdfParserException
* @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
* @throws \setasign\Fpdi\PdfReader\PdfReaderException
*/
public function splitTo(int $chunkSize)
{
$chunks = array_chunk(range(1, $this->count()), $chunkSize);

$streamReader = StreamReader::createByString($this->content);

$splittedFileContents = [];

foreach ($chunks as $chunk) {
$this->pdf = new Fpdi();
$this->pdf->setSourceFile($streamReader);

foreach ($chunk as $index) {
$this->pdf->AddPage();
$this->pdf->useTemplate($this->pdf->importPage($index));
}

$splittedFileContents[] = $this->pdf->Output('doc.pdf', 'S');
}

return $splittedFileContents;
}

/**
* Render given source.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,19 @@ public function it_can_return_pdf_content()

$this->assertSame('application/pdf; charset=binary', $buffer);
}

/** @test */
public function it_can_split_one_file_to_multiple_files()
{
$pdfs = EasyPdf::parser($this->file)
->splitTo(1);

foreach ($pdfs as $pdf) {
$finfo = new finfo(FILEINFO_MIME);
$buffer = $finfo->buffer($pdf);
$this->assertSame('application/pdf; charset=binary', $buffer);
}

$this->assertCount(3, $pdfs);
}
}

0 comments on commit 304e3ad

Please sign in to comment.