Adding logo image on pdf file with fpdi class - php

I managed quite easily to add an image to a pdf file with fpdi class with the script below in Laravel environment.
$pdf = new \setasign\Fpdi\Fpdi();
// set PDF file & get page count
$pageCount = $pdf->setSourceFile("MultiplePage.pdf");
// loop pages
for ($p = 1; $p <= $pageCount; $p++) {
// import page p
$tppl = $pdf->importPage($p);
// add page p
$pdf->AddPage();
// use the imported page and adjust the page size
$pdf->useTemplate($tppl, ['adjustPageSize' => true]);
// insert image at position x,y,w,h (in mm)
$pdf->Image("Logo.png",170,5,36,22);
}
// save new pdf
$pdf->Output("NewPDF.pdf", "F");
It works 100% but I struggled to find detailed info how to use each of the command and which parameter units are used (i.e. for $pdf->Image).
Can someone point to that doc please?

Related

Rescale inserted PDF using FPDI in PHP

I am creating a PDF for our clients website. The PDF is paginated for an A4 print. At the end I need to insert several existing PDFs.
Using the FPDI library works fine except when the inserted PDF is wider than the the A4 width, it doesn't rescale.
In the documentation I've found 2 examples of how to do it, none seems to deliver:
First example
$varPageId = $objPDF->ImportPage($intPageNumber);
$varTemplateSize = $objPDF->getTemplatesize($varPageId);
$objPDF->AddPage(
$varTemplateSize['orientation']
, $varTemplateSize
);
$objPDF->useImportedPage($varPageId);
Second example
$varPageId = $objPDF->ImportPage($intPageNumber);
$objPDF->AddPage();
$objPDF->useTemplate(
$varPageId
, ['adjustpageSize' => true]
);
Would anyone know how I can make sure the inserted PDF gets rescaled and all the content is displayed in the new denerated PDF?
Thanks in advance!
Works as expected. Here is a real example using following following code:
<?php
require_once 'vendor/autoload.php';
$pdf = new \setasign\Fpdi\TcpdfFpdi();
$pageCount = $pdf->setSourceFile('p.pdf');
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$pageId = $pdf->importPage($pageNo);
$s = $pdf->getTemplatesize($pageId);
$pdf->AddPage($s['orientation'], $s);
$pdf->useImportedPage($pageId);
}
$pdf->Output();

PHP Insert multiple PDF's into PDF on specific page number

My problem is - A user can upload a PDF-File and now I want to add other (between 1 and n) single page PDF's after every Page of the user uploaded PDF.
For example a user uploads a PDF with 4 Pages and I have got 3 single page PDF's on my Webserver that I want to merge with the user PDF. The resulting PDF should then look like this:
Page 1: Page #1 from User uploaded PDF
Page 2: First Single-Page-PDF from Webserver
Page 3: Page #2 from User uploaded PDF
Page 4: Second Single-Page-PDF from Webserver
Page 5: Page #3 from User uploaded PDF
Page 6: Third Single-Page-PDF from Webserver
Page 7: Page #4 from User uploaded PDF
Page 8: First Single-Page-PDF from Webserver
So far I've tried it with PDFMerger
$pdf = new PDFMerger;
$pdf->addPDF('samplepdfs/userpdf.pdf', 'all')
->addPDF('samplepdfs/one.pdf', 'all')
->addPDF('samplepdfs/two.pdf', 'all')
->addPDF('samplepdfs/three.pdf', 'all')
->merge('file', 'xxx/result.pdf');
but this only adds the other PDF's at the end..
Is there a solution in PHP where I can specify the number of the page where the other PDF's shall be inserted?
You should avoid using the PDFMerger class. It is almost 6 (!!!) years old!
You can do the same with FPDI without much effort. Anyhow you should insert the pages in the order you want them to appear. So I guess it's more a logic problem. From what you'd written in your question I assume that you want to import a whole document while spreading another document with 3 pages between the imported pages. So a simple POC could look like this:
<?php
require_once("libs/fpdf/fpdf.php");
require_once("libs/fpdi/fpdi.php");
$pdf = new FPDI();
// let's prepare the static "filler pages"
$staticIds = array();
$pageCount = $pdf->setSourceFile('pdf/on/the/webserver.pdf');
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$staticIds[$pageNumber] = $pdf->importPage($pageNumber);
}
// get the page count of the uploaded file
$pageCount = $pdf->setSourceFile('the/uploaded.pdf');
// let's track the page number for the filler page
$fillerPageCount = 1;
// import the uploaded document page by page
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$tplId = $pdf->importPage($pageNumber);
$pdf->AddPage();
$pdf->useTemplate($tplId, null, null, 0, 0, true);
// add the current filler page
$pdf->AddPage();
$pdf->useTemplate($staticIds[$fillerPageCount], null, null, 0, 0, true);
// update the filler page number or reset it
$fillerPageCount++;
if ($fillerPageCount > count($staticIds)) {
$fillerPageCount = 1;
}
}
// done
$pdf->Output('xxx/result.pdf', 'F');

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.

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 :)

Use Zend PDF to Load Multiple PDFs and Draw on Each One

I'm trying to use zend pdf to load multiple pdfs from file (each a 1 page pdf) then draw text, images etc. onto each one. Ideally the drawn text and images would be added within a function. However when I try to do some basic draw it comes back with an error in the pdf. The code works perfectly fine until I try draw text onto the page.
require_once 'zendframework/library/Zend/Loader/Autoloader.php';
include 'form-structures.php'; //
$loader = Zend_Loader_Autoloader::getInstance();
$pdfMerged = new Zend_Pdf();
//load pdf
$loadedpdf = Zend_Pdf::load("form-url.pdf");
//clone pdf
$page1 = clone $loadedpdf->pages[0];
// add text etc.
$page1->drawText('Some text...', 400, 500);
drawmore($page1);
//merge pdf
$pdfMerged->pages[] = $page1;
// do again
$loadedpdf = Zend_Pdf::load("form-url.pdf");
$page = clone $loadedpdf->pages[0];
$pdfMerged->pages[] = $page;
echo $pdfMerged->render();
The form-structure.php file is below with a function to draw something extra on the page:
function drawmore($page)
{
$page->drawText("Height is: 600px", 300,800);
return $page;
}
Resolved.
Zend PDF can't draw text on the screen unless you already specified the font.
I just added:
$page1->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 20);
and it fixed the error.

Categories