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
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.
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);
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
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']);
43 // draw a dotted line over to the right and put on a page number
46 $lbl = substr($tmp,1);
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();
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);
93 // note that object can be told to appear on just odd or even pages by changing 'all' to 'odd'
95 $pdf->addObject($all,'all');
99 $mainFont = 'Helvetica';
100 $codeFont = './src/fonts/Courier.afm';
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'));
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){
118 $factor = $height/$h;
119 $pdf->selectFont('Helvetica-Bold');
122 $th = $pdf->getFontHeight($ts);
123 $td = $pdf->getFontDescender($ts);
124 $tw = $pdf->getTextWidth($ts,$text);
125 $pdf->setColor(0.6,0,0);
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();
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')){
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');
154 $pdf->ezStartPageNumbers(intval($pdf->ez
['pageWidth']/2), 28, 10, 'center');
157 $height = $pdf->getFontHeight($size);
158 $textOptions = array('justification'=>'full');
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
166 if (strlen($line) && $line[0]=='#'){
167 // comment, or new page request
173 $pdf->selectFont($codeFont);
174 $textOptions = array('justification'=>'left','left'=>20,'right'=>20);
178 $pdf->selectFont($mainFont);
179 $textOptions = array('justification'=>'full');
188 $pdf->restoreState();
189 $pdf->selectFont($mainFont);
194 } else if ($collecting){
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);
205 $pdf->setColor(0.5,0.5,0.5);
206 $pdf->ezText("# " . $tmp2 . " #" ,26,array('justification'=>'centre'));
207 $pdf->restoreState();
210 // add a grey bar, highlighting the change
211 $pdf->transaction('start');
214 $thisPageNum = $pdf->ezPageCount
;
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');
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');
237 $pdf->setColor(0.5,0.5,0.5);
238 $pdf->ezText("" . $tmp2,12,array('justification'=>'left'));
239 $pdf->restoreState();
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');
257 $pdf->setColor(0.5,0.5,0.5);
258 $pdf->ezText("Table of Contents\n",26,array('justification'=>'centre'));
260 $contents = $pdf->reportContents
;
261 foreach($contents as $k=>$v){
264 $y=$pdf->ezText('<c:ilink:toc'.$k.'>'.$v[0].'</c:ilink><C:dots:1'.$v[1].'>',16,array('aright'=>$xpos));
267 $pdf->ezText('<c:ilink:toc'.$k.'>'.$v[0].'</c:ilink><C:dots:2'.$v[1].'>',12,array('left'=>50,'aright'=>$xpos));
271 $pdf->restoreState();
273 if (isset($_GET['d']) && $_GET['d']){
275 echo $pdf->ezOutput(TRUE);
278 $pdf->ezStream(['Content-Disposition'=>'readme.pdf']);
281 $end = microtime(true) - $start;