Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Inquisitive's avatar

HTML To PDF with exactly same design as it is in div

Is there any package or method, with which I could just print the data exactly in the same design as it is in webpage, something like taking a screenshot?

I have been taking a look at https://doc.doppio.sh/guide/cookbook/protected-url.html, but could not figure out how could I do printing on protected URL. I am using this on laravel 6 and implementing the default auth (this is currently in ui/auth ) package at the moment. My code:

$client = new \GuzzleHttp\Client();
        $response = $client->post('https://api.doppio.sh/v1/render/pdf/sync', [
            'headers' => [
                'Accept'     => 'application/json',
                'Authorization' => 'Bearer apikey',
                'Content-Type' => 'application/json'
            ],
            'json' => [
                'page' => [
                    "pdf" => [
                        "printBackground" => true
                    ],
                    "goto" => [
                        "url" => 'https://example.com/admin/property'
                    ],
                    "authenticate" => [
                        "username" => '[email protected]',
                        "password" => 'password'
                    ],
                ]
            ]
        ]);
        $responseBody = $response->getBody();

If I pass like this it is printing the login page instead of the login in the user, it is also offering to pass the cookie, could we authenticate it using cookie?

Or are there any other better and easier options to achieve it? Any other paid package? I am open to anything.

0 likes
1 reply
kokoshneta's avatar

The best available is Browsershot by Spatie. It uses a (by default) headless Chromium browser to render the HTML/CSS and print it to PDF.

It can be a bit finicky to set up (requires Puppeteer, which is super-easy in some environments and a bit of a pain in others), but once you get it working, it works quite well.

As long as you’re within your Laravel ecosystem, you can feed Browsershot a rendered view as the data to PDF-ify; you don’t have to load a URL through an HTTP request. Check in your controller whether the user is allowed to create the PDF, then build your view data and manually render the view, passing the rendered data to Browsershot.

But be aware that printing to a PDF file will never be an exact replica of what the browser shows. Screen and print are very different media, and you’ll need to carefully craft your CSS for everything to look how you want it when the PDF is created.

1 like

Please or to participate in this conversation.