|
| 1 | +# How to pass arguments to the javascript file |
| 2 | + |
| 3 | +In this example, we will use arguments to programmatically set the orientation of the PDF. |
| 4 | + |
| 5 | +First, the configuration should be changed |
| 6 | + |
| 7 | +```yaml |
| 8 | +padam87_rasterize: |
| 9 | + script: /js/my-rasterize.js |
| 10 | + arguments: |
| 11 | + format: pdf |
| 12 | + orientation: portrait |
| 13 | +``` |
| 14 | +
|
| 15 | +Note that the orientation argument has been added, by default it will be set as `portrait`. |
| 16 | + |
| 17 | +A custom javascript is also necessary, to handle the newly received argument. |
| 18 | + |
| 19 | +```js |
| 20 | +var page = require('webpage').create(), |
| 21 | + system = require('system'), |
| 22 | + address, output, format, orientation; |
| 23 | +
|
| 24 | +address = system.args[1]; |
| 25 | +output = system.args[2]; |
| 26 | +format = system.args[3]; |
| 27 | +orientation = system.args[4]; |
| 28 | +
|
| 29 | +page.viewportSize = { width: 1000, height: 3000 }; |
| 30 | +page.paperSize = { format: 'A4', orientation: orientation, border: '1cm' }; |
| 31 | +
|
| 32 | +page.open(address, function (status) { |
| 33 | + if (status !== 'success') { |
| 34 | + console.log('Unable to load the address!'); |
| 35 | + phantom.exit(1); |
| 36 | + } else { |
| 37 | + window.setTimeout(function () { |
| 38 | + page.render(output, { format: format }); |
| 39 | + phantom.exit(0); |
| 40 | + }, 200); |
| 41 | + } |
| 42 | +}); |
| 43 | +``` |
| 44 | + |
| 45 | +To change the orientation to `landscape`, you need to add one more parameter to the rasterizer call. |
| 46 | + |
| 47 | +```php |
| 48 | +$this->get('padam87_rasterize.rasterizer')->rasterize( |
| 49 | + $this->renderView('Bundle:Folder:template.pdf.twig') |
| 50 | + [ |
| 51 | + 'orientation' => 'landscape' |
| 52 | + ] |
| 53 | +); |
| 54 | +``` |
0 commit comments