John Davidson

php - How to get page number dynamically when genration custom TOC in mpdf?

0 comments
Message:


I am trying to create custom TOC in mpdf on first page but not sure how to get the page number for dynamic content. Can anyone please help?


        $html = '<div class="frame frame-top">
<div class="mpdf_toc" id="mpdf_toc_0">
<div class="mpdf_toc_level_0">
<a class="mpdf_toc_a" href="#__mpdfinternallink_1">
<span class="mpdf_toc_t_level_0">Section 1</span>
</a>
<dottab outdent="2em" />
<a class="mpdf_toc_a" href="#__mpdfinternallink_1">
<span class="mpdf_toc_p_level_0">5</span>
</a>
</div>
<div class="mpdf_toc_level_1">
<a class="mpdf_toc_a" href="#__mpdfinternallink_2">
<span class="mpdf_toc_t_level_1">Chapter 1</span>
</a>
<dottab outdent="2em" />
<a class="mpdf_toc_a" href="#__mpdfinternallink_2">
<span class="mpdf_toc_p_level_1">5</span>
</a>
</div>
<div class="mpdf_toc_level_2">
<a class="mpdf_toc_a" href="#__mpdfinternallink_3">
<span class="mpdf_toc_t_level_2">Topic 1</span>
</a>
<dottab outdent="2em" />
<a class="mpdf_toc_a" href="#__mpdfinternallink_3">
<span class="mpdf_toc_p_level_2">5</span>
</a>
</div>
</div>


My Answer:
To dynamically get the page number for the custom TOC in mpdf, you can use the `mpdf->page` property. Here's an example of how you can modify your code to include the page number dynamically:

php
// Initialize mpdf
$mpdf = new \Mpdf\Mpdf();

// Add content to mpdf
$mpdf->WriteHTML($html);

// Get the current page number
$pageNumber = $mpdf->page;

// Update the HTML with the dynamic page number
$html = str_replace('5', '' . $pageNumber . '', $html);

// Add the updated content to mpdf
$mpdf->WriteHTML($html);

// Output the pdf
$mpdf->Output();


In this code snippet, we first initialize the mpdf object and add the initial content to it. We then use the `mpdf->page` property to get the current page number. We update the HTML content with the dynamic page number using `str_replace`, and then add the updated content to mpdf. Finally, we output the pdf with the updated custom TOC that includes the dynamic page number.

Rate this post

5 of 5 based on 1280 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us