人気のPHP WEBアプリケーションフレームワークLaravelのTipsを記録していきます

Laravel で BrowsershotでPDF表示

● BrowsershotでPDF表示

● spatie/browsershot

https://github.com/spatie/browsershot

1. puppeteer のインストール

npm install puppeteer

こちらのコマンドでうまくいかないときは以下のコマンドでインストールします

npm install puppeteer --global

2. browsershot のインストール

composer require spatie/browsershot


3-A. 表示のテスト(Tinker使用)

php artisan tinker
use Spatie\Browsershot\Browsershot;
$timestamp = \Carbon\Carbon::now()->format("Ymd_His_v");
$file_path = "C:/Users/hogehoge/test_puppeteer_{$timestamp}.png";
Browsershot::url('https://www.google.com/?hl=ja')->setOption('args', ['--no-sandbox','--disable-web-security'])->save($file_path);

testpuppeteer<実行日時>.png が作成されれば成功です

3-B. 表示のテスト(ウェブサイトをpdfにして表示する場合)

use Spatie\Browsershot\Browsershot;
$timestamp = \Carbon\Carbon::now()->format("Ymd_His_v");
$file_path = "C:/Users/hogehoge/test_puppeteer_{$timestamp}.png";

Browsershot::url('https://www.google.com/?hl=ja')
    ->setOption('args', ['--no-sandbox','--disable-web-security'])
    ->save( $file_path );

$file = file_get_contents( public_path( $file_path ) );

return response($file, 200)
    ->header('Content-Type', 'application/pdf')
    ->header('Content-Disposition', 'inline; filename="' . $file_path . '"');

● node が見つからないと言うエラーになる場合は明示的に指定します

nodenv を使用している場合は ~/.anyenv/envs/nodenv/versions/ 以下のパスを直接指定します。

/home/kusanagi/.anyenv/envs/nodenv/versions/18.13.0/bin

の場合

->setNodeBinary('/home/kusanagi/.anyenv/envs/nodenv/versions/18.13.0/bin/node')
->setNpmBinary('/home/kusanagi/.anyenv/envs/nodenv/versions/18.13.0/bin/npm')

setNodeBinary でnodeのパスを明示的に指定します

Browsershot::url('https://www.google.com/?hl=ja')
            ->setNodeBinary('/home/kusanagi/.anyenv/envs/nodenv/versions/18.13.0/bin/node')

3-C. 表示のテスト(ローカルのhtmlをpdfにして表示する場合)

use Spatie\Browsershot\Browsershot;
        $html = "<h1>TEST</h1>";
        Browsershot::html( $html )
            ->setOption('args', ['--no-sandbox','--disable-web-security'])
            ->save('./test.pdf');

        $file = file_get_contents( public_path('./test.pdf') );

        return response($file, 200)
            ->header('Content-Type', 'application/pdf')
            ->header('Content-Disposition', 'inline; filename="' . './test.pdf' . '"');

error while loading shared libraries: libatk-1.0.so.0: cannot open shared object file エラーになる場合

puppetter の 共有ライブラリが不足しています。

・足りない共有ライブラリを調べる

cd /YOUR-PATH-TO-PUPPETEER/puppeteer/.local-chromium/linux-818858/chrome-linux
ldd chrome

これで not found と言われている .so をインストールする必要があります。

yumコマンドでインストールしましょう。

● LaravelでPDF作成時に外部cssが読み込まれない不具合の対応

  <link rel="stylesheet" href="{{ url('/assets/css_pdf/pdfprint.css') }}">

 ↓ 次のようにして絶対パスに書き換えます

  <link rel="stylesheet" href="{{ public_path('/assets/css_pdf/pdfprint.css') }}">

● puppetter の LD_LIBRARY_PATH を手動で追加する

vendor/spatie/browsershot/Browsershot.php

    protected function callBrowser(array $command)
    {
        $fullCommand = $this->getFullCommand($command);
        $process = Process::fromShellCommandline($fullCommand)->setTimeout($this->timeout);

        // ● この行を追加 ↓
        $process->setEnv(array('LD_LIBRARY_PATH' => "/PATH/TO/YOUR/puppeteer_lib64" ));

        $process->run();

● verumconsilium/laravel-browsershot を使用する

こちらのパッケージを利用するともっと簡単にPDF出力することができます

composer require verumconsilium/laravel-browsershot
	return \VerumConsilium\Browsershot\Facades\PDF::loadHtml('<h1>TEST印刷</h1>')
			->setNodeBinary('/Users/hogehoge/.anyenv/envs/nodenv/shims/node')
			->setNpmBinary('/Users/hogehoge/.anyenv/envs/nodenv/shims/npm')		
			->inline();
No.1880
08/14 12:51

edit