MPDF : independent informations in multiple pages - php

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

Related

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();

Setting Cover page and Last page with mPDF

I'm attempting to Upload a PDF as a template for the cover page, then add html for the middle pages, then add a PDF as a template again for the final page.
Here is my code right now:
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4']);
$mpdf->SetDisplayMode('fullpage');
$mpdf->enableImports = true;
$mpdf->debug = true;
$mpdf->SetImportUse();
//Set Cover Page Template
$pagecount = $mpdf->SetSourceFile('site/themes/raven/pdf/cover-page.pdf');
$tplId = $mpdf->ImportPage($pagecount);
$actualsize = $mpdf->SetPageTemplate($tplId);
// Add First page
$mpdf->AddPage();
//Write Content on Inside Pages
$html= (string) get_content("/print-menu");
$mpdf->AddPage();
$mpdf->WriteHTML($html);
//Set Last Page Template
$pagecount2 = $mpdf->SetSourceFile('site/themes/raven/pdf/last-page.pdf');
$tplId2 = $mpdf->ImportPage($pagecount2);
$actualsize2 = $mpdf->SetPageTemplate($tplId2);
//Add Last Page
$mpdf->AddPage();
$mpdf->Output();
I've tried UseTemplate() and UsePageTemplate() and tried changing the order of operations. It works on the first page, however the last page appears as a blank page.
I had the same problem.
In my case, the solution was to set the background-color to transparent, because there was set a background-color in the content. So the background will be rendered over the last page

How to delete blank page in mPdf

I am converting an html file to PDF by using mPdf, but the returned PDF file includes blank pages. I want to delete those blank pages.
Had the same problem and realized that my CSS was forcing these page breaks. Make sure you don't have this in your CSS:
page-break-after: always;
It may be for many reasons:
1) Make sure the elements doesn't have unnecessary margins nor Paddings
2) Configure correctly the page properties (specially margins) :
$page_orientation = 0;
$page_size = 'Letter';
$page_margins = array('LEFT(int)','RIGHT(int)','UP(int)','BOTTOM(int)');
$pdf_output = 'I';
$css_files = array(
'path/file.css',
'path/file_2.css',
);
$orientationPage = array('','-L');
/* ===== [ MPDF ] =====*/
$mpdf=new mPDF('utf-8', $page_size.$orientationPage[$page_orientation],'','',$page_margins[0],$page_margins[1],$page_margins[2],$page_margins[3]);
// Loading CSS
for($i = 0; $i < count($css_files); $i++){
$stylesheet = file_get_contents('../../'.$css_files[$i]);
$mpdf->WriteHTML($stylesheet,1); // 1 para indicar que es CSS
}
// Set header & Footer (This are variables with html code)
$mpdf->SetHTMLHeader($header);
$mpdf->SetHTMLFooter($footer);
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetTitle($title);
$mpdf->WriteHTML($html); // <-- insert HTML
// Create PDF
$mpdf->Output($titulo.'.pdf',$pdf_output);
3) Make sure you haven't unnecessary "Page Breaks" on the HTML
<pagebreak type="NEXT-ODD" resetpagenum="1" pagenumstyle="i" suppress="off" />
I hope it can help you!
I had the same problem and i fixed it by removing the AddPage property from my code
I changed the following code
// Code with bug
$mpdf = new mPDF('utf-8', array(380,500));
$mpdf->WriteHTML($htmlContent);
$mpdf->AddPage('P'); // It will add extra page - I that i removed this line
$mpdf->Output();
Into this
// code after resolving the bug
$mpdf = new mPDF('utf-8', array(380,500));
$mpdf->WriteHTML($htmlContent);
$mpdf->Output();

Dynamic Footer MPDF

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();

a href is not working for dynamic url in mpdf

I am getting problem in anchor tag while export to PDF.Anchor tag is working for static url like http:google.com but it is not working for dynamic url.I am using mpdf module for PDF.
$url = $fullBaseUrl.'/designers/attachment/time/'.$value['filetime'].'/uploadTab/imgattach';
// http://localhost/msme_latest/designers/attachment/time/1394432246/uploadTab/imgattach
$html= ''.$value['filename'].'';
// echo $html; die;
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
When i echo my code $html it is giving my link properly.But when i export this code in PDF it is not giving any kind of link PDF.
Any help will be appriciated.
For solve this problem you have to make just i change in mpdf.php
Actual Code is in mpdf.php on line no.20146
if(isset($vetor[1]) and $vetor[1] != '') //LINK
{
if (strpos($vetor[1],".") === false && strpos($vetor[1],"#") !== 0) //assuming every external link has a dot indicating extension (e.g: .html .txt .zip www.somewhere.com etc.)
{
//Repeated reference to same anchor?
/*
while(array_key_exists($vetor[1],$this->internallink)) $vetor[1]="#".$vetor[1];
$this->internallink[$vetor[1]] = $this->AddLink();
$vetor[1] = $this->internallink[$vetor[1]];
*/
}
$this->HREF = $vetor[1]; // HREF link style set here ******
}
and you have just comment line of code (line number:20151 to 20153)
/*
while(array_key_exists($vetor[1],$this->internallink)) $vetor[1]="#".$vetor[1];
$this->internallink[$vetor[1]] = $this->AddLink();
$vetor[1] = $this->internallink[$vetor[1]];
*/
and your pdf will accept all link include "localhost" and other external links.
use Ip address instead of localhost or use Live server url it is working for me
<?php
$fullBaseUrl = "http://127.0.0.1/meme_latest";
$url = $fullBaseUrl.'/designers/attachment/time/'.$value['filetime'].'/uploadTab/imgattach';
//(http://localhost/msme_latest/designers/attachment/time/1394432246/uploadTab/imgattach)
$html= 'Test link';
//echo $html; die;
include("../mpdf.php");
$mpdf=new mPDF();
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>

Categories