MPDF adding a header and footer to large html - php

I have a large html file.
With Mpdf, I output a pdf and it perfectly work but now I want to add header and footer to all pages.
I have only one section define by my html file.
Here mpdf is in landscape mode.
I try this :
$mpdf = new \Mpdf\Mpdf([
'mode' => 'utf-8',
'orientation' => 'L'
]);
$mpdf->SetHTMLHeader('<h1>TEST</h1>');
$mpdf->SetHTMLFooter('<h1>TEST</h1>');
$mpdf->WriteHTML($html);
$mpdf->Output($namePDF,'F');
but nothing shown in pdf file...
Can't figure why

Try this
<?php
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetHeader('First section header');
$mpdf->SetFooter('First section footer');
$mpdf->WriteHTML('First section text...');
// Set the new Header before you AddPage
$mpdf->SetHeader('Second section header');
$mpdf->AddPage();
// Set the new Footer after you AddPage
$mpdf->SetFooter('Second section footer');
$mpdf->WriteHTML('Second section text...');
$mpdf->Output();

Related

Why aren't my images loading now I've updated DomPDF to 2.0.3?

I just updated from a fairly old version of DOMPDF and now can't see the images which were previously generating.
This is how I'm calling the PDF generation. It generates everything on the PDF apart from the images.
require_once 'dompdf_2-0-3/dompdf/autoload.inc.php';
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->set('isJavascriptEnabled', TRUE);
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html); // adds the html to the pdf
$dompdf->setPaper('A4', 'portrait'); // paper size and orientation
$dompdf->render(); // Render the HTML as PDF
$date = (string)date("d.m.y");
$dompdf->stream("Yandiya Calc Results ".($date));
Below is an example of where I use images in the HTML document.
<div class="Page3 page-break">
<div class="House_Back_Container"><img src="images/PDF/buildingInformation.png" class="Template"></img></div>
<div class="House_Type_Icon"><img src="images/{HOUSE_TYPE_ICON}" class="House_Type_Icon"></img></div>
<h1 class="House_Type">{HOUSE_TYPE}</h1>
<h1 class="House_Age">{HOUSE_AGE}</h1>
<h1 class="Tariff_Value">{TARIFF}</h1>
<img src="images/{LOCATION_ICON}" class="Location"></img>
</div>
Obviously, before I updated, the images were rendering with the PDF generation, so it's been confusing me as to what has changed since.
Text displayed as {EXAMPLE} is for my PHP file where the dompdf is ran to replace with variables that I send through AJAX from my client-side.

FPDF generates blank page if I try to add Header and Footer

