devture / browserless
Library for communicating with the Browserless.io API (generating PDFs, etc.)
Installs: 4 182
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=8.0
- guzzlehttp/guzzle: >=6.3,<8.0
- symfony/uid: >=6.4.0,<8.0-dev
README
This is a library for interacting with the Browserless.io APIs.
For the time being, this library only supports these APIs:
- /pdf - for generating PDFs from a URL or inline HTML (like wkhtmltopdf, but better -- more up-to-date browser engine, etc.)
Prerequisites
You either need to use your own self-hosted Browserless instance (see how to do it with Docker) or their hosted offering (see Pricing).
You could use the following compose.yml
setup:
version: '2.1' services: browserless: image: ghcr.io/browserless/chromium:v2.23.0 restart: unless-stopped # Matches the owner (`blessuser:blessuser`) of `/usr/src/app` user: 999:999 environment: CONCURRENT: 10 TOKEN: SOME_TOKEN_HERE # Not exposing the port is recommended, if PHP is running in a container alongisde this one ports: - "127.0.0.1:3000:3000" tmpfs: - /tmp
Usage
Creating a Browserless API client
$browserlessApiUrl = 'http://localhost:3000'; // Or 'http://browserless:3000', etc. $browserlessToken = 'SOME_TOKEN_HERE'; $browserlessTimeoutSeconds = 15; $client = new \Devture\Component\Browserless\Client( new \GuzzleHttp\Client(), $browserlessApiUrl, $browserlessToken, $browserlessTimeoutSeconds, );
Generating a PDF from a URL
$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);
Generating a PDF from inline HTML
$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);
Alternatives
-
the SynergiTech/chrome-pdf-php library can also render PDFs via Browserless
-
wkhtmltopdf invoked via knplabs/knp-snappy