PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/DomPDF/www/usage.php

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