Skip to content

Instantly share code, notes, and snippets.

@bhandarisaurav
Last active December 17, 2018 10:31
Show Gist options
  • Save bhandarisaurav/ed6b0c27e643ff161c3714c9ba3a1104 to your computer and use it in GitHub Desktop.
Save bhandarisaurav/ed6b0c27e643ff161c3714c9ba3a1104 to your computer and use it in GitHub Desktop.
Dynamic Certificate Generation Using FPDF and FPDI in PHP | Download the pdf Template here : https://goo.gl/ZRXtLt
<?php
use setasign\Fpdi\Fpdi;
require_once('vendor/setasign/fpdf/fpdf.php');
require_once('vendor/autoload.php');
global $pdf;
if (1) {
$countV = 1;
if ($countV == 1) {
$name = "Saurav Bhandari";
$fromDate = "October 1, 2018";
$toDate = "December 29, 2018";
$course = "Programming in JAVA";
$verification_id = "ID-20181008-001";
$course_duration = "129";
$title = "MR. ";
$trainer = "Jane Doe";
$trainer_title = "Java Trainer";
}
// initiate FPDI
$pdf = new Fpdi('L');
function setStyle($x_coordinate, $y_coordinate, $size, $font, $pdf)
{
$pdf->SetXY($x_coordinate, $y_coordinate);
$pdf->SetFont($font, "", $size);
}
function addFont($pdf)
{
$pdf->AddFont("Times-Bold", "", "Timesb.php");
$pdf->AddFont("Times", "", "Times.php");
}
//Add a blank page to the pdf
$pdf->AddPage();
//set the source file of the created pdf
try {
$pdf->setSourceFile('Deerwalk Training Certificate.pdf');
} catch (\setasign\Fpdi\PdfParser\PdfParserException $e) {
}
//extract first page from the source pdf
try {
$template = $pdf->importPage(1);
} catch (\setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException $e) {
} catch (\setasign\Fpdi\PdfParser\Filter\FilterException $e) {
} catch (\setasign\Fpdi\PdfParser\Type\PdfTypeException $e) {
} catch (\setasign\Fpdi\PdfParser\PdfParserException $e) {
} catch (\setasign\Fpdi\PdfReader\PdfReaderException $e) {
}
//now use the extracted page as the template for new psd
$pdf->useTemplate($template);
//add custom fonts
addFont($pdf);
$pdf->SetAutoPageBreak(false);
// Name Section
//----------------------------------------------------------------------------------//
//set custom style with the coordinate of text its size and font
setStyle(100, 73, 27, 'Times-Bold', $pdf);
//write the text with the height
$pdf->Write(0, $title . strtoupper($name));
//----------------------------------------------------------------------------------//
// Course Section
//----------------------------------------------------------------------------------//
$pdf->SetXY(1, 107);
$pdf->SetFont('Times-Bold', "", 20);
$pdf->Cell(0, 0, strtoupper($course), "", 2, 'C');
//----------------------------------------------------------------------------------//
// Course Duration Section
//----------------------------------------------------------------------------------//
setStyle(96.5, 126, 18, 'Times-Bold', $pdf);
$pdf->Write(0, $course_duration);
//----------------------------------------------------------------------------------//
// Date Section
//----------------------------------------------------------------------------------//
// setStyle(103.5, 141, 17, 'Times-Bold', $pdf);
// $pdf->Write(0, $fromDate);
$pdf->SetFont('Times-Bold', "", 17);
$pdf->Cell(47, 30, $fromDate, "", 2, 'C');
setStyle(165, 141, 17, 'Times-Bold', $pdf);
$pdf->Write(0, $toDate);
//----------------------------------------------------------------------------------//
// Verification ID
//----------------------------------------------------------------------------------//
$pdf->SetXY(144, -43.5);
$pdf->SetFont('Times-Bold', "", 16);
$pdf->Write(0.1, $verification_id);
//----------------------------------------------------------------------------------//
// Trainer Details
//----------------------------------------------------------------------------------//
$pdf->SetXY(-94, -12);
$pdf->SetFont('Times', "", 14);
$pdf->Cell(0, 0, strtoupper($trainer_title), "", 2, 'C');
$pdf->SetXY(-91, -18);
$pdf->SetFont('Times', "", 14);
$pdf->Cell(0, 0, strtoupper($trainer), "", 2, 'C');
//----------------------------------------------------------------------------------//
//$pdf->Output('D','Deerwalk Training Certificate.pdf');
$pdf->Output();
} else {
$url = '../file.php';
echo '<META HTTP-EQUIV=REFRESH CONTENT="0; ' . $url . '">';
}
{
"require": {
"setasign/fpdi-fpdf": "^2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment