Dynamic Footer MPDF - php

I'm using mdpf and php to generate a PDF. I need to create a different footer every time I use the tag pagebreak.
My code is something like this (and it's not working this way)
$mpdf = new mPDF('c', 'A4');
$mpdf->SetHTMLFooter('First Article','O');
$html = 'Lots of text';
.
.
.
$html .= "pagebreak"; (this is a html tag)
$html .= 'More lots of text';
$mpdf->SetHTMLFooter('Second Article','O');
$mpdf->WriteHTML($html);
print $mpdf->Output();
How could I do that?

You need to flush the content with WriteHTML, then set a new footer.
$mpdf = new mPDF('c', 'A4');
$mpdf->SetHTMLFooter('First Article','O');
$html = 'Lots of text';
.
.
.
$html .= "pagebreak"; (this is a html tag)
$mpdf->WriteHTML($html); //flush
$html .= 'More lots of text';
$mpdf->SetHTMLFooter('Second Article','O');
$mpdf->WriteHTML($html);
print $mpdf->Output();

Related

How generate n pages whith dompdf PHP [duplicate]

<?php
require_once 'classes/postgredb.class.php';
require_once 'include/functions.php';
require_once("/tools/dompdf/dompdf_config.inc.php");
$con=new PostgreDB();
ob_start();
$html =
'<html><body>'.
'<p>Hello World!</p>'.
'<div style="page-break-after: always;"></div>'.
'</body></html>';
for($i=0;$i<5;$i++)
{
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Admit card.pdf",array("Attachment"=>0));
}
?>
I want to print the "Hello World" in each page,but my code is printing it in only one page.
How do i print "Hello World" in each page using a loop.Please help me.
Try this:
Your loop is wrong. The way you add pages to your PDF is probably wrong. Apparently you are overwriting one page again and again instead of attaching a new one.
$html = <<<HTML
<html>
<head>
<style type="text/css">
/* Your document styling goes here */
</style>
</head>
<body>
HTML;
for($i=0;$i<5;$i++)
{
$html .= '<div style="page-break-after: always;"><p>Hello World!</p></div>';
}
$html .= '</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Admit card.pdf",array("Attachment"=>0));
$dompdf->clear();
NOTE: You need to make sure, is that heredoc closer HTML is in a new line and not indented.

Make part of PDF start on odd or even page in Dompdf

I convert html to pdf.
Is it possible to make a part of the PDF start only on an odd or even page, for example, using the page-break-after parameter?
Thanks!
This is my solution that works for me.
This allows new pages to be output from odd.
If you need output new pages from even, change '$PAGE_NUM%2 != 0' to '$PAGE_NUM%2 == 0'
$html ='<body>';
$html .='here is my html';
$html.='<script type="text/php">
if ( isset($pdf) ) {
if($PAGE_NUM%2 != 0) //add empty page if odd one
{
$pdf->new_page();
}
}
</script>';
$html.='<div style="page-break-before: always;"></div>'; //start new page
$html .='this is my html which will start at odd page';
$html .='</body>';
$options = new Options();
$options->setIsRemoteEnabled(true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);
$dompdf->set_option("isPhpEnabled", true);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();

mpdf after break page next page header text should be change

hello please check pdf sample,
I'm using mpdf to generate a pdf document in php.
I got this pdf generated by the code below.
1st copy is perfect, but second copy of 1st page top header title show wrong i want to 2nd copy on header $title2
Can you help me?
<?php
include('mPDF/mpdf.php');
$mpdf = new mPDF('c', 'A4', '9', '', '10', '18', '55', '50', '50', '12', 'P');
$html = '
<html>
<head>
</head>
<body>';
for ($i = 1; $i < 80; $i++) {
$html .= '<div>Here is the text of the first chapter</div>';
}
$html .= '</body>
</html>';
$title1 = "<p style='text-align:right; margin:0px; font-size:15px;'>ORIGINAL </p>";
$title2 = "<p style='text-align:right; margin:0px; font-size:15px;'>DUPLICATE</p>";
$title3 = "<p style='text-align:right; margin:0px; font-size:15px;'>TRIPLICATE</p>";
$mpdf->SetHeader($title1 . $header);
$mpdf->SetFooter($footer . 'Page No. {PAGENO} of {nbpg}');
$mpdf->WriteHTML($html);
$mpdf->SetFooter($footer . 'Page No. {PAGENO} of {nbpg}');
$mpdf->WriteHTML('<pagebreak resetpagenum="1" pagenumstyle="1" suppress="" />');
$mpdf->SetHeader($title2 . $header);
$mpdf->SetFooter($footer . 'Page No. {PAGENO} of {nbpg}');
$mpdf->WriteHTML($html);
$mpdf->SetFooter($footer . 'Page No. {PAGENO} of {nbpg}');
$mpdf->Output();
PDF Sample
According to the documentation the SetHeader function can take three parameters, the third of which is $write.
This third parameter forces the Header to be written immediately to the current page. Use it if the header is being set after the new page has been added.
Change to:
$mpdf->SetHeader($title2 . $header, [], true);
and it should allow the second header to be seen.
You may also want to look at AddPage().

How to use loop in creating multiple pdf file in dompdf

<?php
require_once 'classes/postgredb.class.php';
require_once 'include/functions.php';
require_once("/tools/dompdf/dompdf_config.inc.php");
$con=new PostgreDB();
ob_start();
$html =
'<html><body>'.
'<p>Hello World!</p>'.
'<div style="page-break-after: always;"></div>'.
'</body></html>';
for($i=0;$i<5;$i++)
{
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Admit card.pdf",array("Attachment"=>0));
}
?>
I want to print the "Hello World" in each page,but my code is printing it in only one page.
How do i print "Hello World" in each page using a loop.Please help me.
Try this:
Your loop is wrong. The way you add pages to your PDF is probably wrong. Apparently you are overwriting one page again and again instead of attaching a new one.
$html = <<<HTML
<html>
<head>
<style type="text/css">
/* Your document styling goes here */
</style>
</head>
<body>
HTML;
for($i=0;$i<5;$i++)
{
$html .= '<div style="page-break-after: always;"><p>Hello World!</p></div>';
}
$html .= '</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("Admit card.pdf",array("Attachment"=>0));
$dompdf->clear();
NOTE: You need to make sure, is that heredoc closer HTML is in a new line and not indented.

MPDF : independent informations in multiple pages

I actually use MPDF in order to show my HTML code on a PDF.
The problem that I have a lot of payroll employees , and I want to make each payroll on a page.
This code works well.
$html = $this->view->partial('fiche/telechargerfiche.phtml',
array('fichep' => $recuperationFiche));
$mpdf=new mPDF();
foreach ($recuperationFiche as $fiche) {
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->SetHTMLFooter("<div style='text-align: center'><img src='pieddepage.jpg' /></div>") ;
$mpdf->Output();
exit;
But the problem is that my payrolls is shown successively in the same page.
Now I want to make each payroll on an independant page .
I have to use a foreach , but I don't know where is the error , because it's shown to me the same result :
$html = $this->view->partial('fiche/telechargerfiche.phtml',
array('fichep' => $recuperationFiche));
$mpdf=new mPDF();
foreach ($recuperationFiche as $fiche) {
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->SetHTMLFooter("<div style='text-align: center'><img src='pieddepage.jpg' /></div>") ;
$mpdf->Output();
exit;
}
You need to :
Change your partial file to generate the HTML for only one payroll (fiche).
Update your code
Example
$mpdf = new mPDF();
$mpdf->SetDisplayMode('fullpage');
foreach ($recuperationFiche as $i => $fiche) {
if($i) { //If not the first payroll then add a new page
$mpdf->AddPage();
}
$html = $this->view->partial(
'fiche/telechargerfiche.phtml',
array('fichep' => $fiche)
);
$mpdf->WriteHTML($html);
}
$mpdf->SetHTMLFooter( "" );
$mpdf->Output();
exit;
Hope it helps

Categories