【问题标题】:How to add page number for every page in laravel dompdf?如何为laravel dompdf中的每一页添加页码?
【发布时间】:2017-05-07 11:16:25
【问题描述】:

我从这里得到: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>

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

有没有人可以帮助我?

【问题讨论】:

标签: php laravel pdf laravel-5.3 dompdf


【解决方案1】:

按照以下步骤来实现它:
- 从/config/dompdf.php启用DOMPDF_ENABLE_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获得更多想法

【讨论】:

【解决方案2】:

例如,您可以这样做..

在您的控制器中:

$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>

【讨论】:

  • 我可以知道什么是字体吗?
【解决方案3】:

试试这个。

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 2020-02-15
    相关资源
    最近更新 更多