-
Notifications
You must be signed in to change notification settings - Fork 63
/
readme.php
288 lines (251 loc) · 9.78 KB
/
readme.php
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
//===================================================================================================
// this is the php file which creates the readme.pdf file, this is not seriously
// suggested as a good way to create such a file, nor a great example of prose,
// but hopefully it will be useful
//
// adding ?d=1 to the url calling this will cause the pdf code itself to be echoed to the
// browser, this is quite useful for debugging purposes.
// there is no option to save directly to a file here, but this would be trivial to implement.
//
// note that this file comprises both the demo code, and the generator of the pdf documentation
//
//===================================================================================================
// don't want any warnings turning up in the pdf code if the server is set to 'anal' mode.
date_default_timezone_set('UTC');
include './src/Cezpdf.php';
// define a class extension to allow the use of a callback to get the table of contents, and to put the dots in the toc
class Creport extends Cezpdf
{
public $reportContents = [];
public function __construct($p, $o, $t, $op)
{
parent::__construct($p, $o, $t, $op);
}
public function rf($info)
{
// this callback records all of the table of contents entries, it also places a destination marker there
// so that it can be linked too
$tmp = $info['p'];
$lvl = $tmp[0];
$lbl = rawurldecode(substr($tmp, 1));
$num = $this->ezWhatPageNumber($this->ezGetCurrentPageNumber());
$this->reportContents[] = [$lbl, $num, $lvl];
$this->addDestination('toc' . (count($this->reportContents) - 1), 'XYZ', 0, $info['y'] + $info['height'], 0);
}
public function dots($info)
{
// draw a dotted line over to the right and put on a page number
$tmp = $info['p'];
$lvl = $tmp[0];
$lbl = substr($tmp, 1);
$xpos = 520;
switch ($lvl) {
case '1':
$size = 16;
$thick = 1;
break;
case '2':
$size = 12;
$thick = 0.5;
break;
}
$this->saveState();
$this->setLineStyle($thick, 'round', '', [0, 2]);
$this->line($xpos, $info['y'], $info['x'] + 5, $info['y']);
$this->restoreState();
$this->addText($xpos + 5, $info['y'], $size, $lbl);
}
}
// I am in NZ, so will design my page for A4 paper.. but don't get me started on that.
// (defaults to legal)
// this code has been modified to use ezpdf.
$project_url = "https://github.com/rospdf/";
$project_version = "Version 0.12.67";
$pdf = new Creport('a4', 'portrait', 'none', null);
$start = microtime(true);
// IMPORTANT: To allow custom callbacks being executed
$pdf->allowedTags .= '|uline|rf:?.*?|dots:[0-9]+';
$pdf->ezSetMargins(50, 70, 50, 50);
// put a line top and bottom on all the pages
$all = $pdf->openObject();
$pdf->saveState();
$pdf->setStrokeColor(0, 0, 0, 1);
$pdf->line(20, 40, 578, 40);
$pdf->line(20, 822, 578, 822);
$pdf->addText(20, 30, 8, $project_url);
$w = $pdf->getTextWidth(8, $project_version);
$pdf->addText(578 - intval($w), 30, 8, $project_version);
$pdf->restoreState();
$pdf->closeObject();
// note that object can be told to appear on just odd or even pages by changing 'all' to 'odd'
// or 'even'.
$pdf->addObject($all, 'all');
$pdf->ezSetDy(-150);
$mainFont = 'Helvetica';
$codeFont = './src/fonts/Courier.afm';
// select a font
$pdf->selectFont($mainFont);
$pdf->ezText("PHP Pdf Class\n", 30, ['justification' => 'centre']);
$pdf->ezText("Native PDF document creation with PHP\n", 20, ['justification' => 'centre']);
$pdf->ezText("released under the terms of the MIT license\n\n<c:alink:https://github.com/rospdf/pdf-php/graphs/contributors>Contributors</c:alink>\n", 14, ['justification' => 'centre']);
$pdf->ezText($project_version, 12, ['justification' => 'centre']);
$pdf->ezText('php ' . phpversion(), 12, ['justification' => 'centre']);
$pdf->ezSetDy(-150);
// modified to use the local file if it can
$pdf->ezText("FORK ON GITHUB.COM", 12, ['justification' => 'right']);
$pdf->openHere('Fit');
function ros_logo(&$pdf, $x, $y, $height, $wl = 0, $wr = 0)
{
global $project_url;
$pdf->saveState();
$h = 100;
$factor = $height / $h;
$pdf->selectFont('Helvetica-Bold');
$text = 'R&OS';
$ts = 100 * $factor;
$th = $pdf->getFontHeight($ts);
$td = $pdf->getFontDescender($ts);
$tw = $pdf->getTextWidth($ts, $text);
$pdf->setColor(0.6, 0, 0);
$z = 0.86;
$pdf->filledRectangle($x - $wl, $y - $z * $h * $factor, $tw * 1.2 + $wr + $wl, $h * $factor * $z);
$pdf->setColor(1, 1, 1);
$pdf->addText($x, $y - $th - $td, $ts, $text);
$pdf->setColor(0.6, 0, 0);
$pdf->addText($x, $y - $th - $td, $ts * 0.1, $project_url);
$pdf->restoreState();
return $height;
}
ros_logo($pdf, 100, $pdf->y - 80, 80, 180, 300);
$pdf->selectFont($mainFont);
if (file_exists('ros.jpg')) {
$pdf->addJpegFromFile('ros.jpg', 199, 650, 200, 0);
}
if (file_exists('github.jpg')) {
$pdf->ezSetDy(-30);
$pdf->addJpegFromFile('github.jpg', 330, $pdf->y);
$pdf->addLink($project_url . 'pdf-php', 330, $pdf->y, 394, $pdf->y + 64);
}
//-----------------------------------------------------------
// load up the document content
$data = file('./data.txt');
$pdf->ezNewPage();
$pdf->ezStartPageNumbers(intval($pdf->ez['pageWidth'] / 2), 28, 10, 'center');
$size = 12;
$height = $pdf->getFontHeight($size);
$textOptions = ['justification' => 'full'];
$collecting = 0;
$code = '';
foreach ($data as $line) {
// go through each line, showing it as required, if it is surrounded by '<>' then
// assume that it is a title
$line = chop($line);
if (strlen($line) && $line[0] == '#') {
// comment, or new page request
switch ($line) {
case '#NP':
$pdf->ezNewPage();
break;
case '#C':
$pdf->selectFont($codeFont);
$textOptions = ['justification' => 'left', 'left' => 20, 'right' => 20];
$size = 10;
break;
case '#c':
$pdf->selectFont($mainFont);
$textOptions = ['justification' => 'full'];
$size = 12;
break;
case '#X':
$collecting = 1;
break;
case '#x':
$pdf->saveState();
eval($code);
$pdf->restoreState();
$pdf->selectFont($mainFont);
$code = '';
$collecting = 0;
break;
}
} else if ($collecting) {
$code .= $line;
} else if (((strlen($line) > 1 && $line[1] == '<'))) {
// then this is a title
$tmp = substr($line, 2, strpos($line, '>') - 2);
$tmp2 = $tmp . '<C:rf:' . $line[0] . '' . rawurlencode($tmp) . '>';
$tmp3 = substr($line, strpos($line, '>') + 1);
switch ($line[0]) {
case '1':
$pdf->saveState();
$pdf->setColor(0.5, 0.5, 0.5);
$pdf->ezText("# " . $tmp2 . " #", 26, ['justification' => 'centre']);
$pdf->restoreState();
break;
case '2':
// add a grey bar, highlighting the change
$pdf->transaction('start');
$ok = 0;
while (!$ok) {
$thisPageNum = $pdf->ezPageCount;
$pdf->saveState();
$pdf->setColor(0.5, 0.5, 0.5);
//$pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y-$pdf->getFontHeight(18)+$pdf->getFontDescender(18),$pdf->ez['pageWidth']-$pdf->ez['leftMargin']-$pdf->ez['rightMargin'],$pdf->getFontHeight(18));
$pdf->ezText("# " . $tmp2, 18);
$w = $pdf->getTextWidth(18, "# " . $tmp2);
$pdf->y = $pdf->y + 15;
$pdf->ezText($tmp3, 12, ['left' => $w]);
$pdf->restoreState();
if ($pdf->ezPageCount == $thisPageNum) {
$pdf->transaction('commit');
$ok = 1;
} else {
// then we have moved onto a new page, bad bad, as the background colour will be on the old one
$pdf->transaction('rewind');
$pdf->ezNewPage();
}
}
break;
case '3':
$pdf->saveState();
$pdf->setColor(0.5, 0.5, 0.5);
$pdf->ezText("" . $tmp2, 12, ['justification' => 'left']);
$pdf->restoreState();
break;
}
} else {
// then this is just text
// the ezpdf function will take care of all of the wrapping etc.
$pdf->ezText($line, $size, $textOptions);
}
}
$pdf->ezStopPageNumbers(1, 1);
// now add the table of contents, including internal links
$pdf->ezInsertMode(1, 1, 'after');
$pdf->ezNewPage();
$pdf->saveState();
$pdf->setColor(0.5, 0.5, 0.5);
$pdf->ezText("Table of Contents\n", 26, ['justification' => 'centre']);
$xpos = 520;
$contents = $pdf->reportContents;
foreach ($contents as $k => $v) {
switch ($v[2]) {
case '1':
$y = $pdf->ezText('<c:ilink:toc' . $k . '>' . $v[0] . '</c:ilink><C:dots:1' . $v[1] . '>', 16, ['aright' => $xpos]);
break;
case '2':
$pdf->ezText('<c:ilink:toc' . $k . '>' . $v[0] . '</c:ilink><C:dots:2' . $v[1] . '>', 12, ['left' => 50, 'aright' => $xpos]);
break;
}
}
$pdf->restoreState();
if (isset($_GET['d']) && $_GET['d']) {
echo "<pre>";
echo $pdf->ezOutput(true);
echo "</pre>";
} else {
$pdf->ezStream(['Content-Disposition' => 'readme.pdf']);
}
$end = microtime(true) - $start;
error_log($end);