1. Go to this page and download the library: Download html2img/html2img-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
html2img / html2img-php example snippets
use Html2img\Html2imgClient;
use Html2img\Request\HtmlRequest;
$client = new Html2imgClient('your-api-key');
$response = $client->html(new HtmlRequest(
html: '<!doctype html><html><body><h1>Hello</h1></body></html>',
width: 1200,
height: 630,
));
echo $response->url; // https://i.html2img.com/abc123def456.png
use Html2img\Html2imgClient;
$client = new Html2imgClient(
apiKey: 'your-api-key',
baseUri: 'https://app.html2img.com', // default
timeout: 35.0, // seconds, default
);
use GuzzleHttp\Client;
use Html2img\Html2imgClient;
$guzzle = new Client([
'base_uri' => 'https://app.html2img.com',
'timeout' => 60,
// your own handler stack, middleware, proxy settings, etc.
]);
$client = new Html2imgClient('your-api-key', httpClient: $guzzle);
$response->success; // bool
$response->id; // string|null, the render id
$response->url; // string|null, the CDN URL of the image
$response->creditsRemaining; // int|null, credits left after this call
$response->status; // string|null, "processing" for async jobs
$response->message; // string|null
$response->template; // string|null, the template slug, when applicable
$response->isProcessing(); // bool
$response->raw(); // array, the full decoded JSON payload
$response = $client->screenshot(new ScreenshotRequest(
url: 'https://example.com/long-report',
fullpage: true,
webhookUrl: 'https://your-app.example.com/hooks/html2img',
));
if ($response->isProcessing()) {
// The final URL will arrive at your webhook, not on this response.
}
use Html2img\Exception\Html2imgException;
use Html2img\Exception\ValidationException;
use Html2img\Exception\InsufficientCreditsException;
try {
$response = $client->html(new HtmlRequest(html: $document));
} catch (ValidationException $e) {
// 400 or 422: inspect the per-field messages
foreach ($e->details() as $field => $messages) {
// ...
}
} catch (InsufficientCreditsException $e) {
// 402: out of credits
$left = $e->creditsRemaining();
} catch (Html2imgException $e) {
// anything else
$e->statusCode(); // int|null
$e->errorCode(); // string|null, the API "code" field
$e->payload(); // array, the decoded body
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.