How to apply an HTML Template in PHP
1 min readApr 15, 2020
HTML templates are something we have all used, but their application is generally not something we implement directly. Today we are going to dive into this problem, and I will be showing you an incredibly easy way to get it done using PHP. Let’s begin.
Starting off, we need to install our client. This composer command will handle that.
composer require cloudmersive/cloudmersive_document_convert_api_client
Next up, let’s call the function convertTemplateApplyHtmlTemplate:
<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\ConvertTemplateApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$value = new \Swagger\Client\Model\HtmlTemplateApplicationRequest(); // \Swagger\Client\Model\HtmlTemplateApplicationRequest | Operations to apply to templatetry {$result = $apiInstance->convertTemplateApplyHtmlTemplate($value);print_r($result);} catch (Exception $e) {echo 'Exception when calling ConvertTemplateApi->convertTemplateApplyHtmlTemplate: ', $e->getMessage(), PHP_EOL;}?>
Well, that sure was easy, wasn’t it? Some other related API functions allow converting HTML to to PDF and removing HTML script from strings to protect against scripting attacks.