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();
Related
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();
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
Hi I'm using FPDF and FPDI, I'm using FPDI to concatenate several PDFs then using FPDF to fill in the information based on a form that is filled out, I've setup a SetPage method within FPDF to be able to set the page on which I'm working on, I'm able to write on the first file completely fine (first 3 pages). However, when I'm trying to write on the second file (4th and continuing pages), I use the SetXY and Write but nothing is written, I am able to add an image (barcode at the bottom of the page) but no text, any ideas as to what I'm doing wrong?
This is the code that I've got to concatenate the files:
<?php
session_start();
require_once('lib/pdf/fpdf.php');
require_once('lib/pdi/fpdi.php');
require_once('lib/barcode/class/BCGFontFile.php');
require_once('lib/barcode/class/BCGColor.php');
require_once('lib/barcode/class/BCGDrawing.php');
require_once('lib/barcode/class/BCGcode39extended.barcode.php');
$contractType = $_SESSION['addition'];
require_once('barcode.php');
if(isset($contractType))
{
$files = array('lib/blank/NDA.pdf');
if($contractType = 'artist')
{
array_push ($files,
'lib/blank/Distro.pdf',
'lib/blank/Management-Trial.pdf'
);
} else {
echo "Whoops! Something must've happened when you were filling out your contracts! Please try filling them out again. Sorry!";
}
}
$pdf = new FPDI();
foreach ($files AS $file) {
$pageCount = $pdf->setSourceFile($file);
for($n = 1; $n <= $pageCount; $n++) {
$tmpIdx = $pdf->importPage($n);
$size = $pdf->getTemplateSize($tmpIdx);
if($size['w'] > $size['h']) {
$pdf->AddPage('L', array($size['w'], $size['h']));
} else {
$pdf->AddPage('P', array($size['w'], $size['h']));
}
$pdf->useTemplate($tmpIdx);
}
}
//NDA FILLER
include('lib/filler/NDA.php');
//Distro Contract Filler
include('lib/filler/Distro.php');
//session_unset();
$pdf->Output();
?>
This is the code for filling out the first PDF (which works completely fine):
NDA.php
<?php
//ID No.
$idcoded = 'idbars/'.$_SESSION['name'].'.png';
/*
for($p = 2; $p <= $pages; $p++)
{
$pdf->Image($idcoded,0,350);
$pdf->setPage($p);
}
*/
$pdf->SetPage(1);
$pdf->Image($idcoded,0,350);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
//NDA DATE
$pdf->SetXY(51, 109.5);
$pdf->Write(0, date(d));
$pdf->SetXY(72, 109.5);
$pdf->Write(0, date(F));
//Legal Name
$pdf->SetXY(72, 114.5);
$pdf->Write(0, $_SESSION['name']);
//stage Name
$pdf->SetXY(80, 119.5);
$pdf->Write(0, $_SESSION['sname']);
$pdf->setPage(2);
$pdf->Image($idcoded,0,350);
$pdf->setPage(3);
$pdf->Image($idcoded,0,350);
$signature = 'idbars/'.$_SESSION['name'].'_sig.png';
$pdf->Image($signature,20,105,100);
?>
This is what I'm using to try to write on the second PDF, I've tried combining the NDA.php and Distro.php into one file and that makes no difference
Distro.php
<?php
$pdf->SetPage(4);
$pdf->SetXY(10,10);
$pdf->Cell(0, $_SESSION['name']);
$pdf->Write(0, $_SESSION['name']);
$pdf->Image($idcoded,0,350);
?>
The page that this is building works off of this form:
https://secure.gr8label.com/sign/artist/Dev%20Test/
FPDF "caches" the font information that is currently used. As you jump back to another page FPDF "thinks" that the font is already defined/set but in the PDF file itself it isn't. You should set your font and size in your import loop, to ensure that the font is available on all pages (I think it also could work, by defining it only on the first one).
Anyhow you should have seen that jumping between written pages results in problems and you should create a logic which creates the file from top to bottom without using things like "SetPage()" at all.
This looks like the simplest thing but I can't get it to work.
I need to add text to the first page of a multi-page pdf (could be any number of pages)
Using this code on a two page pdf (without the for loop, just using $pdf->importPage(2)) I end up with two pages but the second page is a repeat of page one. The text is written on the first page only which is good but I need all pages included in the output pdf. Here is my code
// Original file with multiple pages
$fullPathToFile = 'full/path/to/file.pdf';
class PDF extends FPDI {
var $_tplIdx;
function Header() {
global $fullPathToFile;
if (is_null($this->_tplIdx)) {
$this->setSourceFile($fullPathToFile);
$this->_tplIdx = $this->importPage(1);
}
$this->useTemplate($this->_tplIdx);
}
function Footer() {}
}
// initiate PDF
$pdf = new PDF();
$pdf->setFontSubsetting(true);
// add a page
$pdf->AddPage();
// The new content
$pdf->SetFont("helvetica", "B", 14);
$pdf->Text(10,10,'Some text here');
// How to get the number of pages of original pdf???
// $numPages = $pdf->getNumPages(???);
// Carry on adding all remaining pages starting from page 2
for($i=2;$i<=$numPages;$i++) {
// Add another page
$pdf->AddPage();
// Do I need to declare the source file here?
// $pdf->setSourceFile($fullPathToWD);
$pdf->importPage($i);
}
// Output the file as forced download
$pdf->Output('theNewFile.pdf', 'D');
Links to docs
TCPDF Classes
http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced
FPDI Classes
http://www.setasign.de/support/manuals/fpdi/
FPDF_TPL Classes
http://www.setasign.de/support/manuals/fpdf-tpl/
Solved my problem...
// Original file with multiple pages
$fullPathToFile = 'full/path/to/file.pdf';
class PDF extends FPDI {
var $_tplIdx;
function Header() {
global $fullPathToFile;
if (is_null($this->_tplIdx)) {
// THIS IS WHERE YOU GET THE NUMBER OF PAGES
$this->numPages = $this->setSourceFile($fullPathToFile);
$this->_tplIdx = $this->importPage(1);
}
$this->useTemplate($this->_tplIdx);
}
function Footer() {}
}
// initiate PDF
$pdf = new PDF();
$pdf->setFontSubsetting(true);
// add a page
$pdf->AddPage();
// The new content
$pdf->SetFont("helvetica", "B", 14);
$pdf->Text(10,10,'Some text here');
// THIS PUTS THE REMAINDER OF THE PAGES IN
if($pdf->numPages>1) {
for($i=2;$i<=$pdf->numPages;$i++) {
$pdf->endPage();
$pdf->_tplIdx = $pdf->importPage($i);
$pdf->AddPage();
}
}
// Output the file as forced download
$pdf->Output('theNewFile.pdf', 'D');
You get the number of pages by adding the first part of this line
$this->numPages = $this->setSourceFile($fullPathToFile);
And see the second last block of code - the for loop adds the remainder of the pages.
Don't know if this is how it should be done? I read in a few places that it wasn't even possible to achieve this, also the code is not supplied in the docs. However, this works, hope it helps someone.
I struggled with this a little and tried to come up with simplest way to add some text to the last page of a multi-page document. Here is the very simple code that worked for me:
require_once('fpdf/fpdf.php');
require_once('fpdf/fpdi.php');
$pdf = new FPDI();
$fullPathToPDF = '/usr/local/common/my.pdf';
$pageCount = $pdf->setSourceFile($fullPathToPDF);
for ($i = 1; $i <= $pageCount; $i++) {
$pdf->importPage($i);
$pdf->AddPage();
$pdf->useTemplate($i);
}
$pdf->SetFont('Helvetica');
$pdf->SetXY(110, 225);
$pdf->Write(8, 'A complete document imported with FPDI');
$pdf->Output($fullPathToPDF);
Just change the full path to file to a location where you have a multi-page PDF.
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
...
$pdf->SetMargins(10, 10, 10);
$pdf->SetAutoPageBreak(true, 10);
foreach($array as $item)
{
$pdf->AddPage(); //add new page for new item
$txt = some_long_long_text;
$pdf->Write(0, $txt, '', 0, 'C', true);
$pdf->endPage(); //do end of page
$pdf->lastPage(); //set cursor at last page, because autopagebreak not do it
}
In example, you have 10 pupils in array, and you need create resume for each. In exam, one resume have 3 pages. So in out u get pdf with 30 pages, with correct text.
SetAutoPageBreak(true, 10), not set cursor at last page, so you need to do it manually with function $pdf->lastPage();
that code wont work, try this:
$pdf = new PDI();
$pdf->AddPage();
$pdf->setSourceFile('zzz.pdf');
$pdf->numPages = $pdf->setSourceFile('zzz.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 20, 200);
if($pdf->numPages>1) {
for($i=2;$i<=$pdf->numPages;$i++) {
$pdf->AddPage();
$tplIdx = $pdf->importPage($i);
$pdf->useTemplate($tplIdx, 10, 20, 200);
}
}
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.