composer package updates
[openemr.git] / vendor / rospdf / pdf-php / readme.php
blob1630ac58f024bb52567bffd470a0772b8383c328
1 <?php
2 //===================================================================================================
3 // this is the php file which creates the readme.pdf file, this is not seriously
4 // suggested as a good way to create such a file, nor a great example of prose,
5 // but hopefully it will be useful
6 //
7 // adding ?d=1 to the url calling this will cause the pdf code itself to ve echoed to the
8 // browser, this is quite useful for debugging purposes.
9 // there is no option to save directly to a file here, but this would be trivial to implement.
11 // note that this file comprisises both the demo code, and the generator of the pdf documentation
13 //===================================================================================================
16 // don't want any warnings turning up in the pdf code if the server is set to 'anal' mode.
17 //error_reporting(7);
18 error_reporting(E_ALL);
19 date_default_timezone_set('UTC');
21 include './src/Cezpdf.php';
23 // define a clas extension to allow the use of a callback to get the table of contents, and to put the dots in the toc
24 class Creport extends Cezpdf {
25 var $reportContents = array();
27 public function __construct($p,$o,$t,$op){
28 parent::__construct($p,$o,$t,$op);
31 function rf($info){
32 // this callback records all of the table of contents entries, it also places a destination marker there
33 // so that it can be linked too
34 $tmp = $info['p'];
35 $lvl = $tmp[0];
36 $lbl = rawurldecode(substr($tmp,1));
37 $num=$this->ezWhatPageNumber($this->ezGetCurrentPageNumber());
38 $this->reportContents[] = array($lbl,$num,$lvl );
39 $this->addDestination('toc'.(count($this->reportContents)-1),'Fit',$info['y']+$info['height']);
42 function dots($info){
43 // draw a dotted line over to the right and put on a page number
44 $tmp = $info['p'];
45 $lvl = $tmp[0];
46 $lbl = substr($tmp,1);
47 $xpos = 520;
49 switch($lvl){
50 case '1':
51 $size=16;
52 $thick=1;
53 break;
54 case '2':
55 $size=12;
56 $thick=0.5;
57 break;
60 $this->saveState();
61 $this->setLineStyle($thick,'round','',[0,2]);
62 $this->line($xpos,$info['y'],$info['x']+5,$info['y']);
63 $this->restoreState();
64 $this->addText($xpos+5,$info['y'],$size,$lbl);
67 // I am in NZ, so will design my page for A4 paper.. but don't get me started on that.
68 // (defaults to legal)
69 // this code has been modified to use ezpdf.
71 $project_url = "https://github.com/rospdf/";
72 $project_version = "0.12.46";
74 $pdf = new Creport('a4','portrait', 'none', null);
76 $start = microtime(true);
78 // IMPORTANT: To allow custom callbacks being executed
79 $pdf->allowedTags .= '|uline|rf:?.*?|dots:[0-9]+';
81 $pdf->ezSetMargins(50,70,50,50);
83 // put a line top and bottom on all the pages
84 $all = $pdf->openObject();
85 $pdf->saveState();
86 $pdf->setStrokeColor(0,0,0,1);
87 $pdf->line(20,40,578,40);
88 $pdf->line(20,822,578,822);
89 $pdf->addText(20,30,8,$project_url);
90 $pdf->addText(515,30,8,'Version ' . $project_version);
91 $pdf->restoreState();
92 $pdf->closeObject();
93 // note that object can be told to appear on just odd or even pages by changing 'all' to 'odd'
94 // or 'even'.
95 $pdf->addObject($all,'all');
97 $pdf->ezSetDy(-150);
99 $mainFont = 'Helvetica';
100 $codeFont = './src/fonts/Courier.afm';
101 // select a font
102 $pdf->selectFont($mainFont);
104 $pdf->ezText("PHP Pdf Class\n",30,array('justification'=>'centre'));
105 $pdf->ezText("Native PDF document creation with PHP\n",20,array('justification'=>'centre'));
106 $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,array('justification'=>'centre'));
107 $pdf->ezText("Version $project_version", 12, array('justification' => 'centre'));
108 $pdf->ezSetDy(-150);
109 // modified to use the local file if it can
110 $pdf->ezText("FORK ON GITHUB.COM",12,array('justification'=>'right'));
112 $pdf->openHere('Fit');
114 function ros_logo(&$pdf,$x,$y,$height,$wl=0,$wr=0){
115 global $project_url;
116 $pdf->saveState();
117 $h=100;
118 $factor = $height/$h;
119 $pdf->selectFont('Helvetica-Bold');
120 $text = 'R&OS';
121 $ts=100*$factor;
122 $th = $pdf->getFontHeight($ts);
123 $td = $pdf->getFontDescender($ts);
124 $tw = $pdf->getTextWidth($ts,$text);
125 $pdf->setColor(0.6,0,0);
126 $z = 0.86;
127 $pdf->filledRectangle($x-$wl,$y-$z*$h*$factor,$tw*1.2+$wr+$wl,$h*$factor*$z);
128 $pdf->setColor(1,1,1);
129 $pdf->addText($x,$y-$th-$td,$ts,$text);
130 $pdf->setColor(0.6,0,0);
131 $pdf->addText($x,$y-$th-$td,$ts*0.1, $project_url);
132 $pdf->restoreState();
133 return $height;
136 ros_logo($pdf,100,$pdf->y-80,80,180,300);
137 $pdf->selectFont($mainFont);
139 if (file_exists('ros.jpg')){
140 $pdf->addJpegFromFile('ros.jpg',199,650,200,0);
142 if (file_exists('github.jpg')){
143 $pdf->ezSetDy(-30);
144 $pdf->addJpegFromFile('github.jpg',330,$pdf->y);
145 $pdf->addLink($project_url . 'pdf-php',330,$pdf->y,394,$pdf->y + 64);
148 //-----------------------------------------------------------
149 // load up the document content
150 $data=file('./data.txt');
152 $pdf->ezNewPage();
154 $pdf->ezStartPageNumbers(intval($pdf->ez['pageWidth']/2), 28, 10, 'center');
156 $size=12;
157 $height = $pdf->getFontHeight($size);
158 $textOptions = array('justification'=>'full');
159 $collecting=0;
160 $code='';
162 foreach ($data as $line){
163 // go through each line, showing it as required, if it is surrounded by '<>' then
164 // assume that it is a title
165 $line=chop($line);
166 if (strlen($line) && $line[0]=='#'){
167 // comment, or new page request
168 switch($line){
169 case '#NP':
170 $pdf->ezNewPage();
171 break;
172 case '#C':
173 $pdf->selectFont($codeFont);
174 $textOptions = array('justification'=>'left','left'=>20,'right'=>20);
175 $size=10;
176 break;
177 case '#c':
178 $pdf->selectFont($mainFont);
179 $textOptions = array('justification'=>'full');
180 $size=12;
181 break;
182 case '#X':
183 $collecting=1;
184 break;
185 case '#x':
186 $pdf->saveState();
187 eval($code);
188 $pdf->restoreState();
189 $pdf->selectFont($mainFont);
190 $code='';
191 $collecting=0;
192 break;
194 } else if ($collecting){
195 $code.=$line;
196 } else if (((strlen($line)>1 && $line[1]=='<') )) {
197 // then this is a title
198 $tmp = substr($line,2,strpos($line, '>')-2);
199 $tmp2 = $tmp .'<C:rf:'.$line[0].''.rawurlencode($tmp).'>';
200 $tmp3 = substr($line, strpos($line, '>') + 1);
202 switch($line[0]){
203 case '1':
204 $pdf->saveState();
205 $pdf->setColor(0.5,0.5,0.5);
206 $pdf->ezText("# " . $tmp2 . " #" ,26,array('justification'=>'centre'));
207 $pdf->restoreState();
208 break;
209 case '2':
210 // add a grey bar, highlighting the change
211 $pdf->transaction('start');
212 $ok=0;
213 while (!$ok){
214 $thisPageNum = $pdf->ezPageCount;
215 $pdf->saveState();
216 $pdf->setColor(0.5,0.5,0.5);
217 //$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));
218 $pdf->ezText("# " . $tmp2,18);
220 $w = $pdf->getTextWidth(18, "# " . $tmp2);
221 $pdf->y = $pdf->y + 15;
222 $pdf->ezText($tmp3, 12, array('left' => $w));
223 $pdf->restoreState();
225 if ($pdf->ezPageCount==$thisPageNum){
226 $pdf->transaction('commit');
227 $ok=1;
228 } else {
229 // then we have moved onto a new page, bad bad, as the background colour will be on the old one
230 $pdf->transaction('rewind');
231 $pdf->ezNewPage();
234 break;
235 case '3':
236 $pdf->saveState();
237 $pdf->setColor(0.5,0.5,0.5);
238 $pdf->ezText("" . $tmp2,12,array('justification'=>'left'));
239 $pdf->restoreState();
240 break;
242 } else {
243 // then this is just text
244 // the ezpdf function will take care of all of the wrapping etc.
245 $pdf->ezText($line,$size,$textOptions);
250 $pdf->ezStopPageNumbers(1,1);
252 // now add the table of contents, including internal links
253 $pdf->ezInsertMode(1,1,'after');
254 $pdf->ezNewPage();
256 $pdf->saveState();
257 $pdf->setColor(0.5,0.5,0.5);
258 $pdf->ezText("Table of Contents\n",26,array('justification'=>'centre'));
259 $xpos = 520;
260 $contents = $pdf->reportContents;
261 foreach($contents as $k=>$v){
262 switch ($v[2]){
263 case '1':
264 $y=$pdf->ezText('<c:ilink:toc'.$k.'>'.$v[0].'</c:ilink><C:dots:1'.$v[1].'>',16,array('aright'=>$xpos));
265 break;
266 case '2':
267 $pdf->ezText('<c:ilink:toc'.$k.'>'.$v[0].'</c:ilink><C:dots:2'.$v[1].'>',12,array('left'=>50,'aright'=>$xpos));
268 break;
271 $pdf->restoreState();
273 if (isset($_GET['d']) && $_GET['d']){
274 echo "<pre>";
275 echo $pdf->ezOutput(TRUE);
276 echo "</pre>";
277 } else {
278 $pdf->ezStream(['Content-Disposition'=>'readme.pdf']);
281 $end = microtime(true) - $start;
282 error_log($end);