Unable to get Laravel Spatie/Browsershot working with Puppeteer, Ubuntu 24.04, PHP 8.3, Chrom & Nginx [closed]

Environment:

  • PHP 8.3
  • Laravel 11
  • Ubuntu 24.04 LTS
  • NGINX
  • Node (v18.19.1) (latest for today)
  • Spatie/Browsershot (Puppeteer, Chrome 128.0.6613.119)

Issues:

  1. Spatie wanting me to install outdated Node/Puppeteer versions (https://spatie.be/docs/browsershot/v4/requirements). And we all want the latest.
  2. Even if we follow Spatie instructions, we get Permissions errors (www-data user cannot run Chrome or has no permissions to create temp directories).
  3. Other Chrome versions (chromium-browser or non-snap .deb installations) methods do not work (php8.3-fpm does not belong to Snap cgroup or other issues with permissions).
  4. Since we want to use the latest soft versions, some packages are missing.

1

How to make it work (“This is the way”):

  1. Install LEMP. Just as you usually do.

  2. Create a real www-data user with /home/www-data directory.

  • Crete directory: sudo mkdir /home/www-data
  • Stop NGINX and php-fpm: sudo service nginx stop, sudo service php8.3-fpm stop
  • Change www-data user settings: sudo usermod -d /home/www-data www-data -s /bin/bash
  • Maybe kill user related processes kill {proocess_id}
  • Start NGINX and php-fpm: sudo service nginx start, sudo service php8.3-fpm start
  1. Install Laravel into /home/www-data/laravel directory.

  2. Install Puppeteer globally.

  • Install node and npm: sudo apt install nodejs, sudo apt install npm
  • Install Puppeteer: npm install puppeteer --location=global
  • Install related soft: gconf-service, libasound2, libatk1.0-0, libc6, libcairo2, libcups2, libdbus-1-3, libexpat1, libfontconfig1, libgbm1, libgcc1, libgdk-pixbuf2.0-0, 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, ca-certificates, fonts-liberation, libnss3, lsb-release, xdg-utils, wget, libgbm-dev, libxshmfence-dev, libatk-bridge-2.0.so.0, liboss4-salsa-asound2, alsa.
    Install it as sudo apt-get install -y {package_name}. I found some issues when trying to install it in a bulky way, so I recommend installing it one by one to make sure everything is installed.
  1. Ask Puppeteer to install Chrome browser.
    Run: sudo -u www-data npx puppeteer browsers install chrome
    When it’s done, you will see the Chrome file location. For me it was: /home/www-data/.cache/puppeteer/chrome/linux-128.0.6613.119/chrome-linux64/chrome. Use this path in your code with Browsershot: ->setChromePath("/home/www-data/.cache/puppeteer/chrome/linux-128.0.6613.119/chrome-linux64/chrome")

  2. Use Puppeteer Chrome browser to generate PDF.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Browsershot::html($html)
->setOption('args', ['--disable-web-security'])
->ignoreHttpsErrors()
->noSandbox()
->setCustomTempPath('/home/www-data/browsershot-html')
->addChromiumArguments([
'lang' => "en-US,en;q=0.9",
'hide-scrollbars',
'enable-font-antialiasing',
'force-device-scale-factor' => 1,
'font-render-hinting' => 'none',
'user-data-dir' => '/home/www-data/user-data',
'disk-cache-dir' => '/home/www-data/user-data/Default/Cache',
])
->setChromePath("/home/www-data/.cache/puppeteer/chrome/linux-128.0.6613.119/chrome-linux64/chrome")
->newHeadless()
->showBackground()
->savePdf($tmp_file);
</code>
<code>Browsershot::html($html) ->setOption('args', ['--disable-web-security']) ->ignoreHttpsErrors() ->noSandbox() ->setCustomTempPath('/home/www-data/browsershot-html') ->addChromiumArguments([ 'lang' => "en-US,en;q=0.9", 'hide-scrollbars', 'enable-font-antialiasing', 'force-device-scale-factor' => 1, 'font-render-hinting' => 'none', 'user-data-dir' => '/home/www-data/user-data', 'disk-cache-dir' => '/home/www-data/user-data/Default/Cache', ]) ->setChromePath("/home/www-data/.cache/puppeteer/chrome/linux-128.0.6613.119/chrome-linux64/chrome") ->newHeadless() ->showBackground() ->savePdf($tmp_file); </code>
Browsershot::html($html)
            ->setOption('args', ['--disable-web-security'])
            ->ignoreHttpsErrors()
            ->noSandbox()
            ->setCustomTempPath('/home/www-data/browsershot-html')
            ->addChromiumArguments([
                'lang' => "en-US,en;q=0.9",
                'hide-scrollbars',
                'enable-font-antialiasing',
                'force-device-scale-factor' => 1,
                'font-render-hinting' => 'none',
                'user-data-dir' => '/home/www-data/user-data',
                'disk-cache-dir' => '/home/www-data/user-data/Default/Cache',
            ])
->setChromePath("/home/www-data/.cache/puppeteer/chrome/linux-128.0.6613.119/chrome-linux64/chrome")
            ->newHeadless()
            ->showBackground()
            ->savePdf($tmp_file);

You may not use some options from the example above to make your PDF generation more secure. Feel free to adjust it for your needs.

Hope my struggles will help someone save time.

This answer helped me a lot:
Unable to get Laravel Browsershot working with Puppeteer Ubuntu 20.04 Arm PHP 8.2 with Snap Chromium & Nginx

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật