PHP code example of devture / browserless

1. Go to this page and download the library: Download devture/browserless 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/ */

    

devture / browserless example snippets


$browserlessApiUrl = 'http://localhost:3000'; // Or 'http://browserless:3000', etc.
$browserlessToken = 'SOME_TOKEN_HERE'; // Can be null for unsecured instances
$browserlessTimeoutSeconds = 15;

$client = new \Devture\Component\Browserless\Client(
	new \GuzzleHttp\Client(),
	$browserlessApiUrl,
	$browserlessToken,
	$browserlessTimeoutSeconds,
);

$url = 'https://devture.com';

$pdfCreationRequest = new \Devture\Component\Browserless\Model\PdfCreationRequest();
$pdfCreationRequest->setUrl($url);
$pdfCreationRequest->setOptions([
	'printBackground' => true,
	'format' => 'A4',
	'landscape' => true,
]);

$pdfBytes = $client->createPdfFromRequest($pdfCreationRequest);

$html = '<html><body>Some <strong>HTML</strong> here</body></html>';

$pdfCreationRequest = new \Devture\Component\Browserless\Model\PdfCreationRequest();
$pdfCreationRequest->setHtml($html);
$pdfCreationRequest->setOptions([
	'printBackground' => true,
	'format' => 'A4',
	'margin' => [
		'top' => '20mm',
		'bottom' => '10mm',
		'left' => '10mm',
		'right' => '10mm',
	],
]);

$pdfBytes = $client->createPdfFromRequest($pdfCreationRequest);

// Alternatively, save this as a local workspace file and load it from there using the `file://` protocol.
// (This allows you to access other files you may have mounted on the filesystem).
// $pdfBytes = $client->createPdfFromHtmlRequestUsingFileProtocol($pdfCreationRequest);

// These calls save files into the Browserless workspace directory (e.g. `/workspace/<UUID>.<extension>`).
$workspaceFileLogo = $client->createWorkspaceFile(file_get_contents('/path/to/logo.jpg'), 'jpg');
$workspaceFileStyles = $client->createWorkspaceFile(file_get_contents('/path/to/styles.css'), 'css');

$html = '
<html>
	<head>
		<link rel="stylesheet" href="file://' . $workspaceFileStyles->getPath() . '" />
	</head>
	<body>
		<img src="' . $workspaceFileLogo->getPath() . '" alt="Logo" />

		Some <strong>HTML</strong> here
	</body>
</html>';

$pdfCreationRequest = new \Devture\Component\Browserless\Model\PdfCreationRequest();
$pdfCreationRequest->setHtml($html);
$pdfCreationRequest->setOptions([
	'printBackground' => true,
	'format' => 'A4',
]);

// We need to use `createPdfFromHtmlRequestUsingFileProtocol()` here,
// because we can only access files via the `file://` protocol
// if the HTML is also served from a `file://`-accessed file.
$pdfBytes = $client->createPdfFromHtmlRequestUsingFileProtocol($pdfCreationRequest);

// Optionally, clean up. Because we have `WORKSPACE_DELETE_EXPIRED` enabled,
// workspace files will be auto-cleaned at some point anyway, but..
$client->deleteWorkspaceFile($workspaceFileLogo);
$client->deleteWorkspaceFile($workspaceFileStyles);