I make a php script which gets an url of a website and convert the website to PDF. This works fine with MPDF version 5.7. I want to upgrade my MPDf to version 6.1 because the version interprets the <li> tag better. I update my fonts and now if i try to execute the script I get an empty Page.
New Code:
ob_start();
include(substr($url, 1, strlen($url)-1));
$html=ob_get_contents();
ob_end_clean();
require_once("mpdf61/mpdf.php");
$mpdf = new mPDF('', 'A4', 8,'roboto');
$mpdf->SetFooter('W&T||Seite {PAGENO}');
$mpdf->SetHTMLHeader('<div style="text-align: right;" ><img style="height:50px;" src="/pics/head/e-logow-01-grww-000.png " ></div>');
$mpdf->SetAutoPageBreak(true , 15);
$mpdf->SetTopMargin(30);
$mpdf->AddPage();
$mpdf->list_indent_first_level = 0;
$mpdf->WriteHTML($html);
$mpdf->Output();
Old code:
ob_start();
include(substr($url, 1, strlen($url)-1));
$html=ob_get_contents();
ob_end_clean();
require_once("mpdf57/mpdf.php");
$mpdf = new mPDF('', 'A4', 8,'roboto');
$mpdf->SetFooter('W&T||Seite {PAGENO}');
$mpdf->SetHTMLHeader('<div style="text-align: right;" ><img style="height:50px;" src="/pics/head/e-logow-01-grww-000.png " ></div>');
$mpdf->SetAutoPageBreak(true , 15);
$mpdf->SetTopMargin(30);
$mpdf->AddPage();
$mpdf->list_indent_first_level = 0;
$mpdf->WriteHTML($html);
$mpdf->Output();
it is the same code code with another libary version.
You have to check your PHP version. Check that mpdf is supported or not to your current PHP version.
Look into this change log file and check If any deprecated function you used or not.
https://github.com/mpdf/mpdf/blob/v6.1.0/CHANGELOG.txt
Related
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
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();
I have a problem with the mpdf library.
When I insert a img tag the library I received error 500.
This is my code:
<?php
$tabla = $this->load->view("prueba",$data , true);
$this->load->library('mpdf');
//size
$mpdf = new mPDF('utf-8','Letter',0,0,10.1,10.1,32,29,13.2,13.2);
$mpdf->showImageErrors = true;
$stylesheet = file_get_contents('assets/css/bootstrap/css/bootstrap.min.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->SetHTMLHeader('<div class="cabecera"> <img src="assets/img/asd.png"></div>');
$mpdf-> WriteHTML ($tabla,2);
$mpdf-> SetHTMLFooter('<div class="footer"> <span> some text!</span></div>');
$mpdf->Output('files/daniel.pdf','F');
$this->response("True", 200);
?>
If I do that even without the tag IMG everything works fine.
Help me please.
with mpdf I use base64 and it works well. Like:
<img alt="logo" width="298" height="108" src="data:image/png;base64,{base64 of the image}>" />
To get convert a picture in base64 do:
base64 filename.png
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
I'd installed the MPDF utility in order to convert HTML&CSS to PDF reports.
So far things have been working just fine, until I've tried converting certain page to PDF ,and there's no output.
I have to mention that i'm able to display the page regularly through browser - the problem only comes up when i'm trying to convert it to PDF - then I receive blank page. Moreover, there are no encoding problems (part of the output is written in Hebrew, but I've already overcame this obstacle)
Here's part of the code :
if($customer!=$tempCustomer)
{
if($tempCustomer!="")
{
$html.=("</table>");
$html.=("</BR>סהכ".$sumTotal."</BR>");
$html.=("</BR>משטחים".$sumPallets."</BR>");
}
$sumTotal=0; //RESET SUM OF EACH CUSTOMER
$sumPallets=0; //RESET PALLETS COUNT
$html.=("</div>");
$html.=("<div class='subTable'>");
// $html.=("לקוח: ".$customerName."</br>");
$sumTotal=0;
$sumPallets=0;
$tempCustomer=$customer;
$html.=("<table border='3'
<tr><td>מגדל</td><td>תאריך</td><td>תעודה</td><td>פריט</td><td>סוג</td><td>גודל</td><td>כמות</td><td>משקל</td><td>מחיר
מכירה</td><td>סכום</td><td>משטחים</td></tr>");
$html.=("<tr>");
$html.=("<td>".$grower."</td>");
$html.=("<td>".$date."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$item."</td>");
$html.=("<td>".$type."</td>");
$html.=("<td>".$size."</td>");
$html.=("<td>".$quantity."</td>");
$html.=("<td>".$weight."</td>");
$html.=("<td>".$price."</td>");
$html.=("<td>".$total."</td>");
$html.=("<td>".$pallet."</td>");
$html.=("</tr>");
$sumTotal+=$total;
$sumPallets+=$pallet;
}
else
{
$html.=("<tr>");
$html.=("<td>".$grower."</td>");
$html.=("<td>".$date."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$item."</td>");
$html.=("<td>".$type."</td>");
$html.=("<td>".$size."</td>");
$html.=("<td>".$quantity."</td>");
$html.=("<td>".$weight."</td>");
$html.=("<td>".$price."</td>");
$html.=("<td>".$total."</td>");
$html.=("<td>".$pallet."</td>");
$html.=("</tr>");
$sumTotal+=$total;
$sumPallets+=$pallet;
}
/*
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("<td>".$form."</td>");
$html.=("</tr>");
*/
}
$html2='אבדרכדכגכגכגכג';
$html3='אבדרכדכגכגכגכג';
//==============================================================
//MPDF SETTINGS - CONTINUE
$mpdf->SetAutoFont();
$mpdf->autoFontGroupSize = 1;
$mpdf->SetDirectionality('rtl');
$mpdf->useLang = true;
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
Any suggestions?
Thanks in advance
Have you tried debugging it? Per mpdf's site: If you get nothing but a blank screen on your browser, it may be because there is a script error. Turn on debugging at the start of your script.
<?php
include("../mpdf.php");
$mpdf=new mPDF();
$mpdf->debug = true;
$mpdf->WriteHTML("Hallo World");
$mpdf->Output();
?>
If the above works, then it's something with your code. Sometime, even a single space before any html output can throw off MPDF
First of all, If you get nothing but a blank screen on your browser, it may be because there is a script error. Turn on debugging at the start of your script.
// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';
try {
$mpdf = new \Mpdf\Mpdf();
$mpdf->debug = true;
$mpdf->WriteHTML("Hello World");
$mpdf->Output();
} catch (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception
// name used for catch
// Process the exception, log, print etc.
echo $e->getMessage();
}
After that if there is any error like
Data has already been sent to output, unable to output PDF file
This means before creating pdf with mPDF some data is stored in the buffer which is sended to the browser. Therefore it is unable to create PDF.
Just do this..
Add this below php built-in function at the first line of your page were you are preparing data for pdf.
op_start();
And add this php built-in function before mPDF code (before where you are calling mpdf)
ob_end_flush();
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();
So that it will clear all buffer output before processing mPDF.
Make sure if you use any functions then keep it in the same page.
<?php
include_once("mpdf-master/mpdf.php");
include_once('../../../../wp-load.php');
$htm = get_template_directory_uri().'/admin/_invoice.php?id='.$_GET['id'];
$html = file_get_contents("$htm");
$mpdf=new mPDF('c');
$mpdf->WriteHTML($html);
$mpdf->Output();
?>
You can try link this.but you will get seconde page blank but in first page you will get the detail.
Thanks
Sanket.