prexview / prexview
A composer library to use PrexView, a fast, scalable and friendly service for programatic HTML, PDF, PNG or JPG generation using JSON or XML data.
Requires
- rmccue/requests: ^1.7
This package is not auto-updated.
Last update: 2024-09-15 02:35:28 UTC
README
A composer library to use PrexView, a fast, scalable and friendly service for programatic HTML, PDF, PNG or JPG generation using JSON or XML data.
See PrexView for more information about the service.
Installation
Composer
php composer.phar require prexview/prexview
Manually
git clone https://github.com/prexview/prexview-php.git vendor/prexview
Getting started
Get your API Key
You can get an API Key from PrexView
Set up your API Key
If you can setup enviroment variables
export PXV_API_KEY="YOUR_API_KEY"
If you can't setup environment variables, create the PrexView object with your API Key as argument
$pxv = new PrexView\PrexView('YOUR_API_KEY');
Include the library
Composer installation
require __DIR__ . '/vendor/autoload.php';
Manual installation
require __DIR__ . '/vendor/prexview/src/PrexView.php';
Sending an XML
To send an XML string use $pxv->sendXML($xml, $options)
method, this method will return a Response object on success or thrown an error.
Example
$pxv = new Prexview\Prexview(); $options = new stdClass(); $options->template = 'supported_languages'; $options->output = 'pdf'; $xml = '<?xml version="1.0" encoding="UTF-8"?> <languages> <lang code="en">English</lang> <lang code="es">Español</lang> <lang code="fr">Française</lang> </languages>'; $file = 'test.pdf'; try { $res = $pxv->sendXML($xml, $options); file_put_contents($file, $res->file); echo 'File created: ' . $file; } catch (Exception $e) { die($e->getMessage()); }
Sending a JSON
To send a JSON string or PHP standard object use $pxv->sendJSON($json, $options)
method, this method will return a Response object on success or thrown an error.
Example
$pxv = new Prexview\Prexview(); $options = new stdClass(); $options->template = 'supported_languages'; $options->output = 'pdf'; $json = new StdClass(); $en = new StdClass(); $es = new StdClass(); $fr = new StdClass(); $en->code = 'en'; $en->name = 'English'; $es->code = 'es'; $es->name = 'Español'; $fr->code = 'fr'; $fr->name = 'Française'; $json->languages = [$en, $es, $fr]; $file = 'test.pdf'; try { $res = $pxv->sendJSON($json, $options); file_put_contents($file, $res->file); echo 'File created: ' . $file; } catch (Exception $e) { die($e->getMessage()); }
Response object
Options
Dynamic values
In template or note options you can use JSON sintax to access data and have dynamic values, for instance having the following JSON data:
{ "Data": { "customer": "123" } }
Your template or note can use any data attribute or text node, for instance:
'invoice-customer-{{Data.customer}}'
Then we will translate that to the following:
'invoice-customer-123'
And finally the service will try to find the template or note invoice-customer-123
in order to transform the data and generate the document.
License
MIT © PrexView