Parser::setPage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace TarfinLabs\EasyPdf;
4
5
use setasign\Fpdi\PdfParser\StreamReader;
6
use setasign\Fpdi\Tcpdf\Fpdi;
7
8
class Parser extends BasePdf
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $path;
14
15
    /**
16
     * @var int
17
     */
18
    protected $page;
19
20
    /**
21
     * Parser constructor.
22
     *
23
     * @param  string  $path
24
     *
25
     * @throws Exceptions\UnableToOpen
26
     */
27
    public function __construct(string $path)
28
    {
29
        parent::__construct();
30
31
        $this->path = $path;
32
        $this->setFileContent($this->path);
33
    }
34
35
    /**
36
     * Return pdf page count.
37
     *
38
     * @return int
39
     *
40
     * @throws \setasign\Fpdi\PdfParser\PdfParserException
41
     */
42
    public function count(): int
43
    {
44
        return $this->pdf->setSourceFile(StreamReader::createByString($this->content));
45
    }
46
47
    /**
48
     * Set page for save methods.
49
     *
50
     * @param  int  $page
51
     * @return Parser
52
     */
53
    public function setPage(int $page)
54
    {
55
        $this->page = $page;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Returns splitted pdf file contents.
62
     *
63
     * @param  int  $chunkSize
64
     * @return array
65
     *
66
     * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
67
     * @throws \setasign\Fpdi\PdfParser\Filter\FilterException
68
     * @throws \setasign\Fpdi\PdfParser\PdfParserException
69
     * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
70
     * @throws \setasign\Fpdi\PdfReader\PdfReaderException
71
     */
72
    public function splitTo(int $chunkSize)
73
    {
74
        $chunks = array_chunk(range(1, $this->count()), $chunkSize);
75
76
        $streamReader = StreamReader::createByString($this->content);
77
78
        $splittedFileContents = [];
79
80
        foreach ($chunks as $chunk) {
81
            $this->pdf = new Fpdi();
82
            $this->pdf->setPrintHeader(false);
83
            $this->pdf->setPrintFooter(false);
84
85
            $this->pdf->setSourceFile($streamReader);
86
87
            foreach ($chunk as $index) {
88
                $this->pdf->AddPage();
89
                $this->pdf->useTemplate($this->pdf->importPage($index));
90
            }
91
92
            $splittedFileContents[] = $this->pdf->Output('doc.pdf', 'S');
93
        }
94
95
        return $splittedFileContents;
96
    }
97
98
    /**
99
     * Render given source.
100
     *
101
     * @return Parser
102
     */
103
    public function render()
104
    {
105
        $this->pdf->AddPage();
106
107
        $this->pdf->setSourceFile(StreamReader::createByString($this->content));
108
109
        $this->pdf->useTemplate($this->pdf->importPage($this->page));
110
111
        return $this;
112
    }
113
}
114