Repository navigation
Expand file tree
/
Copy pathTcpdfHeaderFooterTest.php
More file actions
123 lines (107 loc) · 4.58 KB
/
Copy pathTcpdfHeaderFooterTest.php
File metadata and controls
123 lines (107 loc) · 4.58 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
<?php
declare(strict_types=1);
/**
* Header/footer family tests (deferred page decoration).
*
* @package com.tecnick.tcpdf
*/
require_once __DIR__ . '/TcpdfTestCase.php';
class TcpdfHeaderFooterTest extends TcpdfTestCase
{
public function testHeaderDataState(): void
{
$pdf = $this->newPdf();
$pdf->setHeaderData('', 0, 'My Title', 'My Header String', [0, 64, 255], [0, 64, 128]);
$data = $pdf->getHeaderData();
$this->assertSame('My Title', $data['title']);
$this->assertSame('My Header String', $data['string']);
$pdf->setHeaderMargin(7);
$this->assertSame(7.0, $pdf->getHeaderMargin());
$pdf->setFooterMargin(9);
$this->assertSame(9.0, $pdf->getFooterMargin());
$pdf->setHeaderFont(['times', 'B', 11]);
$this->assertSame(['times', 'B', 11.0], $pdf->getHeaderFont());
$pdf->setFooterFont(['courier', '', 8]);
$this->assertSame(['courier', '', 8.0], $pdf->getFooterFont());
}
public function testDefaultHeaderAndFooterAreRendered(): void
{
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->setHeaderData('', 0, 'Header Title 42', 'Header line one', [0, 0, 0], [0, 0, 0]);
$pdf->setHeaderFont(['helvetica', '', 10]);
$pdf->setFooterFont(['helvetica', '', 8]);
$pdf->setMargins(15, 27, 15);
$pdf->setFont('helvetica', '', 12);
$pdf->AddPage();
$pdf->Cell(0, 0, 'page one body', 0, 1);
$pdf->AddPage();
$pdf->Cell(0, 0, 'page two body', 0, 1);
$text = $this->extractText($pdf);
$this->assertStringContainsString('Header Title 42', $text);
$this->assertStringContainsString('Header line one', $text);
// pdftotext does not always preserve the spaces around the slash.
$compact = str_replace(' ', '', $text);
$this->assertStringContainsString('1/2', $compact, 'default footer must print real page numbers');
$this->assertStringContainsString('2/2', $compact);
}
public function testHeaderAndFooterCanBeDisabled(): void
{
$pdf = $this->newPdf();
$pdf->setHeaderData('', 0, 'Hidden Header', '', [0, 0, 0], [0, 0, 0]);
$pdf->setFont('helvetica', '', 12);
$pdf->AddPage();
$pdf->Cell(0, 0, 'body only', 0, 1);
$text = $this->extractText($pdf);
$this->assertStringContainsString('body only', $text);
$this->assertStringNotContainsString('Hidden Header', $text);
$this->assertStringNotContainsString('1 / 1', $text);
}
public function testCustomHeaderSubclassIsInvoked(): void
{
$pdf = new class('P', 'mm', 'A4', true, 'UTF-8', false) extends TCPDF {
public function Header()
{
$this->setFont('helvetica', 'B', 9);
$this->Cell(0, 6, 'CUSTOM-HEADER-MARK', 0, 1, 'C');
}
public function Footer()
{
$this->setFont('helvetica', '', 8);
$this->Cell(0, 6, 'CUSTOM-FOOTER-' . $this->PageNo(), 0, 0, 'C');
}
};
$pdf->setFont('helvetica', '', 12);
$pdf->AddPage();
$pdf->Cell(0, 0, 'subclass body', 0, 1);
$text = $this->extractText($pdf);
$this->assertStringContainsString('CUSTOM-HEADER-MARK', $text);
$this->assertStringContainsString('CUSTOM-FOOTER-1', $text);
}
public function testHtmlFooterBelowBottomMarginStaysOnItsPage(): void
{
$pdf = new class('P', 'mm', 'A4', true, 'UTF-8', false) extends TCPDF {
public function Footer()
{
$this->writeHTML('HTMLFOOTER' . $this->PageNo() . ' <em>test</em>');
}
};
$pdf->setPrintHeader(false);
$pdf->setMargins(15, 28, 15);
$pdf->setFooterMargin(10);
$pdf->setAutoPageBreak(true, 25);
$pdf->setFont('helvetica', '', 10);
for ($page = 1; $page <= 3; $page++) {
$pdf->AddPage();
$pdf->writeHTML('<p>BODY' . $page . '</p>');
}
$this->assertMatchesRegularExpression('/^Pages:\s+3$/m', $this->pdfInfo($pdf));
for ($page = 1; $page <= 3; $page++) {
$text = $this->popplerTool('pdftotext', $pdf, '-f ' . $page . ' -l ' . $page . ' -');
$this->assertStringContainsString('BODY' . $page, $text);
$this->assertStringContainsString('HTMLFOOTER' . $page, $text);
}
// A4 height (841.89 pt) minus the 10 mm footer margin (28.35 pt).
$box = $this->wordBox($pdf, 'HTMLFOOTER1');
$this->assertEqualsWithDelta(813.54, $box['ymin'], 3.0);
}
}