php - Laravel 5 : DOMPDF(0. 8.3) 每页页眉页脚

标签 php laravel dompdf export-to-pdf

我们使用的是 dompdf 版本 0.8.3。我们正在我们的报告中使用它,我们希望通过在每个页面的底部添加一些标题和页码来改进我们的报告。

目前,我们有页码,我们想要一个标题。

Controller

<!-- My other function/data -->
$pdf = PDF::loadView('purchase-order.export.export-pdf', $data)->setPaper('a4');
$pdf->getDomPDF()->set_option("enable_php", true);
return $pdf->stream('purchase-order-'.$purchase_order->number.'.pdf');

PDF
@extends('layouts.formpdf')

@section('content')

<div id="page-wrap">
    <script type="text/php">
        if (isset($pdf)) {
            $x = 550;
            $y = 800;
            $text = "{PAGE_NUM}";
            $font = null;
            $size = 12;
            $color = array(255,0,0);
            $word_space = 0.0;  //  default
            $char_space = 0.0;  //  default
            $angle = 0.0;   //  default
            $pdf->page_text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
        }
    </script>
.......
</div>

问题:我们如何在每个页面中添加一些标题?

最佳答案

这是我的 Controller :

    $pdf = \PDF::loadView('shipment.shipment.invoice', compact('invoice', 'invoice_taxes'));
    $output = $pdf->output();
    $pdf->save(public_path('\pdf\Invoices').'\Invoice'.$invoice->id.'.pdf');
    return $pdf->stream(public_path('\pdf\Invoices').'\Invoice'.$invoice->id.'.pdf', compact($pdf));

对于标题(在每一页上重复)。在 CSS 中添加这个
<style>
    @page { margin-top: 120px; margin-bottom: 120px}
    header { position: fixed; left: 0px; top: -90px; right: 0px; height: 150px; text-align: center; }
    #footer { position: fixed; left: 0px; bottom: -145px; right: 0px; height: 150px; }
</style>

将此添加到您的正文中(用于页码和标题)
<body>
    <script type="text/php">
        if ( isset($pdf) ) {
            $y = $pdf->get_height() - 20; 
            $x = $pdf->get_width() - 15 - 50;
            $pdf->page_text($x, $y, "Page No: {PAGE_NUM} of {PAGE_COUNT}", '', 8, array(0,0,0));
        }
    </script> 
    <header style="width: 100%; margin-top: 0px; margin-bottom: 10px;">
        <img src="{{ public_path('img/invoice_header.png') }}" alt="header" style="width: 100%;">
    </header>
    <footer style="width: 100%;" id="footer">
        <img src="{{ public_path('img/invoice_footer.png') }}" alt="footer" style="width: 100%;">
    </footer>
    <div>
         Rest of the content from here
    </div>
</body>

关于php - Laravel 5 : DOMPDF(0. 8.3) 每页页眉页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57652068/

相关文章:

php - `static` 函数内部的关键字?

php - 只有部分 PHP 表单变量被插入到 MySQL 表中

Laravel Vue SPA 与 MPA/SSR

php - 如何在 Laravel 5.3 中安装 dompdf?

php - 将变量从按钮传递到 Controller Laravel

javascript - 打印预览中不显示条形码 - javascript 打印方法

php - SQLite 数据库中的递归被锁定

php - Mysql REGEXP 搜索产品名称

php - 在子文件夹中定义 Laravel 5 路由并通过 Controller 显示它

css - 如何在 DOMPDF 生成器上显示背景图像