So I have run into a bit of a problem with fpdf. I am generating a report successfully, except when I try to add a header/footer, it just displays a blank page. Not even the Pdf viewer. I have just copied the example from the tutorial on the fpdf website, and I have used $pdf-> new PDF instead of $pdf-> new FPDF, as suggested under every similar question here. But the problem persists. There seems to be another issue, that is the I also get a blank page unless I comment out the require 'fpdf/fpdf.php';. Only if it is commented out, does the pdf show.
Here's my code :
//require '/usr/share/php/fpdf/fpdf.php';
/////////ADD HEADER, FOOTER AND NEW PAGE/ MARGIN SETTING////////////
class PDF extends FPDF
{
// Page header
function Header()
{
// Logo
$this->Image("../tmp/headers/header2.",0,0,210);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(30,10,'Title',1,0,'C');
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
// Instanciation of inherited class
//$pdf = new PDF();
//$pdf->AliasNbPages();
//$pdf->AddPage();
//$pdf->Header();
//$pdf->SetFont('Times','',12);
//for($i=1;$i<=40;$i++)
// $pdf->Cell(0,10,'Printing line number '.$i,0,1);
//$pdf->Output();
//-------------------- Start of Title Page --------------------//
//start pdf file, add page, set font
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddGBFont();
$pdf->AddBig5Font();
$pdf->AddPage();
/////rest of the report /////
$pdf->Output();

Convert the same page to PDF

I want to convert the same document to PDF or I want to add post method in url just like:
$dompdf->loadHtml($_POST['name']);
Here is the code:
<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml(file_get_contents('http://localhost/));
// (Optional) Setup the paper size and orientation
$dompdf->setPaper(array(0,0,850,2250), 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
//$dompdf->stream();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codex",array("Attachment"=>0));
?>
You're missing single quote in line if file_get_contents
<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml(file_get_contents('http://localhost/'));
// (Optional) Setup the paper size and orientation
$dompdf->setPaper(array(0,0,850,2250), 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
//$dompdf->stream();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codex",array("Attachment"=>0));
?>

TCPDF does not render external CSS

I am trying to generate PDFs using TCPDF library. So far, I am able to generate the PDF files using PHP code. Although it does not render external CSS and result becomes useless as without CSS the document is not readable.
Following is my code in the controller:
public function generate_pdf() {
$this->load->model('rosters', 'roster');
$data = array();
$data['start_date'] = isset($_POST['fortnightStartDate']) ?
date("Y-m-d", strtotime(getFortnightStartDate($_POST['fortnightStartDate']))) :
getFortnightStartDate();
$cid = $_POST['cid'];
$wno = $_POST['wno'];
$data['no']=$wno;
$startDate = strtotime($data['start_date']);
$data['schedule'] = $this->roster->getShifts($cid, $data);
$data['startDate'] = $startDate;
$listing = $this->load->view('roster/roster_pdf', $data, true);
/**
* Creates an example PDF TEST document using TCPDF
* #package com.tecnick.tcpdf
* #abstract TCPDF - Example: XHTML + CSS
* #author Nicola Asuni
* #since 2010-05-25
*/
// Include the main TCPDF library (search for installation path).
require_once(APPPATH.'libraries/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 061');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 061', PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (#file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
$pdf->writeHTML($listing, true, false, true, false, '');
//Close and output PDF document
$pdf->Output('TEST.pdf', 'F');
}
I am passing the $listing variable which holds the data that needs to be included in the PDF. The data is printing fine but without CSS. If I try to include the external CSS it pops up the error.
Code to include external css
$listing. = '<style>'.file_get_contents(base_url("styles/style.css")).'</style>';
I try to include this line of code below the $listing variable but it generates following error:
A PHP Error was encountered
Severity: Warning
Message: array_merge(): Argument #2 is not an array
Filename: tcpdf/tcpdf.php
Line Number: 16375
Is there any other way I can load the external CSS ??
Any help will be highly appreciated.
After researching here and there, I come to know that TCPDF library has few limitations including CSS limitation and does not support many CSS properties. Then, I looked into dompdf library and found it useful. Surprisingly, it solved all my problem with almost similar code and also it is way easy to use than TCPDF. Following is my piece of code i wrote to solve my problem (The code is similar until the $listing variable then i simply change the library code.)
$listing .= '<style>'.file_get_contents(base_url("styles/style.css")).'</style>';
require_once(APPPATH.'libraries/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
$dompdf->load_html($listing);
$dompdf->set_paper('a4', 'landscape');
$dompdf->render();
file_put_contents('my_pdf_test.pdf', $dompdf->output());
//$dompdf->stream("dompdf_out.pdf", array("Attachment" => true));
exit(0);

Changing or eliminating Header & Footer in TCPDF

AddPage() in tcpdf automatically calls Header and Footer. How do I eliminate/override this?
Use the SetPrintHeader(false) and SetPrintFooter(false) methods before calling AddPage(). Like this:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
A nice easy way to have control over when to show the header - or bits of the header - is by extending the TCPDF class and creating your own header function like so:
class YourPDF extends TCPDF {
public function Header() {
if (count($this->pages) === 1) { // Do this only on the first page
$html .= '<p>Your header here</p>';
}
$this->writeHTML($html, true, false, false, false, '');
}
}
Naturally you can use this to return no content as well, if you'd prefer to have no header at all.
Here is an alternative way you can remove the Header and Footer:
// Remove the default header and footer
class PDF extends TCPDF {
public function Header() {
// No Header
}
public function Footer() {
// No Footer
}
}
$pdf = new PDF();
How do I eliminate/override this?
Also, Example 3 in the TCPDF docs shows how to override the header and footer with your own class.
Example:
- First page, no footer
- Second page, has footer, start with page no 1
Structure:
// First page
$pdf->startPageGroup();
$pdf->setPrintFooter(false);
$pdf->addPage();
// ... add page content here
$pdf->endPage();
// Second page
$pdf->startPageGroup();
$pdf->setPrintFooter(true);
$pdf->addPage();
// ... add page content here
$pdf->endPage();
// set default header data
$pdf->SetHeaderData('', PDF_HEADER_LOGO_WIDTH, 'marks', 'header string');
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
With the help of above functions you can change header and footer.

Categories