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?
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();
i'm developin an app that sholud create a pdf document with more than one page, my problem is that i found the way to add pages when needen but after the break page it doesn't write.
So the result is a pdf with 8 pages where 7 are blank and only one, the first page that in the document is the last, is correctly writed.
THIS IS MY CODE:
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Helvetica','',10);
$pdf->SetXY(20,20);
$pdf->AcceptPageBreak();
$pdf->SetAutoPageBreak(true);
//PDF content before table
$pdf->Write(14,"Resoconto dell'applicazione EBI dell'ultimo mese:");
$pdf_table=new FPDF_Table($pdf);
//$pdf_table->setDefaultCellWidth(50);
//echo "<br>";
//echo "------------------------------------------- <br>";
$i=0;
$righe=0;
while (...) {
if ($righe % 20 === 0) {
$pdf->AddPage();
$pdf->SetFont('Helvetica','',10);
$pdf->SetXY(20,20);
}
$righe++;
$pdf->Write(...);
}
How can i solve this?
Thank's
I have been testing with FPDF and it's going okay in terms of making the PDF.
In my application I plan to have a button that reads 'Convert to PDF' and when the user clicks it, FPDF will create the PDF in a new window using given variables.
The code to generate the PDF is like the below:
require_once("fpdf.php");
$author = "Test";
$title = "Candidate profile";
$width = 10;
$height = 10;
$text = "Test";
$font_family = 'Arial';
$font_size = 11;
$file_name = "candidate-profile-jesse-orange";
$extension = ".pdf";
$output = "I";
$heading_size = 16;
$subheading_size = 13;
class PDF extends FPDF
{
// Page header
function Header()
{
// Logo
$this->Image('logo.png',10,10,30);
// Arial bold 15
$this->SetFont('Arial','B',16);
// Move to the right
$this->Cell(50);
// Title
$this->Cell(100,8,'Candidate profile for: Jesse Orange',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(),0,0,'R');
}
}
// Create a new instance
$pdf = new PDF("P", "mm", "A4");
// Set some default options
$pdf->setAuthor($author);
$pdf->setTitle($title);
$pdf->AddPage();
$pdf->SetDisplayMode("real",'default');
$pdf->SetFont($font_family, '', $font_size);
// Field
$pdf->SetFont($font_family, 'B', $font_size);
$pdf->Cell(40, $height, "Position applied for:", 0, 0);
// Value
$pdf->SetFont($font_family, '', $font_size);
$pdf->Cell(60, $height, "Innovation Advisor (Job ID: 221)", 0, 0);
// Field
$pdf->SetFont($font_family, 'B', $font_size);
$pdf->Cell(40, $height, "Date of application:", 0, 0);
// Value
$pdf->SetFont($font_family, '', $font_size);
$pdf->Cell(50, $height, "17th July 2017", 0, 1);
$pdf->Output($file_name.$extension, $output);
On a separate page you have the output from a database query and variables for each field eg:
$name = "John";
$surname = "Smith";
Or more correctly...
$name = $row['name'];
My question is: should I post the already stored variables to a seperate script to generate the PDF or should it be on the same page?
Theoretically on button press you could call $pdf->Output();
Addition:
The variables displayed on the page are for viewing in the browser, then when a user would click convert to PDF it would simply grab these variables and use them with FPDF.
A similar example is how Google Analytics allows you to download a report as a PDF.
Given this flow:
Database values are displayed to end user
User reviews values and optionally generates the PDF files
... where the user is not allowed to edit the values (i.e. they aren't defaults but final ones), submitting them to the server does not make sense. The server already has direct access to such values, if you make them come from external sources they become untrusted input and you're forced to validate them.
Just make sure you have a reusable design so you don't need to implement the read values code twice.
In fact it's an anti-pattern I've actually seen in the wild now and then. In the worst case, it was a "Modify my profile" page that took user ID from the URL (rather than grabbing the ID of the currently validated user from session). Go figure the risk ;-)
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.