1. Products
  2.   HTML
  3.   PHP
  4.   Browsershot
 
  

Free PHP Library for Converting a Webpage to an Image or PDF

Open Source PHP Library that allows software developers to convert a webpage to an image or PDF with just a couple of lines of PHP code.

In today's digital age, web development, and design have become more critical than ever. One of the essential aspects of this process is ensuring that web pages look and function as intended across different browsers and devices. Taking screenshots of web pages and exporting them to other file formats is a very useful and complex process. One such tool that makes it easy for software developers is Browsershot. It provides a straightforward API that allows software developers to generate screenshots effortlessly. It abstracts away the complexities of configuring a headless browser and provides a clean and intuitive interface to work with.

Browsershot is a popular PHP library that enables software developers to create high-quality screenshots of web pages and convert them to a webpage, an image, or a pdf with just a couple of lines of PHP code. It leverages the power of Puppeteer, a Node.js library developed by Google, which controls a headless Chrome or Chromium browser instance to interact with web pages and capture screenshots. Software developers can easily automate the process of capturing website screenshots, saving significant time and effort. This is especially beneficial when users need to capture screenshots of multiple web pages or perform regular visual regression testing.

The Browsershot library provides a wide range of configuration options to customize the screenshot generation process. Software developers can set the viewport size, adjust the delay before capturing the screenshot, define the output format (e.g., PNG or JPEG), capture full-page screenshots or screenshots of specific elements within a web page, and even apply custom CSS styles to the web page before taking the screenshot. It is a PHP library and can be easily integrated into existing PHP projects. If you're a PHP developer looking for an efficient way to automate screenshot generation, Browsershot is definitely worth exploring.

Previous Next

Getting Started with Browsershot

The recommended and easiest way to install Browsershot is using Composer, the dependency management tool for PHP. Please use the following command a smooth installation.

Install Browsershot via Composer

composer require spatie/browsershot 

You can also install it manually; download the latest release files directly from GitHub repository.

Image Generation via PHP API

The open source Browsershot library allows software developers to load HTML file and convert it to various Image file formats using PHP API. By default the screenshot's size will match the resolution user’s use for their desktop but allows customize according their needs. The library has included various important features for generations such as taking a full page screenshot, creating a greyscale image, setting the device scale, emulate a mobile view, disabling JavaScript when capturing the page, block URLs, add CSS styles prior to your screenshot, output the image directly to the browser and many more. The following example shows how to create Greyscale Image via PHP API.

Creating a Greyscale Image via PHP API

Browsershot::url('https://example.com')
    ->windowSize(640, 480)
    ->greyscale()
    ->save($pathToImage);

PDF Generation via PHP Library

The open source Browsershot library makes it easy for software developers to save a web page to a PDF files inside their PHP applications. The will save a pdf if the path passed to the save method has a pdf extension. There are several important features part of the library related to PDF creation, such as specifying the width and the height of the PDF page, setting margins, adding header and footer to PDF, hide the header or footer, add background of HTML page, Landscape orientation support, export specific pages, set the initial page number and many more. The following example shows how software developers can add headers and footers to PDF while generating it from HTML page.

How to Add Headers and Footers While Generating PDF using PHP Code?

Browsershot::html($someHtml)
   ->showBrowserHeaderAndFooter()
   ->headerHtml($someHtml)
   ->footerHtml($someHtml)
   ->save('example.pdf');

Headless Chrome and Puppeteer Integration

Browsershot utilizes the power of headless Chrome or Puppeteer to render web pages accurately. This ensures that the screenshots generated by Browsershot match the actual appearance of the web page in a browser. Additionally, Browsershot supports both local and remote browsers, giving you the freedom to choose the most suitable setup for your needs. Browsershot leverages headless Chrome or Puppeteer to ensure that the rendered web pages closely match their appearance in a real browser. This guarantees consistent and accurate screenshots, providing reliable visual representation for documentation and testing purposes.