Skip to content

HTMLレンダラー

HtmlRenderer クラスは、すべてのHTML-to-PDF操作のメインエントリポイントです。コンテンツの読み込み、出力の設定、PDFドキュメントのレンダリングのためのフルーエントAPIを提供します。

ファクトリメソッド

静的 create() ファクトリでレンダラーインスタンスを作成します。

php
use Yeeefang\TcpdfNext\Artisan\HtmlRenderer;

// デフォルト設定(Chromeを自動検出)
$renderer = HtmlRenderer::create();

// カスタムChromeバイナリパスを指定
$renderer = HtmlRenderer::create(
    chromePath: '/usr/bin/google-chrome',
);

// カスタム一時ディレクトリを指定
$renderer = HtmlRenderer::create(
    chromePath: '/usr/bin/chromium',
    tempDir: '/tmp/artisan-render',
);

コンテンツの読み込み

文字列から

loadHtml() で生のHTMLを直接渡します。文字列は完全なHTMLドキュメントまたはフラグメントが使用できます。

php
$renderer->loadHtml('<h1>Hello, World!</h1>');

フラグメントを渡すと、Artisanが自動的に最小限の <!DOCTYPE html> ドキュメントでラップします。

ローカルファイルから

loadFile() でディスクから .html ファイルを読み込みます。ファイル内のスタイルシート、画像、スクリプトへの相対パスは、ファイルのディレクトリから解決されます。

php
$renderer->loadFile('/templates/quarterly-report.html');

URLから

loadUrl() でライブURLを取得してレンダリングします。ページはヘッドレスChrome内で読み込まれるため、JavaScriptが実行されAJAXコールがレンダリング前に解決されます。

php
$renderer->loadUrl('https://reports.example.com/q4-2026');

遅いページでハングしないようにナビゲーションタイムアウトを設定できます:

php
$renderer->loadUrl('https://example.com/dashboard', timeoutMs: 30000);

出力メソッド

ファイルに保存

php
$renderer->save('/output/report.pdf');

文字列として取得

さらなる処理(データベースへの保存、メールへの添付など)のために生のPDFバイトを取得します。

php
$pdfContent = $renderer->toString();

// 例:データベースに保存
DB::table('documents')->insert([
    'name'    => 'report.pdf',
    'content' => $pdfContent,
]);

ブラウザに送信

適切なヘッダーとともにPDFをHTTPレスポンスに直接ストリーミングします。

php
// インライン表示(ブラウザPDFビューア)
$renderer->output('report.pdf', 'inline');

// 強制ダウンロード
$renderer->output('report.pdf', 'download');

完全な例:インボイス

php
use Yeeefang\TcpdfNext\Artisan\HtmlRenderer;

$html = <<<'HTML'
<!DOCTYPE html>
<html>
<head>
    <style>
        body { font-family: 'Inter', sans-serif; margin: 20mm; }
        .header { display: flex; justify-content: space-between; align-items: flex-start; }
        .company { font-size: 24px; font-weight: 700; color: #1a237e; }
        .meta { text-align: right; color: #666; font-size: 13px; }
        .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin: 30px 0; }
        .grid section { padding: 15px; background: #f8f9fa; border-radius: 6px; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th { background: #1a237e; color: white; padding: 10px 12px; text-align: left; }
        td { border-bottom: 1px solid #e0e0e0; padding: 10px 12px; }
        tr:nth-child(even) { background: #fafafa; }
        .total { font-weight: 700; font-size: 18px; text-align: right; margin-top: 20px; }
    </style>
</head>
<body>
    <div class="header">
        <div class="company">Acme Corporation</div>
        <div class="meta">
            Invoice #2026-001<br>
            Date: 2026-02-16<br>
            Due: 2026-03-16
        </div>
    </div>
    <div class="grid">
        <section>
            <strong>Bill To</strong><br>
            Jane Smith<br>
            456 Oak Avenue<br>
            Springfield, IL 62704
        </section>
        <section>
            <strong>Ship To</strong><br>
            Jane Smith<br>
            789 Elm Street<br>
            Springfield, IL 62704
        </section>
    </div>
    <table>
        <thead>
            <tr>
                <th>Item</th>
                <th>Qty</th>
                <th>Unit Price</th>
                <th>Amount</th>
            </tr>
        </thead>
        <tbody>
            <tr><td>Web Development</td><td>40 hrs</td><td>$150.00</td><td>$6,000.00</td></tr>
            <tr><td>UI/UX Design</td><td>20 hrs</td><td>$125.00</td><td>$2,500.00</td></tr>
            <tr><td>Annual Hosting</td><td>1</td><td>$1,200.00</td><td>$1,200.00</td></tr>
        </tbody>
    </table>
    <div class="total">Total: $9,700.00</div>
</body>
</html>
HTML;

HtmlRenderer::create()
    ->loadHtml($html)
    ->save('/invoices/2026-001.pdf');

メソッドチェーン

HtmlRenderer のすべてのセッターは $this を返すため、フルーエントビルダーパターンが可能です。

php
use Yeeefang\TcpdfNext\Artisan\HtmlRenderer;
use Yeeefang\TcpdfNext\Artisan\RenderOptions;
use Yeeefang\TcpdfNext\Artisan\StyleInjector;

HtmlRenderer::create()
    ->loadFile('/templates/report.html')
    ->withOptions(
        RenderOptions::create()
            ->setPageSize('A4')
            ->setLandscape(false)
            ->setMargins(top: 15, right: 10, bottom: 15, left: 10)
            ->setPrintBackground(true)
    )
    ->withStyleInjector(
        StyleInjector::create()
            ->addCss('body { font-size: 12pt; }')
    )
    ->save('/output/styled-report.pdf');

エラーハンドリング

php
use Yeeefang\TcpdfNext\Artisan\HtmlRenderer;
use Yeeefang\TcpdfNext\Artisan\Exceptions\RenderException;
use Yeeefang\TcpdfNext\Artisan\Exceptions\ChromeNotFoundException;
use Yeeefang\TcpdfNext\Artisan\Exceptions\TimeoutException;

try {
    HtmlRenderer::create()
        ->loadUrl('https://example.com/slow-report')
        ->save('/output/report.pdf');
} catch (ChromeNotFoundException $e) {
    // Chromeバイナリが見つからない -- CHROME_PATHを確認
    logger()->error('Chrome not installed: ' . $e->getMessage());
} catch (TimeoutException $e) {
    // ページの読み込みまたはレンダリングに時間がかかりすぎた
    logger()->warning('Render timed out: ' . $e->getMessage());
} catch (RenderException $e) {
    // その他のレンダリング失敗
    logger()->error('Render failed: ' . $e->getMessage());
}

次のステップ

LGPL-3.0-or-later ライセンスの下で公開されています。