How to get page number on dompdf PDF when using “view”

前端 未结 5 1464
南笙
南笙 2020-11-30 11:49

Ok, so I use the following snippet to get \"views\" of HTML with PHP variables loaded in as $data so that I can do things like fill in tr\'s of dat

相关标签:
5条回答
  • 2020-11-30 12:19

    Improved version of Dennis Ameling's answer to take into account…

    Translation and text centering

    Do not forget to enable PHP support like this $dompdf->set_option("isPhpEnabled", true); (or, if you're using laravel-dompdf PDF::setOptions(['isPhpEnabled' => true]);).

    <script type="text/php">
        if (isset($pdf)) {
            $pdf->page_script('
                $text = sprintf(_("Page %d/%d"),  $PAGE_NUM, $PAGE_COUNT);
                // Uncomment the following line if you use a Laravel-based i18n
                //$text = __("Page :pageNum/:pageCount", ["pageNum" => $PAGE_NUM, "pageCount" => $PAGE_COUNT]);
                $font = null;
                $size = 9;
                $color = array(0,0,0);
                $word_space = 0.0;  //  default
                $char_space = 0.0;  //  default
                $angle = 0.0;   //  default
    
                // Compute text width to center correctly
                $textWidth = $fontMetrics->getTextWidth($text, $font, $size);
    
                $x = ($pdf->get_width() - $textWidth) / 2;
                $y = $pdf->get_height() - 35;
    
                $pdf->text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
            '); // End of page_script
        }
    </script>
    

    But

    If you only need $pageNum, using a CSS counter is a way simpler approach!

    0 讨论(0)
  • 2020-11-30 12:24

    Update Regarding changes with version of dompdf >= 0.7.0
    1. Because the dompdf_config.inc.php file has been removed from this release (and is no longer referenced) all dompdf options should be set at run time.
    4. The FontMetrics class is now instantiated instead of static. To simplify migration of embedded scripts from earlier versions of dompdf we provide access to the instantiated FontMetrics class via the $fontMetrics variable. Please update your embedded scripts. For example, FontMetrics::get_font('helvetica') would now be $fontMetrics->getFont('helvetica').
    ~ Thanks to Dennis Ameling's answer for the updated information.

    Found my answer by looking over the dompdf_config.inc.php file. As it turns out, DOMPDF_ENABLE_PHP is set to false thus causing the inline php script to be ignored. I simply edited dompdf_config.custom.inc.php to the following and all is fine and working with the later code in the view.

    In dompdf/dompdf_config.custom.inc.php

    <?php
        define("DOMPDF_ENABLE_PHP", true);
    

    At Run Time

    $dompdf->set_option("isPhpEnabled", true);
    

    Then, in my html file

    <body>
        <script type="text/php">
            if ( isset($pdf) ) {
                // OLD 
                // $font = Font_Metrics::get_font("helvetica", "bold");
                // $pdf->page_text(72, 18, "{PAGE_NUM} of {PAGE_COUNT}", $font, 6, array(255,0,0));
                // v.0.7.0 and greater
                $x = 72;
                $y = 18;
                $text = "{PAGE_NUM} of {PAGE_COUNT}";
                $font = $fontMetrics->get_font("helvetica", "bold");
                $size = 6;
                $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
    

    If you go this route, don't forget to restart Apache

    0 讨论(0)
  • 2020-11-30 12:30

    I have a simple code to showing page number in footer of every page of dom pdf

    #footer { position: fixed; right: 0px; bottom: 10px; text-align: center;border-top: 1px solid black;}
            #footer .page:after { content: counter(page, decimal); }
     @page { margin: 20px 30px 40px 50px; }
    

    above code is css code

    <div id="footer">
        <p class="page">Page </p>
      </div> 
    

    You can change text position according to your requirement

    0 讨论(0)
  • 2020-11-30 12:32

    First you must enable executing inline php scripts in dompdf_config.inc.php file. (change define("DOMPDF_ENABLE_PHP", true); this line to true)

    This code sets Page indicator in right corner of the header...

        <script type="text/php">
        if ( isset($pdf) ) { 
            $pdf->page_script('
                if ($PAGE_COUNT > 1) {
                    $font = Font_Metrics::get_font("Arial, Helvetica, sans-serif", "normal");
                    $size = 12;
                    $pageText = Page . " " . $PAGE_NUM . " of " . $PAGE_COUNT;
                    $y = 15;
                    $x = 520;
                    $pdf->text($x, $y, $pageText, $font, $size);
                } 
            ');
        }
    </script> 
    

    Be sure that script tag is within body tag... otherwise it doesn't work!

    0 讨论(0)
  • 2020-11-30 12:35

    If you're using DOMPDF >= 0.7.0, the dompdf_config.inc.php file has been removed and adding a page number now requires a slightly different approach:

    Because the dompdf_config.inc.php file has been removed from this release (and is no longer referenced) all dompdf options should be set at run time.

    To enable PHP code to be executed by DOMPDF, use:

    $dompdf->set_option("isPhpEnabled", true);
    

    Also, FontMetrics should now be called by using $fontMetrics instead of Font_Metrics, so the code mentioned by @user1231342435346354 changes slightly:

    <script type="text/php">
    if ( isset($pdf) ) { 
        $pdf->page_script('
            if ($PAGE_COUNT > 1) {
                $font = $fontMetrics->get_font("Arial, Helvetica, sans-serif", "normal");
                $size = 12;
                $pageText = "Page " . $PAGE_NUM . " of " . $PAGE_COUNT;
                $y = 15;
                $x = 520;
                $pdf->text($x, $y, $pageText, $font, $size);
            } 
        ');
    }
    </script>
    
    0 讨论(0)
提交回复
热议问题