dynamicpdf-plugin icon indicating copy to clipboard operation
dynamicpdf-plugin copied to clipboard

Always have even number of pages

Open jjezik82 opened this issue 11 months ago • 3 comments

Hello, I need to achieve always even number of pages. Let's say my document will have 7 pages, then I have to insert one more empty page. Is there some way to achieve this? Thanks.

jjezik82 avatar Mar 13 '24 15:03 jjezik82

Check here: https://github.com/mplodowski/dynamicpdf-plugin?tab=readme-ov-file#page-numbers

You have access to PAGE_NUM constant, so check if it is not even and add page empty page to pdf. Check dompdf documentation how to add new page.

If you figure it out, please add your solution here.

mplodowski avatar Mar 17 '24 11:03 mplodowski

Well I'm really lost here. It would be really nice, if PAGE_COUNT constant could be used in template where I could do something like: `{% if PAGE_COUNT is odd %}

{% endif %}`

I could use PAGE_COUNT variable also in other cases, like put it in table on front page, but I just have no idea how to get access to that.

jjezik82 avatar Mar 19 '24 12:03 jjezik82

Enable demo: https://github.com/mplodowski/dynamicpdf-plugin?tab=readme-ov-file#demo-examples

There is Header and Footer example there. Go to Header and Footer layout and there is a code for using page numbers.

You can use following methods to accomplish what you need:

$pdf->new_page(); // add new page

$pdf->get_page_count(); // get all page count

mplodowski avatar Mar 20 '24 09:03 mplodowski

Ok, got it working like this

`

		$font = "{{ 'plugins/renatio/dynamicpdf/assets/fonts/Montserrat-Regular.ttf'|app }}";
        $textHeight = $fontMetrics->getFontHeight($font, $size);
        $width = $fontMetrics->getTextWidth('Page 1 of 2', $font, $size);

        $foot = $pdf->open_object();

        $w = $pdf->get_width();
        $h = $pdf->get_height();

        $y = $h - $textHeight - 35;

        $pdf->close_object();
        $pdf->add_object($foot, 'all');

        $text = "Strana {PAGE_NUM} z {PAGE_COUNT}";

	    if ($PAGE_COUNT % 2 != 0) { // Check if page number is odd
	      $pdf->new_page(); // Add a new blank page
	    }
        // Center the text
        $pdf->page_text(40, $y, $text, $font, $size, $color);
        
    }
</script>`

jjezik82 avatar May 28 '24 14:05 jjezik82