-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathTcpdfPageBreakTest.php
More file actions
247 lines (203 loc) · 8.64 KB
/
Copy pathTcpdfPageBreakTest.php
File metadata and controls
247 lines (203 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
declare(strict_types=1);
/**
* Automatic page break tests for the flowing cursor methods.
*
* @package com.tecnick.tcpdf
*/
require_once __DIR__ . '/TcpdfTestCase.php';
class TcpdfPageBreakTest extends TcpdfTestCase
{
private const IMAGE = __DIR__ . '/../examples/images/logo_example.png';
/**
* Create a LETTER document with 20mm margins and an 11pt font.
*/
private function newFlowPdf(): TCPDF
{
$pdf = $this->newPdf('P', 'mm', 'LETTER');
$pdf->setMargins(20, 20, 20);
$pdf->setAutoPageBreak(true, 20);
$pdf->AddPage();
$pdf->setFont('helvetica', '', 11);
return $pdf;
}
/**
* Row pitch of a Write() call with the given height parameter.
*/
private function rowPitch(TCPDF $pdf, float $height): float
{
return max($height, $pdf->getCellHeight($pdf->getFontSize()));
}
public function testWriteBreaksPageAndKeepsAllText(): void
{
$pdf = $this->newFlowPdf();
$total = 80;
for ($idx = 1; $idx <= $total; ++$idx) {
$pdf->Write(5, 'Line ' . $idx . ' of ordinary body text.' . "\n");
}
$this->assertSame(2, $pdf->getPage());
$this->assertLessThanOrEqual($pdf->getPageHeight() - 20, $pdf->GetY(), 'the cursor must stay on the page');
$text = $this->extractText($pdf);
for ($idx = 1; $idx <= $total; ++$idx) {
$this->assertStringContainsString('Line ' . $idx . ' of', $text, 'line ' . $idx . ' must be rendered');
}
}
public function testWriteCursorFollowsTheBreak(): void
{
$pdf = $this->newFlowPdf();
$pitch = $this->rowPitch($pdf, 5);
$pdf->setY($pdf->getPageHeight() - 20 - ($pitch / 2));
$pdf->Write(5, 'broken' . "\n");
$this->assertSame(2, $pdf->getPage());
$this->assertEqualsWithDelta(20 + $pitch, $pdf->GetY(), 0.001, 'the cursor must restart below the top margin');
$this->assertSame(20.0, $pdf->GetX(), 'ln must reset X to the left margin');
}
public function testWriteFlowingAcrossPagesKeepsCursorOnTheLastPage(): void
{
$pdf = $this->newFlowPdf();
$pitch = $this->rowPitch($pdf, 5);
$perpage = (int) floor(($pdf->getPageHeight() - 40) / $pitch);
$total = (2 * $perpage) + 36;
$txt = '';
for ($idx = 1; $idx <= $total; ++$idx) {
$txt .= 'Line ' . $idx . ' of ordinary body text.' . "\n";
}
$pdf->Write(5, $txt);
$this->assertSame(3, $pdf->getPage(), 'the flow must end on the third page');
$this->assertEqualsWithDelta(20 + (36 * $pitch), $pdf->GetY(), 0.001, 'the cursor must follow the text');
$text = $this->extractText($pdf);
$this->assertStringContainsString('Line 1 of', $text);
$this->assertStringContainsString('Line ' . $total . ' of', $text);
}
public function testWriteKeepsWritingWhenAutoPageBreakIsDisabled(): void
{
$pdf = $this->newFlowPdf();
$pdf->setAutoPageBreak(false, 20);
$pdf->setY(240);
$total = 8;
for ($idx = 1; $idx <= $total; ++$idx) {
$pdf->Write(5, 'Line ' . $idx . ' of ordinary body text.' . "\n");
}
$this->assertSame(1, $pdf->getPage(), 'no page must be added when the break is disabled');
// Legacy keeps writing past the bottom margin instead of dropping the
// overflowing lines.
$text = $this->extractText($pdf);
for ($idx = 1; $idx <= $total; ++$idx) {
$this->assertStringContainsString('Line ' . $idx . ' of', $text, 'line ' . $idx . ' must be rendered');
}
}
public function testColumnMarginsFollowTheSelectedColumn(): void
{
$pdf = $this->newFlowPdf();
$pdf->setEqualColumns(3, 55);
$pdf->selectColumn(0);
$first = $pdf->getMargins();
$this->assertIsArray($first);
/** @var array{left: float, right: float} $first */
$this->assertEqualsWithDelta(20.0, $first['left'], 0.001);
$this->assertEqualsWithDelta($pdf->getPageWidth() - 75.0, $first['right'], 0.001);
$pdf->selectColumn(1);
$second = $pdf->getMargins();
$this->assertIsArray($second);
/** @var array{left: float, right: float} $second */
$this->assertEqualsWithDelta($pdf->GetX(), $second['left'], 0.001, 'the column edge is the left margin');
$this->assertEqualsWithDelta(
$pdf->getPageWidth() - $second['left'] - 55.0,
$second['right'],
0.001,
'the column width is the writable width',
);
// A cell with ln = 1 returns to the column edge, not to the page one.
$pdf->Cell(50, 5, 'in the second column', 0, 1);
$this->assertEqualsWithDelta($second['left'], $pdf->GetX(), 0.001);
$pdf->resetColumns();
$reset = $pdf->getMargins();
$this->assertIsArray($reset);
/** @var array{left: float, right: float} $reset */
$this->assertEqualsWithDelta(20.0, $reset['left'], 0.001);
$this->assertEqualsWithDelta(20.0, $reset['right'], 0.001);
}
public function testWriteFillsTheColumnsBeforeAddingAPage(): void
{
$pdf = $this->newFlowPdf();
$pdf->setEqualColumns(3, 55);
$pdf->selectColumn(0);
$total = 120;
for ($idx = 1; $idx <= $total; ++$idx) {
$pdf->Write(5, 'L' . $idx . ' text.' . "\n");
}
$this->assertSame(1, $pdf->getPage(), 'three columns must hold the text of a single page');
$this->assertSame(2, $pdf->getColumn(), 'the flow must end in the last column');
$text = $this->extractText($pdf);
for ($idx = 1; $idx <= $total; ++$idx) {
$this->assertStringContainsString('L' . $idx . ' text.', $text, 'line ' . $idx . ' must be rendered');
}
}
public function testCellFillsTheColumnsBeforeAddingAPage(): void
{
$pdf = $this->newFlowPdf();
$pdf->setEqualColumns(3, 55);
$pdf->selectColumn(0);
$total = 120;
for ($idx = 1; $idx <= $total; ++$idx) {
$pdf->Cell(50, 5, 'L' . $idx . ' text.', 0, 1);
}
$this->assertSame(1, $pdf->getPage(), 'three columns must hold the text of a single page');
$this->assertSame(2, $pdf->getColumn(), 'the flow must end in the last column');
$text = $this->extractText($pdf);
$this->assertStringContainsString('L1 text.', $text);
$this->assertStringContainsString('L' . $total . ' text.', $text);
}
public function testColumnsRestartOnTheNextPage(): void
{
$pdf = $this->newFlowPdf();
$pdf->setEqualColumns(3, 55);
$pdf->selectColumn(0);
for ($idx = 1; $idx <= 300; ++$idx) {
$pdf->Write(5, 'L' . $idx . ' text.' . "\n");
}
$this->assertSame(3, $pdf->getPage());
$this->assertSame(0, $pdf->getColumn(), 'a new page must restart in the first column');
$text = $this->extractText($pdf);
$this->assertStringContainsString('L300 text.', $text);
}
public function testAcceptPageBreakOverrideBlocksTheBreak(): void
{
$pdf = new class('P', 'mm', 'LETTER', true, 'UTF-8', false) extends TCPDF {
public int $calls = 0;
public function AcceptPageBreak()
{
++$this->calls;
return false;
}
};
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->setMargins(20, 20, 20);
$pdf->setAutoPageBreak(true, 20);
$pdf->AddPage();
$pdf->setFont('helvetica', '', 11);
for ($idx = 1; $idx <= 60; ++$idx) {
$pdf->Cell(0, 5, 'line ' . $idx, 0, 1);
}
$this->assertSame(1, $pdf->getPage(), 'the override must prevent the automatic break');
$this->assertGreaterThan(0, $pdf->calls, 'the override must be consulted');
$this->assertLessThan(60, $pdf->calls, 'the override must only be consulted for content that does not fit');
}
public function testImageFollowsTheCursorAcrossAnAutomaticBreak(): void
{
$pdf = $this->newFlowPdf();
$pdf->setY(230);
$pdf->Image(self::IMAGE, null, null, 50, 50);
$this->assertSame(2, $pdf->getPage());
$this->assertEqualsWithDelta(70.0, $pdf->getImageRBY(), 0.001, 'the image must start at the top margin');
}
public function testImageWithExplicitYIsNotMoved(): void
{
$pdf = $this->newFlowPdf();
$pdf->setY(230);
$pdf->Image(self::IMAGE, 20, 230, 50, 50);
$this->assertSame(1, $pdf->getPage());
$this->assertEqualsWithDelta(280.0, $pdf->getImageRBY(), 0.001, 'an explicit ordinate must be preserved');
}
}