Skip to content

Quick start with Symfony

Most HTML-to-PDF converters in PHP either have limited support for HTML, CSS, and JavaScript, or rely on wkhtmltopdf, which is no longer maintained. Doppio gives you a simpler alternative.

Learn how to send your first request with Symfony.

Render PDF

Using Doppio is simpler than maintaining a third-party bundle. In practice, all you need to do is send an HTTP request.

You can wrap the Doppio call in a service class:

php
namespace App\Service;

use Symfony\Contracts\HttpClient\HttpClientInterface;

class PdfService
{
    private $client;
    private $apiEndpoint;

    public function __construct(HttpClientInterface $client)
    {
        $this->client = $client;
        $this->apiEndpoint = 'https://api.doppio.sh/v1/render/pdf/direct'; // Your API endpoint
    }

    public function createPdf(array $data): string
    {
        $response = $this->client->request('POST', $this->apiEndpoint, [
            'headers' => [
                'Authorization' => 'Bearer ' . $_ENV['DOPPIO_AUTH_TOKEN'],
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
            ],
            'json' => $data,
        ]);

        if ($response->getStatusCode() === 200) {
            return $response->getContent();
        }

        throw new \Exception('Failed to generate PDF: ' . $response->getContent(false));
    }
}

Then you can use the service like this:

php
$data = [
    'page' => [
        'pdf' => [
            'printBackground' => 'true'
        ],
        'goto' => [
            'url' => 'https://your-url',
            'options' => [
                'waitUntil' => [
                    'networkidle0',
                ],
            ],
        ],
    ],
];

$pdfContent = $this->pdfService->createPdf($data);

Check the synchronous and asynchronous methods to decide which one best fits your project.

Need more?

Looking for another framework? Explore our quick start guides by category.

All rights reserved