Created
September 12, 2016 20:36
-
-
Save niraj-shah/0d6f2e5ed84d2842e12a949c08abbe4a to your computer and use it in GitHub Desktop.
Creating a PDF using FPDF and PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// include composer packages | |
include "vendor/autoload.php"; | |
// Create new Landscape PDF | |
$pdf = new FPDI('l'); | |
// Reference the PDF you want to use (use relative path) | |
$pagecount = $pdf->setSourceFile( 'certificate.pdf' ); | |
// Import the first page from the PDF and add to dynamic PDF | |
$tpl = $pdf->importPage(1); | |
$pdf->AddPage(); | |
// Use the imported page as the template | |
$pdf->useTemplate($tpl); | |
// Set the default font to use | |
$pdf->SetFont('Helvetica'); | |
// adding a Cell using: | |
// $pdf->Cell( $width, $height, $text, $border, $fill, $align); | |
// First box - the user's Name | |
$pdf->SetFontSize('30'); // set font size | |
$pdf->SetXY(10, 89); // set the position of the box | |
$pdf->Cell(0, 10, 'Niraj Shah', 1, 0, 'C'); // add the text, align to Center of cell | |
// add the reason for certificate | |
// note the reduction in font and different box position | |
$pdf->SetFontSize('20'); | |
$pdf->SetXY(80, 105); | |
$pdf->Cell(150, 10, 'creating an awesome tutorial', 1, 0, 'C'); | |
// the day | |
$pdf->SetFontSize('20'); | |
$pdf->SetXY(118,122); | |
$pdf->Cell(20, 10, date('d'), 1, 0, 'C'); | |
// the month | |
$pdf->SetXY(160,122); | |
$pdf->Cell(30, 10, date('M'), 1, 0, 'C'); | |
// the year, aligned to Left | |
$pdf->SetXY(200,122); | |
$pdf->Cell(20, 10, date('y'), 1, 0, 'L'); | |
// render PDF to browser | |
$pdf->Output(); |
hi , i use your codes seem there's an error " Warning: include(vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\practice\admin\makecert.php on line 4
WHERE to find this?
thanks,
You need to use composer to install the library. Have a look at the tutorial that references this code: https://www.webniraj.com/2016/09/12/creating-editing-a-pdf-using-php/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi , i use your codes seem there's an error " Warning: include(vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\practice\admin\makecert.php on line 4
Warning: include(): Failed opening 'vendor/autoload.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\practice\admin\makecert.php on line 4
Fatal error: Uncaught Error: Class 'FPDI' not found in C:\xampp\htdocs\practice\admin\makecert.php:7 Stack trace: #0 {main} thrown in C:\xampp\htdocs\practice\admin\makecert.php on line 7"
WHERE to find this?
thanks,