如何在laravel dompdf中为每个页面添加页码?

时间:2016-12-21 22:15:01

标签: php laravel pdf laravel-5.3 dompdf

我从这里开始:https://github.com/barryvdh/laravel-dompdf

我的控制器是这样的:

public function listdata()
{
    $pdf=PDF::loadView('print_tests.test_pdf');
    $pdf->setPaper('L', 'landscape');
    return $pdf->stream('test_pdf.pdf');
}

我的观点是这样的:

<table>
    <tr>
        <th>header1</th>
        <th>header2</th>
        <th>header3</th>
    </tr>
    <tr>
        <td>content-row-1</td>
        <td>content-row-1</td>
        <td>content-row-1</td>
    </tr>
    <tr>
        <td>content-row-2</td>
        <td>content-row-2</td>
        <td>content-row-2</td>
    </tr>
</table>

我希望每个页面都有一个页码

有没有人可以帮助我?

3 个答案:

答案 0 :(得分:2)

按照以下步骤实现:
- 从DOMPDF_ENABLE_PHP启用/config/dompdf.php - 通过php artisan vendor:publish command发布供应商文件 - 从控制器传递$pdf个对象:
- 在视图文件中添加以下代码:

<script type="text/php">
    if ( isset($pdf) ) {
        $font = Font_Metrics::get_font("helvetica", "bold");
        $pdf->page_text(72, 18, "Header: {PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(0,0,0));
    }
</script> 

您可以从Page count and page number

获得更多想法

答案 1 :(得分:0)

例如,您可以这样做。

在您的控制器中:

$pdf = app('dompdf.wrapper');
$pdf->getDomPDF()->set_option("enable_php", true);
$data = ['title' => 'Testing Page Number In Body'];
$pdf->loadView('welcomeView', $data);

在您的welcomeView.blade.php

<html>
 <body>
   <h1> {{ $title }} </h1>

   <script type="text/php">
    if (isset($pdf)) {
        $x = 250;
        $y = 10;
        $text = "Page {PAGE_NUM} of {PAGE_COUNT}";
        $font = null;
        $size = 14;
        $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>

</body>
</html>

答案 2 :(得分:0)

尝试这个。

<style>
   .pagenum:before {
        content: counter(page);
    }
</style>
<body>

    <span class="pagenum"></span>

    <table>
        <tr>
          <th>header1</th>
          <th>header2</th>
          <th>header3</th>
     </tr>
     <tr>
         <td>content-row-1</td>
          <td>content-row-1</td>
         <td>content-row-1</td>
     </tr>
     <tr>
        <td>content-row-2</td>
        <td>content-row-2</td>
        <td>content-row-2</td>
     </tr>
    </table>
</body>