Insert mpdf TOC with page number for dynamic data PHP - php

I am using mpdf to create PDF files from data extracted from a database. There are three sections in pdf which i need to display in TOC with their page number on which they are appearing. They can appear on separate page or on a single page as well when data is too short. Below is my code currently using:
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetFont('avenirnext');
$mpdf->SetTitle($title);
$mpdf->SetAuthor('Blavatnik');
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML($stylesheet2, 1);
$mpdf->WriteHTML($stylesheet3, 1);
$mpdf->setAutoTopMargin = 'stretch';
$mpdf->SetHtmlHeader($header, 'OE');
$mpdf->SetHTMLFooter($footer, 'OE');
$mpdf->AddPage('', // L - landscape, P - portrait
'', '', '', '', 20, // margin_left
20, // margin right
5, // margin top
25, // margin bottom
10, // margin header
10); // margin footer
$mpdf->WriteHTML($html);
$mpdf->Output($title . '.pdf', 'd');
I need to display TOC on first page and it also has some designing so I think it has to be done via html as displayed in example below:
https://mpdf.github.io/what-else-can-i-do/table-of-contents.html

Related

How do I center text horizontally within an FPDF generated PDF file

I am using FPDI/FPDF to dynamically add text to a PDF file. I would like for the text to be centered horizontally on the PDF regardless of how long the text is, but I am not sure how to set this using $pdf->SetXY
Here is what I have so far:
<?php
use setasign\Fpdi\Fpdi;
require_once('fpdf.php');
require_once('autoload.php');
// initiate FPDI
$pdf = new Fpdi();
$pdf->AddPage();
$pdf->setSourceFile('test2.pdf');
$tplIdx = $pdf->importPage(1);
$size = $pdf->getTemplateSize($tplIdx);
$pdf->useTemplate($tplIdx, null, null, $size['width'], $size['height'],FALSE);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(0, 0, 0);
// Here's what I have tried
$pdf->SetXY($size['width']/2, 100);
//
$pdf->Write(0, 'Lorem Ipsum');
$pdf->Output();
I tried to use the width of the imported PDF divided by 2 as the X position, however this makes it so that the text starts at that point rather than being centered on it.

dompdf - text on a single page / page_script doesnt work

