source: trunk/filemanager/tp/dompdf/www/usage.php @ 2000

Revision 2000, 20.4 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implementação do módulo gerenciador de arquivos

Line 
1<?php include("head.inc"); ?>
2<div id="toc">
3<h2>On this page:</h2>
4<ul>
5<?php echo li_arrow() ?><a href="#usage">Usage</a></li>
6<ul><?php echo li_arrow() ?><a href="#web">Invoking via the web</a></li>
7    <?php echo li_arrow() ?><a href="#cli">Invoking via the command line</a></li>
8    <?php echo li_arrow() ?><a href="#class">Using the dompdf class directly</a></li>
9    <?php echo li_arrow() ?><a href="#method_summary">dompdf class reference</a></li>
10</ul>
11<?php echo li_arrow() ?><a href="#inline">Inline PHP support</a></li>
12</ul>
13</div>
14
15<a name="usage"> </a>
16<h2>Usage</h2>
17
18<p>The dompdf.php script included in the distribution can be used both from
19the <a href="#cli">command line</a> or via a <a href="#web">web browser</a>.
20Alternatively, the dompdf class can be used <a href="#class">directly</a>.</p>
21
22<a name="web"> </a>
23<h3>Invoking dompdf via the web</h3>
24
25<p>The dompdf.php script is not intended to be an interactive page.  It
26receives input parameters via $_GET and can stream a PDF directly to the
27browser.  This makes it possible to embed links to the script in a page that
28look like static PDF links, but are actually dynamically generated.  This
29method is also useful as a redirection target.</p>
30
31<p>dompdf.php accepts the following $_GET variables:</p>
32
33<table>
34<tr><td class="bar1" colspan="3">&nbsp;</td></tr>
35<tr>
36<td class="input">input_file</td>  <td>required</td>
37
38<td class="description">a rawurlencoded() path to the HTML file to process.  Remote files
39(http/ftp) are supported if fopen wrappers are enabled.</td>
40</tr>
41
42<tr>
43<td class="input">paper</td>  <td>optional</td>
44
45<td class="description">the paper size.  Defaults to 'letter' (unless the default has been
46changed in dompdf_config.inc.php).  See include/pdf_adapter.cls.php, or
47invoke dompdf.php on the command line with the -l switch for accepted paper
48sizes.</td>
49</tr>
50
51<tr>
52<td class="input">orientation</td>  <td>optional</td>
53<td class="description">'portrait' or 'landscape'.  Defaults to 'portrait'.</td>
54</tr>
55
56<tr>
57<td class="input">base_path</td>  <td>optional</td>
58
59<td class="description">the base path to use when resolving relative links (images or CSS
60files).  Defaults to the directory containing the file being accessed.
61(This option is useful for pointing dompdf at your CSS files even though the
62HTML file may be elsewhere.)</td>
63</tr>
64
65<tr>
66<td class="input">output_file</td>  <td>optional</td>
67
68<td class="description">the rawurlencoded() name of the output file. Defaults to 'dompdf_out.pdf'.</td>
69</tr>
70
71<tr>
72<td class="input">save_file</td>  <td>optional</td>
73
74<td class="description">If present (i.e. <code>isset($_GET["save_file"]) == true');</code>),
75output_file is saved locally, Otherwise the file is streamed directly to the client.</td>
76</tr>
77
78<tr><td class="bar2" colspan="3">&nbsp;</td></tr>
79</table>
80
81
82<p>One technique for generating dynamic PDFs is to generate dynamic HTML as you
83normally would, except instead of displaying the output to the browser, you
84use output buffering and write the output to a temporary file.  Once this
85file is saved, you redirect to the dompdf.php script.  If you use a
86templating engine like Smarty, you can simply do:</p>
87
88
89<pre>
90&lt;?php
91$tmpfile = tempnam("/tmp", "dompdf_");
92file_put_contents($tmpfile, $smarty->fetch()); // Replace $smarty->fetch()
93                                                // with your HTML string
94
95$url = "dompdf.php?input_file=" . rawurlencode($tmpfile) .
96       "&amp;paper=letter&amp;output_file=" . rawurlencode("My Fancy PDF.pdf");
97
98header("Location: http://" . $_SERVER["HTTP_HOST"] . "/$url");
99?&gt;
100</pre>
101
102<p>If you use any stylesheets, you may need to provide the
103<code>base_path</code> option to tell dompdf where to look for them, as they
104are not likely relative to /tmp ;).</p>
105
106
107<a name="cli"> </a>
108<h3>Invoking dompdf via the command line</h3>
109
110<p>You can execute dompdf.php using the following command:</p>
111
112<pre>$ php -f dompdf.php -- [options]</pre>
113
114<p>(If you find yourself using only the cli interface, you can add
115<code>#!/usr/bin/php</code> as the first line of dompdf.php to invoke dompdf.php
116directly.)</p>
117
118<p>dompdf.php is invoked as follows:</p>
119
120<table>
121<tr><td class="bar1" colspan="2">&nbsp;</td></tr>
122<tr>
123<td colspan="2" class="input">$ ./dompdf.php [options] html_file</td>
124</tr>
125
126<tr>
127<td colspan="2"><code>html_file</code> can be a filename, a url if
128fopen_wrappers are enabled, or the '-' character to read from standard input.</td>
129</tr>
130
131<tr>
132<td class="input">-h</td>
133<td class="description">Show a brief help message</td>
134</tr>
135
136<tr>
137<td class="input">-l</td>
138<td class="description">list available paper sizes</td>
139</tr>
140
141<tr>
142<td class="input">-p size</td>
143<td class="description">paper size; something like 'letter', 'A4', 'legal', etc. Thee default is 'letter'</td>
144</tr>
145
146<tr>
147<td class="input">-o orientation</td>
148<td class="description">either 'portrait' or 'landscape'.  Default is 'portrait'.</td>
149</tr>
150
151<tr>
152<td class="input">-b path</td>
153
154<td class="description">the base path to use when resolving relative links
155(images or CSS files). Default is the directory of html_file.</td>
156</tr>
157
158<tr>
159<td class="input">-f file</td>
160<td class="description">the output filename.  Default is the input <code>[html_file].pdf</code>.</td>
161</tr>
162
163<tr>
164<td class="input">-v</td>
165<td class="description">verbose: display html parsing warnings and file not found errors.</td>
166</tr>
167
168<tr>
169<td class="input">-d</td>
170
171<td class="description">very verbose: display oodles of debugging output;
172every frame in the tree is printed to stdout.</td>
173</tr>
174<tr><td class="bar2" colspan="2">&nbsp;</td></tr>
175
176</table>
177
178<p>Examples:</p>
179
180<pre>
181$ php -f dompdf.php -- my_resume.html
182$ ./dompdf.php -b /var/www/ ./web_stuff/index.html
183$ echo '&lt;html&gt;&lt;body&gt;Hello world!&lt;/body&gt;&lt;/html&gt;' | ./dompdf.php -
184</pre>
185
186
187<a name="class"> </a>
188<h3>Using the dompdf class directly</h3>
189
190<p>Using the dompdf class directly is fairly straightforward:
191<pre>
192&lt;?php
193require_once("dompdf_config.inc.php");
194
195$html =
196  '&lt;html&gt;&lt;body&gt;'.
197  '&lt;p&gt;Put your html here, or generate it with your favourite '.
198  'templating system.&lt;/p&gt;'.
199  '&lt;/body&gt;&lt;/html&gt;';
200
201$dompdf = new DOMPDF();
202$dompdf-&gt;load_html($html);
203$dompdf-&gt;render();
204$dompdf-&gt;stream("sample.pdf");
205
206?&gt;
207</pre></p>
208
209<p>Below is a summary of the methods available in the dompdf class.  For complete details,
210see the <a href="http://www.digitaljunkies.ca/dompdf/doc/">API
211documentation</a> for the class interface definition.</p>
212
213<a name="method_summary"> </a>
214<h3>Method Summary</h3>
215<ul class="method-summary">
216<?php echo li_arrow() ?><span class="method-result">DOMPDF</span> <a href="#method__construct">__construct</a>()</li>
217<?php echo li_arrow() ?><span class="method-result">string</span> <a href="#methodget_base_path">get_base_path</a>()</li>
218<?php echo li_arrow() ?><span class="method-result">string</span> <a href="#methodget_host">get_host</a>()</li>
219<?php echo li_arrow() ?><span class="method-result">string</span> <a href="#methodget_protocol">get_protocol</a>()</li>
220<?php echo li_arrow() ?><span class="method-result">Frame_Tree</span> <a href="#methodget_tree">get_tree</a>()</li>
221<?php echo li_arrow() ?><span class="method-result">void</span> <a href="#methodload_html">load_html</a>(<span class="var-type">string</span> <span class="var-name">$str</span>)</li>
222<?php echo li_arrow() ?><span class="method-result">void</span> <a href="#methodload_html_file">load_html_file</a>(<span class="var-type">string</span> <span class="var-name">$file</span>)</li>
223<?php echo li_arrow() ?><span class="method-result">string</span> <a href="#methodoutput">output</a>()</li>
224<?php echo li_arrow() ?><span class="method-result">void</span> <a href="#methodrender">render</a>()</li>
225<?php echo li_arrow() ?><span class="method-result">void</span> <a href="#methodset_base_path">set_base_path</a>(<span class="var-type">string</span> <span class="var-name">$path</span>)</li>
226<?php echo li_arrow() ?><span class="method-result">void</span> <a href="#methodset_host">set_host</a>(<span class="var-type">string</span> <span class="var-name">$host</span>)</li>
227<?php echo li_arrow() ?><span class="method-result">void</span> <a href="#methodset_paper">set_paper</a>(<span class="var-type">string</span> <span class="var-name">$size</span>, [<span class="var-type">string</span> <span class="var-name">$orientation</span> = "portrait"])</li>
228<?php echo li_arrow() ?><span class="method-result">void</span> <a href="#methodset_protocol">set_protocol</a>(<span class="var-type">string</span> <span class="var-name">$proto</span>)</li>
229<?php echo li_arrow() ?><span class="method-result">void</span> <a href="#methodstream">stream</a>(<span class="var-type">string</span> <span class="var-type">$filename</span>, [<span class="var-type">mixed</span> <span class="var-name">$options</span> = null])</li>
230</ul>
231
232<div class="method-definition" style="background-position: 10px bottom;">
233<a name="method__construct" id="__construct"><!-- --></a>
234<div class="method-header">
235  <span class="method-title">Constructor __construct</span> (line <span class="line-number">163</span>)
236</div>
237<p class="short-description">Class constructor</p> 
238  <div class="method-signature">
239    <span class="method-result">DOMPDF</span>
240    <span class="method-name">__construct</span>()
241  </div>
242</div>
243
244<div class="method-definition"  style="background-position: 302px bottom;">
245<a name="methodget_base_path" id="get_base_path"><!-- --></a>
246<div class="method-header">
247  <span class="method-title">get_base_path</span> (line <span class="line-number">227</span>)
248</div>
249<p class="short-description">Returns the base path</p>
250  <div class="method-signature">
251    <span class="method-result">string</span>
252    <span class="method-name">get_base_path</span>()
253  </div>
254</div>
255
256<div class="method-definition" style="background-position: 710px bottom;">
257<a name="methodget_canvas" id="get_canvas"><!-- --></a>
258<div class="method-header">
259    <span class="method-title">get_canvas</span> (line <span class="line-number">234</span>)
260</div>
261<p class="short-description">Return the underlying Canvas instance (e.g. CPDF_Adapter, GD_Adapter)</p> 
262  <div class="method-signature">
263    <span class="method-result">Canvas</span>
264    <span class="method-name">get_canvas</span>()
265  </div>
266
267</div>
268
269<div class="method-definition"  style="background-position: 252px bottom;">
270<a name="methodget_host" id="get_host"><!-- --></a> 
271  <div class="method-header">
272    <span class="method-title">get_host</span> (line <span class="line-number">220</span>)
273  </div>
274<p class="short-description">Returns the base hostname</p> 
275  <div class="method-signature">
276    <span class="method-result">string</span>
277    <span class="method-name">get_host</span>()
278  </div>
279
280</div>
281
282<div class="method-definition" style="background-position: 498px bottom;">
283<a name="methodget_protocol" id="get_protocol"><!-- --></a>
284<div class="method-header">
285  <span class="method-title">get_protocol</span> (line <span class="line-number">213</span>)
286</div>
287<p class="short-description">Returns the protocol in use</p>
288  <div class="method-signature">
289    <span class="method-result">string</span>
290    <span class="method-name">get_protocol</span>()
291  </div>
292
293</div>
294
295<div class="method-definition" style="background-position: 39px bottom;">
296<a name="methodget_tree" id="get_tree"><!-- --></a>
297<div class="method-header">
298  <span class="method-title">get_tree</span> (line <span class="line-number">182</span>)
299</div>
300<p class="short-description">Returns the underlying Frame_Tree object</p>
301  <div class="method-signature">
302    <span class="method-result">Frame_Tree</span>
303    <span class="method-name">get_tree</span>()
304  </div>
305
306</div>
307
308<div class="method-definition" style="background-position: 653px bottom;">
309<a name="methodload_html" id="load_html"><!-- --></a>
310<div class="method-header">
311  <span class="method-title">load_html</span> (line <span class="line-number">272</span>)
312</div>
313<p class="short-description">Loads an HTML string</p>
314<p class="description"><p>Parse errors are stored in the global array _dompdf_warnings.</p></p>
315  <div class="method-signature">
316    <span class="method-result">void</span>
317    <span class="method-name">load_html</span>(<span class="var-type">string</span>&nbsp;<span class="var-name">$str</span>)
318  </div>
319 
320   <ul class="parameters">
321   <li>
322     <span class="var-type">string</span>
323     <span class="var-name">$str</span><span class="var-description">: HTML text to load</span></li>
324   </ul>
325
326</div>
327
328<div class="method-definition" style="background-position: 479px bottom;">
329<a name="methodload_html_file" id="load_html_file"><!-- --></a>
330<div class="method-header">
331  <span class="method-title">load_html_file</span> (line <span class="line-number">245</span>)
332</div>
333<p class="short-description">Loads an HTML file</p>
334<p class="description"><p>Parse errors are stored in the global array _dompdf_warnings.</p></p>
335  <div class="method-signature">
336    <span class="method-result">void</span>
337    <span class="method-name">load_html_file</span>(<span class="var-type">string</span>&nbsp;<span class="var-name">$file</span>)
338  </div>
339 
340  <ul class="parameters">
341      <li>
342    <span class="var-type">string</span>
343    <span class="var-name">$file</span><span class="var-description">: a filename or url to load</span></li>
344    </ul>
345
346</div>
347
348<div class="method-definition" style="background-position: 182px bottom;">
349<a name="methodoutput" id="output"><!-- --></a>
350<div class="method-header">
351  <span class="method-title">output</span> (line <span class="line-number">451</span>)
352</div>
353<p class="short-description">Returns the PDF as a string</p>
354  <div class="method-signature">
355    <span class="method-result">string</span>
356    <span class="method-name">output</span>()
357  </div>
358
359</div>
360
361<div class="method-definition" style="background-position: 741px bottom;">
362
363<a name="methodrender" id="render"><!-- --></a>
364<div class="method-header">
365  <span class="method-title">render</span> (line <span class="line-number">373</span>)
366</div>
367<p class="short-description">Renders the HTML to PDF</p>
368 
369  <div class="method-signature">
370    <span class="method-result">void</span>
371    <span class="method-name">render</span>()
372  </div>
373</div>
374
375<div class="method-definition" style="background-position: 824px bottom;">
376<a name="methodset_base_path" id="set_base_path"><!-- --></a>
377<div class="method-header">
378  <span class="method-title">set_base_path</span> (line <span class="line-number">206</span>)
379</div>
380<p class="short-description">Sets the base path</p>
381  <div class="method-signature">
382    <span class="method-result">void</span>
383    <span class="method-name">set_base_path</span>(<span class="var-type">string</span>&nbsp;<span class="var-name">$path</span>)
384  </div>
385 
386  <ul class="parameters">
387  <li>
388    <span class="var-type">string</span>
389    <span class="var-name">$path</span></li>
390  </ul>
391
392</div>
393
394<div class="method-definition" style="background-position: 519px bottom;">
395
396<a name="methodset_host" id="set_host"><!-- --></a>
397<div class="method-header">
398  <span class="method-title">set_host</span> (line <span class="line-number">199</span>)
399</div>
400 <p class="short-description">Sets the base hostname</p>
401 
402  <div class="method-signature">
403    <span class="method-result">void</span>
404    <span class="method-name">set_host</span>(<span class="var-type">string</span>&nbsp;<span class="var-name">$host</span>)
405  </div>
406 
407  <ul class="parameters">
408  <li>
409     <span class="var-type">string</span>
410     <span class="var-name">$host</span></li>
411     </ul>
412</div>
413
414<div class="method-definition"  style="background-position: 391px bottom;">
415<a name="methodset_paper" id="set_paper"><!-- --></a>
416<div class="method-header">
417  <span class="method-title">set_paper</span> (line <span class="line-number">353</span>)
418</div>
419<p class="short-description">Sets the paper size &amp; orientation</p>
420 
421  <div class="method-signature">
422    <span class="method-result">void</span>
423    <span class="method-name">set_paper</span>(<span class="var-type">string</span>&nbsp;<span class="var-name">$size</span>, [<span class="var-type">string</span>&nbsp;<span class="var-name">$orientation</span> = <span class="var-default">"portrait"</span>])
424  </div>
425 
426  <ul class="parameters">
427  <li>
428    <span class="var-type">string</span>
429    <span class="var-name">$size</span><span class="var-description">: 'letter', 'legal', 'A4', etc. See CPDF_Adapter::$PAPER_SIZES</span></li>
430  <li>
431    <span class="var-type">string</span>
432    <span class="var-name">$orientation</span><span class="var-description">: 'portrait' or 'landscape'</span></li>
433  </ul>
434</div>
435
436<div class="method-definition" style="background-position: 672px bottom;">
437<a name="methodset_protocol" id="set_protocol"><!-- --></a>
438<div class="method-header">
439  <span class="method-title">set_protocol</span> (line <span class="line-number">192</span>)
440</div>
441<p class="short-description">Sets the protocol to use (http://, file://, ftp:// etc.)</p>
442 
443  <div class="method-signature">
444    <span class="method-result">void</span>
445    <span class="method-name">set_protocol</span>(<span class="var-type">string</span>&nbsp;<span class="var-name">$proto</span>)
446  </div>
447 
448  <ul class="parameters">
449  <li>
450    <span class="var-type">string</span>
451    <span class="var-name">$proto</span></li>
452  </ul>
453</div>
454
455<div class="method-definition" style="background-position: 146px bottom;">
456<a name="methodstream" id="stream"><!-- --></a>
457<div class="method-header">
458  <span class="method-title">stream</span> (line <span class="line-number">441</span>)
459</div>
460<p class="short-description">Streams the PDF to the client</p>
461
462<p class="description">
463
464<p>The file will always open a download dialog.  The options parameter
465controls the output headers.  Accepted headers
466are:<br/><br/>
467
468'Accept-Ranges' =&gt; 1 or 0 - if this is not set to 1, then this
469header is not included, off by default. This header seems to have
470caused some problems despite the fact that it is supposed to solve
471them, so I am leaving it off by default.<br/><br/>
472
473'compress' = &gt; 1 or 0 - apply content stream compression, this is
474on (1) by default<br/><br/>
475
476'Attachment' =&gt; 1 or 0 - if 1, force the browser to open a download
477dialog, on (1) by default</p>
478
479</p>
480 
481  <div class="method-signature">
482    <span class="method-result">void</span>
483    <span class="method-name">stream</span>(<span class="var-type">string</span>&nbsp;<span class="var-name">$filename</span>, [<span class="var-type">array</span>&nbsp;<span class="var-name">$options</span> = <span class="var-default">null</span>])
484  </div>
485 
486   <ul class="parameters">
487       <li>
488     <span class="var-type">string</span>
489     <span class="var-name">$filename</span><span class="var-description">: the name of the streamed file</span>      </li>
490       <li>
491     <span class="var-type">array</span>
492     <span class="var-name">$options</span><span class="var-description">: header options (see above)</span>      </li>
493     </ul>
494</div>
495
496<a name="inline"> </a>
497<h2>Inline PHP Support</h2>
498
499<p>dompdf supports two varieties of inline PHP code.  All PHP evaluation is
500controlled by the <code>DOMPDF_ENABLE_PHP</code> configuration option.  If it is set to
501false, then no PHP code is executed.  Otherwise, PHP is evaluated in two
502passes:</p>
503
504<p>The first pass is useful for inserting dynamic data into your PDF.  You can
505do this by embedding &lt;?php ?&gt; tags in your HTML file, as you would in a
506normal .php file.  This code is evaluated prior to parsing the HTML, so you
507can echo any text or markup and it will appear in the rendered PDF.</p>
508
509<p>The second pass is useful for performing drawing operations on the
510underlying PDF class directly.  You can do this by embedding PHP code within
511&lt;script type="text/php"&gt; &lt;/script&gt; tags.  This code is evaluated
512during the rendering phase and you have access to a few internal objects and
513operations.  In particular, the <code>$pdf</code> variable is the current instance of
514Canvas.  Using this object, you can write and draw directly on the
515current page.  Using the <code>Canvas::open_object()</code>,
516<code>Canvas::close_object()</code> and
517<code>Canvas::add_object()</code> methods, you can create text and
518drawing objects that appear on every page of your PDF (useful for headers &amp;
519footers).</p>
520
521<p>The following variables are defined for you during the second pass of PHP
522execution:</p>
523<pre>
524  $pdf         the current instance of Canvas
525  $PAGE_NUM    the current page number
526  $PAGE_COUNT  the total number of pages in the document
527</pre>
528
529<p>For more complete documentation of the Canvas API, see the <a
530href="http://www.digitaljunkies.ca/dompdf/doc/">API documentation</a>.</p>
531
532<?php include("foot.inc"); ?>
Note: See TracBrowser for help on using the repository browser.