source: trunk/client/inc/hpdf5/spipu/html2pdf/doc/extension.md@ 407

Last change on this file since 407 was 347, checked in by roby, 3 years ago

Aggiornamento per compatibilità con php7.4

File size: 2.8 KB
RevLine 
[347]1# Extensions and Tags
2
3[back](./README.md)
4
5## Html Tags
6
7All the existing tags are under the namespace `\Spipu\Html2Pdf\Tag`.
8
9A tag must implement the interface `\Spipu\Html2Pdf\Tag\TagInterface`.
10
11A tag can extends the abstract class `\Spipu\Html2Pdf\Tag\AbstractTag` to implement some generic methods.
12
13Here is a tag example that will do nothing:
14
15```php
16<?php
17namespace Example\Html2Pdf\Tag;
18
19use \Spipu\Html2Pdf\Tag\AbstractTag;
20
21/**
22 * Tag Example
23 */
24class Example extends AbstractTag
25{
26 /**
27 * get the name of the tag
28 *
29 * @return string
30 */
31 public function getName()
32 {
33 return 'example';
34 }
35
36 /**
37 * Open the HTML tag
38 *
39 * @param array $properties properties of the HTML tag
40 *
41 * @return boolean
42 */
43 public function open($properties)
44 {
45 // there is nothing to do here
46
47 return true;
48 }
49
50 /**
51 * Close the HTML tag
52 *
53 * @param array $properties properties of the HTML tag
54 *
55 * @return boolean
56 */
57 public function close($properties)
58 {
59 // there is nothing to do here
60
61 return true;
62 }
63}
64```
65
66Then, you will be able to use the `<example>` html tag.
67
68Look at all the existing tags in the `/src/Tag` folder to see examples.
69
70## Extensions
71
72In order to add your new tags to Html2Pdf, you must a extension.
73
74An extension must implement the interface `\Spipu\Html2Pdf\Extension\ExtensionInterface`.
75
76You must implement the `getTags` method that will return a list of tag objects.
77
78```php
79<?php
80/**
81 * Extension Example
82 */
83namespace Example\Html2Pdf\Extension;
84
85use \Spipu\Html2Pdf\Extension\ExtensionInterface;
86
87/**
88 * Class MyExtension
89 */
90class MyExtension implements ExtensionInterface
91{
92 /**
93 * @var array
94 */
95 private $tagDefinitions = array();
96
97 /**
98 * {@inheritDoc}
99 */
100 public function getName()
101 {
102 return 'example_extension';
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 public function getTags()
109 {
110 if (empty($this->tagDefinitions)) {
111 $this->tagDefinitions = array(
112 new \Example\Html2Pdf\Tag\Example(),
113 new \Example\Html2Pdf\Tag\Other(),
114 );
115 }
116
117 return $this->tagDefinitions;
118 }
119}
120```
121
122Then, you can add your extension to Html2Pdf with the following method:
123
124```php
125$html2pdf->addExtension(new \Example\Html2Pdf\Extension\MyExtension());
126```
127
128The following core extensions are automatically added to Html2pdf:
129
130 * `\Spipu\Html2Pdf\Extension\Core\HtmlExtension`
131 * `\Spipu\Html2Pdf\Extension\Core\SvgExtension`
132
133It contains all the native tags.
134
135## Overriding
136
137If an extension has the same name that an already added extension, it will replace the first one.
138
139If tag has the same name that an already added tag throw an extension, it will replace the first one.
140
141[back](./README.md)
Note: See TracBrowser for help on using the repository browser.