I'm using dompdf 0.7.0 and try to write down text in PHP on my pdf after rendering. I need text on specific pages and found the following from Answer from Brian
DomPDF {PAGE_NUM} not on first page
The function page_script sound like the correct answer.
I could check if loop is currently on page 3 or whatever.
Does I have to enable any options for this function?
Example:
$dompdf = new Dompdf( $options );
$dompdf->set_option('default_font', 'open sans');
$dompdf->set_option('fontHeightRatio', 1);
$dompdf->setPaper('A4')
$dompdf->set_option('enable_html5_parser', true);
$dompdf->set_option('enable_php', true);
$dompdf->loadHtml( $html );
$dompdf->render();
$canvas = $dompdf->get_canvas();
$canvas->page_script('
if ($PAGE_NUM > 1) {
$current_page = $PAGE_NUM-1;
$total_pages = $PAGE_COUNT-1;
$canvas->text(0, 0, "$current_page / $total_pages", "open sans condensed", 10, array(0,0,0));
}
');
It still be shown on my first page.
$canvas wouldn't be available from the page script as it is out of scope. The canvas object can be referenced inside your page script as $pdf.
Try the following page_script call instead:
$canvas->page_script('
if ($PAGE_NUM > 1) {
$current_page = $PAGE_NUM-1;
$total_pages = $PAGE_COUNT-1;
$font = $fontMetrics->getFont("open sans condensed", "normal"); // or bold, italic, or bold_italic
$pdf->text(0, 0, "$current_page / $total_pages", $font, 10, array(0,0,0));
}
};
There are a few variables available from within page scripts or embedded script:
$PAGE_NUM: current page number
$PAGE_COUNT: total number of pages
$pdf: the canvas object
$fontMetrics: an instance of the FontMetrics class
If you're using page text you have access to the following template variables:
{PAGE_NUM}: current page number
{PAGE_COUNT}: total number of pages
Note: Support for parsing embedded script or page scripts is disabled by default. Enable it with the following command: $dompdf->set_option("isPhpEnabled", true);. This must be done prior to calling $dompdf->render();.

How to delete text from PDF using FPDI/ FPDF

I Want to edit PDF in php using FPDI/FPDF.I want to replace particular text.I have try many solution but they are not giving the desired result.All are writing some new text in new position. I want to search some text and replace that text with a new text.Is This Possible?If yes please explain.Code:
require_once('fpdf.php');
require_once('fpdi.php');
$pdf =& new FPDI();
$pdf->AddPage();
//Set the source PDF file
$pagecount = $pdf->setSourceFile("test.pdf");
//Import the first page of the file
$tpl = $pdf->importPage(1);
//Use this page as template
$pdf->useTemplate($tpl);
//Go vertical position
$pdf->SetY(15);
//Select Arial italic 8
$pdf->SetFont('Arial','I',8);
//Print centered cell with a text in it
$pdf->Cell(0, 10, "Hello World", 0, 0, 'C');
//want something like this
$pdf->replace("old_text","new_text");
$pdf->Output("my_modified_pdf.pdf", "F");
It is not possible to edit a PDF document with FPDI and also it is not possible to replace text of an imported PDF page with FPDI.

Why lines/images draw only on last page of pdf using dompdf in php

I am trying to draw line/image on every page of pdf using dompdf but it starts from second page, why this is so ? anyone has any idea ?
Here is my code
$dompdf = new DOMPDF();
$dompdf->load_html($message2);
$dompdf->set_paper('a4','portrait');
$dompdf->render();
$canvas = $dompdf->get_canvas();
//For Header
$header = $canvas->open_object();
$canvas->image($header_image1,'jpg',0, 0, 595, 100);
$canvas->line(0,100,595,100,array(0,0,0),1);
$canvas->close_object();
$canvas->add_object($header, "all");
//For Footer
$footer = $canvas->open_object();
$canvas->line(0,740,650,740,array(0,0,0),1);
$canvas->image($footer_image1,'jpg',0, 742, 595, 100);
$canvas->close_object();
$canvas->add_object($footer, "all");
$output = $dompdf->output();
this code draw line/image on pdf but it only display on last page.
I have two pages in pdf and my line/images are drawn on second page not on first page.
Please suggest any solution.
Adding objects in DomPDF works from the current page onwards. In other words, your objects will get added, but only from the page you currently have and then onwards to any new pages you add.
In your code, you've already converted your HTML to PDF, so the current page is more than likely the last page in your document. So your header / footer are added there, but not to any previous pages.
To place content on every page, domPDF provides two methods: page_text and page_script.
In your case the following type of code should do the trick:
$canvas->page_script('
$pdf->line(10,730,800,730,array(0,0,0),1);
');
Code in the page_script function is then executed for every PDF page.
The line is not displayed for me because embedded php was disabled.
This solved the problem:
$dompdf->set_option("isPhpEnabled", true);
This line has to be placed before $dompdf->render().
how to add image in all pdf page using header and footer.below is my code.
$pdf = App::make('dompdf');
$pdf->loadFile('invoice.html');
$pdf->output();
$dom_pdf = $pdf->getDomPDF();
$canvas = $dom_pdf ->get_canvas();
$image1="logo.png";
$canvas->image($image1,'png', 0, 0, 50, 25);
$canvas->page_text(10, 10, "Page {PAGE_NUM} of {PAGE_COUNT}", null, 10, array(0, 0, 0));
$pdf->save('pdf_report/eft_payment-'.$RandomAccountNumber.'.pdf');

FPDF Get page numbers at footer on Every A4 size page

I am creating PDF reports using FPDF. Now how do I generate page numbers on each page of a report at the bottom of the page.
Below is the sample code for generating a 2 page PDF.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','',16);
$start_x=$pdf->GetX();
$current_y = $pdf->GetY();
$current_x = $pdf->GetX();
$cell_width = 25; $cell_height=14;
$j = 20; // This value will be coming from Database so we dont know how many pages the report is going to be
for ($i = 0; $i<$j ; $i++){
$pdf->MultiCell($cell_width,$cell_height,'Hello1',1);
$current_x+=$cell_width;
$pdf->Ln();
}
$pdf->Output();
?>
Note : The $j value will be coming from the database so we don't know how many pages is the report going to be.
To add an A4 page, with portrait orientation, do:
$pdf->AddPage("P","A4");
Create a new class which extends the FPDF class, and override the pre-defined Footer method.
Example:
class PDF extends FPDF
{
function Footer()
{
// Go to 1.5 cm from bottom
$this->SetY(-15);
// Select Arial italic 8
$this->SetFont('Arial','I',8);
// Print centered page number
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
}
}
According to my comment you can place
$pdf->PageNo();
on your page where ever you like. Also you can add a placeholder to this
$pdf->AliasNbPages();
What would look like
$pdf->AliasNbPages('{totalPages}');
By default it's {nb}. It's not necessary to add a placeholder
Than you could add the pagesum like
$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{totalPages}", 0, 1);
or without your own placeholder
$pdf->Cell(0, 5, "Page " . $pdf->PageNo() . "/{nb}", 0, 1);
this would produce e.g.
Page 1/10
in case there were 10 pages :)
But beware
Using the placeholder will mess up the width of the cell. So if you have e.g. 180 page-width than 90 isn't the mid anymore (In the line where you use the placeholder). You will see if you try :)

Categories