I am generating the pdf report using the mpdf and library , i wanted to get the current page number so that i can get the current page number while iam generating the pdf.
Thanks
add this to a main mPDF class:
function getPageCount() {
return count($this->pages);
}
and use something like this:
$PageCount = $this->getPageCount();
Related
I'm new to using FPDF / FPDI. My predecessor here extended the FPDF class and I'm adding onto his code to edit his PDFs and make new ones. I've been able to create the first 3 pages of a PDF I want to make by writing values from my database into blank pages, using FPDF. All good!
Now I want to import an existing single page PDF as page 4 and write some stuff on it, and it looks like I need FPDI to do that.
I used the example here: https://www.setasign.com/products/fpdi/about as suggested by another post. However, it's just adding a blank page to the end of my PDF.
I've written the path of the PDF I want to import into my log and ensured that it is correct (I'm not getting any errors in the php log either, which I would expect if the pdf was not found).
I'm initializing the pdf as FPDF, not FPDI, so that may be the issue? But if I initialize it as FPDI then I get an error that my methods are undefined, because they are defined extending the FPDF class. So I'm not sure how to do what I want...do I need to redefine my classes to extend FPDI? I'm just worried this will break the PDFs already being created using some of the same methods. I'm also not getting any errors about using FPDI methods like useImportedPage...so I feel like maybe that's not the issue?
Sorry if I'm not explaining this well, let me know if you have questions. Here is the relevant code:
require_once(APPPATH.'libraries/fpdf/fpdf.php');
require_once(APPPATH.'libraries/fpdi/autoload.php');
require_once(APPPATH.'libraries/fpdi/Fpdi.php');
public function make_fieldpacketMA(){
$plotID=$this->input->get('PlotID');
$year=$this->input->get('Year');
$pdf = new PDF('P','in','Letter');
// Make additional info first page- this works!
$additional_info=$this->fhm_model->get_additionalplotinfo($plotID, "MA");
$pdf->AdditionalInfoSheet($additional_info, $pdf);
//make plot info pages (same as subplot info pages on VT style plots)- this works!
$plotInfo=$this->fhm_model->get_sheetinfoMA($plotID,$year);
$pdf->SubplotSheet($plotInfo, "MA", $pdf);
//make seedling sheet from existing template- this does not work
$seedlingInfo=$this->fhm_model->get_seedlingInfo($plotID, $year);
$pdf->SeedlingSheet($seedlingInfo, $pdf);
//Write the output
$rand=uniqid();
$filename='./fhm_sheets/PlotPacket_'.$rand.'.pdf';
$pdf->Output($filename,"F");
$this->load->helper('download');
$data = file_get_contents($filename);
force_download($plotID.'_'.($year+1).'_FHMPlotPacket.pdf',$data);
}
//Create PDF creation class
class PDF extends PDF_Rotate{
function SeedlingSheet($seedlingInfo=array()) {
$pageCount = $this->setSourceFile(FCPATH.'fhm_template/MAFHM_Microplot_SeedlingForm.pdf');
$pageId = $this->importPage(1);
$this->AddPage();
// $this->useTemplate($pageId);
$this->useImportedPage($pageId, 10, 10, 90);
}
This works! Looks like I needed a second argument for importPage to define the bounding box
function SeedlingSheet($seedlingInfo=array()) {
$pageCount = $this->setSourceFile(FCPATH.'fhm_template/MAFHM_Microplot_SeedlingForm.pdf');
$pageId = $this->importPage(1, \setasign\Fpdi\PdfReader\PageBoundaries::MEDIA_BOX);
$this->AddPage();
$this->useTemplate($pageId, 0, 0);
}
I have many PDFs that are generated and uploaded to my server.
The problem is they contain the same page three times (3 pages in total with the same content).
My goal is to edit the PDF with PHP so that it contains only one page.
Is there any library that allows me to simply load a PDF and keep only the first page?
Thank you!
Using FPDI, you can create a function to extract the first page of a PDF file:
function first_page ($path) {
$pdf = new FPDI();
$pdf->AddPage();
$pdf->setSourceFile($path);
$pdf->useTemplate($pdf->importPage(1));
return $pdf;
}
Then output the extracted PDF as you would do with FPDF:
// Extract first page from /path/to/my.pdf
// and output it to browser with filename "MyPDF".
first_page('/path/to/my.pdf')->Output('MyPDF', 'I');
FPDF (http://www.fpdf.org/) or MDPF (http://www.mpdf1.com/mpdf/index.php) are great libraries for work with PDF files. I have experiences only with creating PDF; but I assume that one of those libraries can solve your problem.
Edit: Here is some example with FPDF
https://gist.github.com/maccath/3981205
I have a HTML document which I'm converting to PDF using TCPDF. The HTML document contains several html tables. This works fine. But, I also need to add the number of pages in the PDF doc to the first page. Is there any solution to build the PDF file and then add a page before current first page ?
I don't know if it works with HTML, but I found a solution that worked for me with movePage.
You create the page, get its position and move it to the first position :
$pdf->AddPage();
$pdf->Cell(0, 10, 'My content');
// Get the current page number
$pageNo = $pdf->PageNo();
// First page of the document
$toPage = 1;
$pdf->movePage($pageNo, $toPage);
Official example here : https://tcpdf.org/examples/example_044/
You should be able to calculate how many pages there will be based on the size of your tables. I would read in all the html data, measure it, and then start to make the PDF.
The idea I found is to clone the TCPDF instance and get the returned number of pages:
$tmp_pdf = clone $pdf;
$tmp_pdf->AddPage();
$tmp_pdf->writeHTML($report, true, false, true, false, '');
$num_pages = $tmp_pdf->getNumPages();
unset($tmp_pdf);
$report = preg_replace('#\{num_pages\}#is', $num_pages, $report);
Does anybody knows how to get the number of generated pages if a PDF document using mPDF library?
I was looking for the same functionallity while using EYiiPdf (a wrapper for mPDF on Yii) and the following worked like a charm:
$mPDF->setFooter('{PAGENO} / {nb}');
I checked mPDF's source and found this at mpdf.php:1656 (version 5.4):
function AliasNbPages($alias='{nb}') {
//Define an alias for total number of pages
$this->aliasNbPg=$alias;
}
Hope it helps!
You can use {nbpg}, like
<div align="center"><b>{PAGENO} / {nbpg}</b></div>
If you are trying to return the number of pages so you can save this to a database or some other operation outside of mpdf it's easy to pull it this way.
After you write your content:
$mpdf->WriteHTML($html);
$page_count = $mpdf -> page;
$mpdf->Output();
add this to a main mPDF class:
function getPageCount() {
return count($this->pages);
}
then add a html-parser such string:
$html = str_replace('{PAGECNT}', $this->getPageCount(), $html);
after these actions you can insert {PAGECNT} directly in your parsed HTML to get the result. This is useful is you need to indicate a page:
replacement aliases {nb} and {nbpg} for total number
{PAGENO} for current page number
UPDATE
Please note, this answer refers to mdf library v4, which was a current version at the time of writing.
Minimum Working Example by #aiao
<?php
$pagenumber= '<!--mpdf Page {PAGENO} of {nbpg}mpdf--> $mpdf->WriteHTML($pagenumber);
$mpdf->Output();
?>
<?php
$pagenumber= '<!--mpdf Page {PAGENO} of {nbpg}mpdf--> $mpdf->WriteHTML($pagenumber);
$mpdf->Output();
?>
Watch for the line:
preg_replace('/\{DATE\s+(.*?)\}/e',"date('\\1')",$hd);
in mpdf.php function Footer()
It may cause your "{PAGENO} / {nb}" to not be displayed.
Just comment it out or use strpos('{DATE' > -1) to check if it is available.
Also you may need to add:
$mpdf->ignore_invalid_utf8 = true;
and also if you don't want footer line:
$mpdf->defaultfooterline = false;
After these changes the pagination worked for me at last.
simple thing which I need to do is, that I have a pdf document and i need return number of pages of this document. I should to do this with a Zend Framework.
Thanks in advance for answers
You can get the number of pages by counting the elements of the $pages property:
$pdf = new Zend_Pdf; // load your PDF file here
$pageCount = count($pdf->pages);
You can use count($pdf->pages) to get the number of pdf pages.