-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultipage.php
More file actions
189 lines (159 loc) · 5.29 KB
/
Copy pathmultipage.php
File metadata and controls
189 lines (159 loc) · 5.29 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
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use TinyPdf\TinyPdf;
use TinyPdf\TextOptions;
use TinyPdf\TextAlign;
echo "Creating multi-page PDF document...\n";
$pdf = TinyPdf::create();
// Page 1 - Title Page
$pdf->page(function ($ctx) {
// Border
$ctx->rect(30, 30, 552, 732, '#ffffff');
$ctx->line(30, 30, 582, 30, '#2c3e50', 2);
$ctx->line(30, 762, 582, 762, '#2c3e50', 2);
$ctx->line(30, 30, 30, 762, '#2c3e50', 2);
$ctx->line(582, 30, 582, 762, '#2c3e50', 2);
// Title
$ctx->text(
'Multi-Page Document',
40,
450,
36,
new TextOptions(align: TextAlign::CENTER, width: 532, color: '#2c3e50')
);
// Subtitle
$ctx->text(
'Demonstrating Multi-Page PDF Generation',
40,
410,
16,
new TextOptions(align: TextAlign::CENTER, width: 532, color: '#7f8c8d')
);
// Decorative line
$ctx->rect(206, 380, 200, 3, '#3498db');
// Footer
$ctx->text('Page 1', 40, 50, 10, new TextOptions(align: TextAlign::CENTER, width: 532, color: '#95a5a6'));
});
// Page 2 - Content Page
$pdf->page(function ($ctx) {
// Header
$ctx->rect(40, 720, 532, 40, '#3498db');
$ctx->text('Chapter 1: Introduction', 55, 737, 20, new TextOptions(color: '#ffffff'));
// Content
$y = 670;
$content = [
'Overview',
'This document demonstrates the multi-page capabilities of TinyPDF-PHP.',
'',
'Features',
'• Create multiple pages in a single document',
'• Each page can have unique content and layout',
'• Consistent styling across pages',
'• Page numbering and headers',
'',
'Usage',
'Simply call the page() method multiple times to add more pages.',
'Each page callback receives a context object for drawing operations.',
];
foreach ($content as $line) {
if ($line === '') {
$y -= 15;
continue;
}
if (!str_starts_with($line, '•')) {
$size = strlen($line) > 30 ? 12.0 : 14.0;
$color = strlen($line) > 30 ? '#34495e' : '#2c3e50';
} else {
$size = 11.0;
$color = '#34495e';
}
$ctx->text($line, 55, $y, $size, new TextOptions(color: $color));
$y -= 20;
}
// Footer with border
$ctx->line(40, 60, 572, 60, '#e0e0e0', 1);
$ctx->text('Page 2', 55, 45, 10, new TextOptions(color: '#95a5a6'));
$ctx->text('TinyPDF-PHP', 55, 45, 10, new TextOptions(align: TextAlign::RIGHT, width: 507, color: '#95a5a6'));
});
// Page 3 - Visual Page
$pdf->page(function ($ctx) {
// Header
$ctx->rect(40, 720, 532, 40, '#3498db');
$ctx->text('Chapter 2: Visual Elements', 55, 737, 20, new TextOptions(color: '#ffffff'));
// Grid of colored squares
$colors = ['#e74c3c', '#3498db', '#2ecc71', '#f39c12', '#9b59b6', '#1abc9c'];
$startY = 640;
$startX = 55;
for ($i = 0; $i < 6; $i++) {
$x = $startX + ($i % 3) * 170;
$y = $startY - (intdiv($i, 3)) * 120;
// Square
$ctx->rect($x, $y - 80, 150, 80, $colors[$i]);
// Label
$ctx->text(
'Color ' . ($i + 1),
$x,
$y - 100,
12,
new TextOptions(align: TextAlign::CENTER, width: 150, color: '#2c3e50')
);
}
// Footer
$ctx->line(40, 60, 572, 60, '#e0e0e0', 1);
$ctx->text('Page 3', 55, 45, 10, new TextOptions(color: '#95a5a6'));
$ctx->text('TinyPDF-PHP', 55, 45, 10, new TextOptions(align: TextAlign::RIGHT, width: 507, color: '#95a5a6'));
});
// Page 4 - Summary Page
$pdf->page(function ($ctx) {
// Header
$ctx->rect(40, 720, 532, 40, '#2c3e50');
$ctx->text('Summary', 55, 737, 20, new TextOptions(color: '#ffffff'));
// Summary content
$ctx->text('Document Summary', 55, 650, 18, new TextOptions(color: '#2c3e50'));
$ctx->rect(55, 635, 150, 2, '#3498db');
$summaryItems = [
'Total Pages: 4',
'Chapters: 2',
'Visual Elements: 6 colored squares',
'Text Alignment: Left, Center, Right',
'Colors Used: 8 different colors',
];
$y = 600;
foreach ($summaryItems as $item) {
$ctx->text('• ' . $item, 70, $y, 12, new TextOptions(color: '#34495e'));
$y -= 25;
}
// Conclusion box
$ctx->rect(55, 380, 502, 100, '#ecf0f1');
$ctx->text(
'Conclusion',
70,
455,
14,
new TextOptions(color: '#2c3e50')
);
$ctx->text(
'This multi-page document demonstrates the flexibility and power',
70,
430,
11,
new TextOptions(color: '#34495e')
);
$ctx->text(
'of TinyPDF-PHP for creating professional PDF documents.',
70,
415,
11,
new TextOptions(color: '#34495e')
);
// Footer
$ctx->line(40, 60, 572, 60, '#e0e0e0', 1);
$ctx->text('Page 4', 55, 45, 10, new TextOptions(color: '#95a5a6'));
$ctx->text('TinyPDF-PHP', 55, 45, 10, new TextOptions(align: TextAlign::RIGHT, width: 507, color: '#95a5a6'));
});
$pdfContent = $pdf->build();
file_put_contents(__DIR__ . '/output.pdf', $pdfContent);
echo "Multi-page PDF created: examples/multipage/output.pdf\n";
echo "File size: " . strlen($pdfContent) . " bytes\n";
echo "Total pages: 4\n";