How to add page numbers to a summary report (PDF)?

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
measan
User
Posts: 33

How to add page numbers to a summary report (PDF)?

Post by measan »

How to add page numbers to a summary report in a PDF report, where each page contains a page number?
Please help me, thanks.


arbei
User
Posts: 9384

Post by arbei »

  1. Read dompdf's doc on Embedded PHP support.
  2. You may try to add the the script tag (which uses the page_text() method to add page number) by Page_Exported.

measan
User
Posts: 33

Post by measan »

I am confused how to insert $PAGE_NUM and $PAGE_COUNT variables into my custom template for my summary report ?
Please help me, thanks.


arbei
User
Posts: 9384

Post by arbei »

You don't need to "insert $PAGE_NUM and $PAGE_COUNT variables" yourself, as the dompdf docs says:

The following template variables (i.e. PAGE_NUM and PAGE_COUNT) are parsed when using the Dompdf\Canvas::page_text() method. To use, surround by {curly braces}.

As the example shows, you simply use {PAGE_NUM} and {PAGE_COUNT}.

$pdf->page_text(550, $y, "Page {PAGE_NUM} of {PAGE_COUNT}", $font_bold, $size, [0, 0, 0]);

However, note that the docs says: Currently only available in the Cpdf adapter, so you can use the following fonts only: Courier, Helvetica, Times. Other fonts use TCPDF so page_text() is not supported.


measan
User
Posts: 33

Post by measan »

require_once 'autoload.inc.php';
use vendor\Dompdf\Dompdf;
<?php
$pdf = new Dompdf();
$font = $pdf->getFontMetrics()->get_font("helvetica", "bold");
$pdf->getCanvas()->page_text(500, 18, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 10, array(0,0,0));
?>
<table class="ew-table no-border">
  <tr>
    <td>
      <strong>Food Name : </strong> {{{food_nme}}}
    </td>
  </tr>
</table>
<br>
<table class="table table-bordered table-sm ew-table ew-export-table w-100">
  <!-- table.ew-table (with border) -->
  <tr>
    <th>{{{caption food_type}}}</th>
    <th>{{{caption food_price}}}</th>
    <th>{{{caption date_in}}}</th>
    <th>{{{caption long_day}}}</th>
  </tr>

I Try this code but i got an error, Please help me.
C:\xampp\htdocs\demo1\views\Report1Summary.php(482): Class "PHPMaker2024\demo1\Dompdf" not found


measan
User
Posts: 33

Post by measan »

I am confused regarding the custom template to display the header and page numbers on each page. I noticed that the PHPMaker tutorial doesn't teach how to display the header on every page and also doesn't explain how to display the page numbers. anyone please help me thanks.


arbei
User
Posts: 9384

Post by arbei »

  1. There is no such feature in PHPMaker, that's why you need to use dompdf's own feature directly,
  2. Your code is wrong, the first two lines should be after <?php,
  3. If you try to add a standalone .php script yourself (to test), you need to include the vendor/autoload.php, there is no autoload.inc.php.

measan
User
Posts: 33

Post by measan »

I fix my code but still got an error :
C:\xampp\htdocs\demo1\views\Report1Summary.php(361): syntax error, unexpected token "use"
this error is "use Dompdf\Dompdf;" what should i do ? i really confused?

<?php
require 'vendor/autoload.php';
use Dompdf\Dompdf;

$pdf = new Dompdf();
$font = $pdf->getFontMetrics()->get_font("helvetica", "bold");
$pdf->getCanvas()->page_text(500, 18, "Page: {PAGE_NUM} of {PAGE_COUNT}", $font, 10, array(0,0,0));
?>
<table class="ew-table no-border">
  <tr>
    <td>
      <strong>Food Name : </strong> {{{food_nme}}}
    </td>
  </tr>
</table>
<br>
<table class="table table-bordered table-sm ew-table ew-export-table w-100">
  <!-- table.ew-table (with border) -->
  <tr>
    <th>{{{caption food_type}}}</th>
    <th>{{{caption food_price}}}</th>
    <th>{{{caption date_in}}}</th>
    <th>{{{caption long_day}}}</th>
  </tr>

arbei
User
Posts: 9384

Post by arbei »

measan wrote:

C:\xampp\htdocs\demo1\views\Report1Summary.php(361): syntax error, unexpected token "use"

If you tried to insert your posted code into Page_Exported server event, it won't work. Server events are methods in the page class, you cannot enter code like a standalone script, you should see the example in the docs AND the example of dompdf, and try to add the code from the dompdf example into the Page_Exported server event (by appending to $doc->Text).


measan
User
Posts: 33

Post by measan »

I've tried every method to add page numbers to the summary report, but I haven't been successful except for creating my own PHP program using FPDF or DomPDF. I prefer using FPDF because it has more comprehensive features; it allows me to adjust the position of columns and rows, something that cannot be done in DomPDF. The built-in Summary report in PHPMaker still cannot do it even when using CustomTemplateHeader. Ideally, there should be an option during the design of the summary report to enable page numbers, perhaps it will be added in the next PHPMaker version, hoping for further improvements.


arbei
User
Posts: 9384

Post by arbei »

arbei wrote:

you should see the example in the docs AND the example of dompdf, and try to add the code from the dompdf example into the Page_Exported server event (by appending to $doc->Text).

If you read the examples in above and try to put them together, it is simply:

function Page_Exported($doc) {
    if ($this->isExport("pdf")) {
        $doc->Text .= '<script type="text/php">...your code...</script>';
        // Log($doc->Text); // Log the whole export document for debugging
    }
}

measan
User
Posts: 33

Post by measan »

where can I see a sample document AND a sample dompdf please advise thank you.


Post Reply