[347] | 1 | # Basic Usage
|
---|
| 2 |
|
---|
| 3 | [back](./README.md)
|
---|
| 4 |
|
---|
| 5 | ## PHP Constructor
|
---|
| 6 |
|
---|
| 7 | The main class of this library is `\Spipu\Html2Pdf\Html2Pdf`.
|
---|
| 8 |
|
---|
| 9 | The PHP constructor takes the following parameters:
|
---|
| 10 |
|
---|
| 11 | Variable | Default value |Description
|
---|
| 12 | ---------|---------------|--------------
|
---|
| 13 | $orientation | P | The default page orientation, can be P (portrait) or L (landscape)
|
---|
| 14 | $format | A4 | The default page format used for pages. The list of the available value are [here](https://github.com/tecnickcom/TCPDF/blob/master/include/tcpdf_static.php#L2097). You can also give a array with 2 values the width and the height in mm.
|
---|
| 15 | $lang | fr | Language to use, for some minor translations. The list of the available languages are [here](https://github.com/spipu/html2pdf/tree/master/src/locale)
|
---|
| 16 | $unicode | true | means that the input HTML string is unicode
|
---|
| 17 | $encoding |UTF-8 | charset encoding of the input HTML string
|
---|
| 18 | $margins | array(5, 5, 5, 8) | Main margins of the page (left, top, right, bottom) in mm
|
---|
| 19 | $pdfa | false | If TRUE set the document to PDF/A mode
|
---|
| 20 |
|
---|
| 21 | In most of the case, you will just use the 3 first parameters :
|
---|
| 22 |
|
---|
| 23 | ```php
|
---|
| 24 | $html2pdf = new \Spipu\Html2Pdf\Html2Pdf('P', 'A4', 'en');
|
---|
| 25 | ```
|
---|
| 26 |
|
---|
| 27 | ## Convert the HTML
|
---|
| 28 |
|
---|
| 29 | The main method to use is `writeHTML`.
|
---|
| 30 |
|
---|
| 31 | It takes one parameter : the HTML in string format that you want to convert into PDF.
|
---|
| 32 |
|
---|
| 33 | ```php
|
---|
| 34 | $html2pdf->writeHTML('<h1>HelloWorld</h1>This is my first test');
|
---|
| 35 | ```
|
---|
| 36 |
|
---|
| 37 | You can call it more than one time, if you want to split the conversion in order to use less memory. It will continue on the same page, directly at the end of the last converted part.
|
---|
| 38 |
|
---|
| 39 | ```php
|
---|
| 40 | $html2pdf->writeHTML('<h1>HelloWorld</h1>This is my first text');
|
---|
| 41 | $html2pdf->writeHTML('<h1>HelloWorld</h1>This is my second text');
|
---|
| 42 | ```
|
---|
| 43 |
|
---|
| 44 | If you want to separate on a new page, you can use the specific HTML tag `page`.
|
---|
| 45 |
|
---|
| 46 | ```php
|
---|
| 47 | $html2pdf->writeHTML('<page><h1>HelloWorld</h1>This is my first page</page>');
|
---|
| 48 | $html2pdf->writeHTML('<page><h1>HelloWorld</h1>This is my second page</page>');
|
---|
| 49 | ```
|
---|
| 50 |
|
---|
| 51 | You can find more information about this specific tag on the [page](page.md) documentation.
|
---|
| 52 |
|
---|
| 53 | ## Get the PDF
|
---|
| 54 |
|
---|
| 55 | The main method to use is `output`.
|
---|
| 56 |
|
---|
| 57 | It takes two not required parameters. You can find more information on the [output](output.md) documentation.
|
---|
| 58 |
|
---|
| 59 | If you do not give any parameters, it will send the PDF file to the browser, to display it.
|
---|
| 60 |
|
---|
| 61 | ```php
|
---|
| 62 | $html2pdf->output();
|
---|
| 63 | ```
|
---|
| 64 |
|
---|
| 65 | ## Full Example
|
---|
| 66 |
|
---|
| 67 | Here is the full code for a helloworld example:
|
---|
| 68 |
|
---|
| 69 | ```php
|
---|
| 70 | $html2pdf = new \Spipu\Html2Pdf\Html2Pdf('P', 'A4', 'en');
|
---|
| 71 | $html2pdf->writeHTML('<h1>HelloWorld</h1>This is my first page');
|
---|
| 72 | $html2pdf->output();
|
---|
| 73 | ```
|
---|
| 74 |
|
---|
| 75 | [back](./README.md)
|
---|