Spatie Browsershot

repository·main·Indexed 26 days ago

https://github.com/spatie/browsershot

A PHP package that renders web pages to images or PDFs using Puppeteer and a headless version of Google Chrome. It supports rendering from URLs, raw HTML strings, or local HTML files, and provides capabilities to interact with pages via clicking, typing, and selecting options. It also includes a CLI tool for executing Puppeteer commands and methods for extracting page body HTML and network requests.

Tokens
11.1K
Snippets
56
Records
98
Agent score
84%

What's inside spatie/browsershot

  1. Render web pages to images or PDFs

    main

    Browsershot uses Puppeteer to convert web pages into images or PDF files. The output format is determined by the file extension provided to the save method.

    To save an image, provide a path with an image extension. To save a PDF, provide a path with a .pdf extension.

  2. Bypass command line character overflow using writeOptionsToFile()

    main

    When providing a large number of options to Puppeteer, Browsershot may fail due to command line character limits (overflow). To prevent this, use the writeOptionsToFile() method. This instructs Browsershot to write the options to a temporary file and pass that file to Puppeteer instead of passing them directly via the command line.

    Browsershot::url('https://example.com')
       ->writeOptionsToFile()
       ...
  3. Convert a webpage to an image or PDF

    main

    Use the url() method to target a specific webpage and the save() method to export the result. Browsershot determines the output format based on the file extension provided to save(): use an image extension (e.g., .png, .jpg) for images, or .pdf for PDF documents.

    use Spatie\Browsershot\Browsershot;
    
    // Save an image
    Browsershot::url('https://example.com')->save($pathToImage);
    
    // Save a PDF
    Browsershot::url('https://example.com')->save('example.pdf');
  4. Install Puppeteer on Ubuntu (Forge/Linux)

    main

    For Ubuntu v24.04 servers (such as those provisioned by Laravel Forge), follow these steps to install Node, Puppeteer, Chromium, and the necessary system dependencies.

    Note for Ubuntu v22.04 users: Replace libasound2t64 with libasound2 in the dependency list.

    # Check if node and npm are installed
    node -v && npm -v
    
    # Install puppeteer
    sudo npm install -g puppeteer
    
    # Install chromium
    npx puppeteer browsers install chrome
    
    # Install dependencies
    sudo apt update
    sudo apt install libasound2t64 libatk1.0-0 libatk-bridge2.0-0 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6
  5. Change the browser language using setEnvironmentOptions

    main

    You can change the language of the browser by using the setEnvironmentOptions method. This is useful when you need to load a page in a specific language or locale. Pass an associative array to setEnvironmentOptions containing the LANG key with your desired locale (e.g., 'en-GB').

    Browsershot::url('https://example.com')
       ->setEnvironmentOptions([
          'LANG' => 'en-GB',
       ])
       ...