Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add two dynamic footers in laravel dompdf? #3016

Open
SumerSingh4594 opened this issue Sep 29, 2022 · 2 comments
Open

How to add two dynamic footers in laravel dompdf? #3016

SumerSingh4594 opened this issue Sep 29, 2022 · 2 comments
Labels

Comments

@SumerSingh4594
Copy link

SumerSingh4594 commented Sep 29, 2022

I had added a dynamic footer text in ten pages single PDF file but now I am stuck in adding two different footers for different pages in the barryvdh/laravel-dompdf composer package in laravel.

I have to add two different footer texts for two different pages in a ten pages single pdf file generated by the above package.

For example:-

Page_1 => footer_text_1 with page_number_1
Page_2 => footer_text_1 with page_number_2
Page_3 => footer_text_1 with page_number_3
Page_4 => footer_text_2 with page_number_1 (again)
Page_5 => footer_text_2 with page_number_2

Please help me out as it's my client's requirement. Below I am sharing my pdf-generating code.

Here is my controller code:-

$pdf = PDF::loadView($viewPath, [
                    'pdf' => true,
                    'key1' => 'value1',
                    'key2' => 'value2'
                ])->setOptions(['defaultFont' => 'sans-serif', 'enable_php' => true])->setPaper('a4', 'landscape')->save('' . $path . '/' . $fileName . '.pdf');
return $pdf->download($fileName . '.pdf');

Here is my blade file code for generating one footer text with dynamic page numbers for every single page:-

At the top of the body tag-

<footer> Footer text here... </footer>

At the bottom of the body tag-

<script>
if (isset($pdf)) {
    $font = null;
    $pdf->page_text(295, 810, "{PAGE_NUM}", $font, 10, array(0,0,0));
}
</script>

Any help would be appreciable.

Thank you in advance!!

@markkimsal
Copy link
Contributor

It might be easier to render each page as an individual PDF then concatenate them together. I use FPDI library and import the pages from the files generated.

I've also used ghostscript (gs) to concatenate multiple PDF renderings into 1 document.

'gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dAutoRotatePages=/None -sOutputFile=' . $destination . ' ' . implode(' ', $pdf_parts)

@bsweeney
Copy link
Member

Thanks @markkimsal I think that's probably the best option. I was trying to work out a method using HTML+CSS plus some callback logic but there are challenges there that I hadn't yet found a way to overcome.

Ultimately something like this would be made possible by adding named page support, but nothing planned for that functionality yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants