Skip to content

Instantly share code, notes, and snippets.

@dljoseph
Created November 22, 2014 08:00
Show Gist options
  • Save dljoseph/c31b8c6c06b6f19308bc to your computer and use it in GitHub Desktop.
Save dljoseph/c31b8c6c06b6f19308bc to your computer and use it in GitHub Desktop.
PDF generation in SilverStripe with FPDF/PDFI using existing PDF document as a base template
// initiate FPDI
$pdf = new FPDI();
// add a page
$pdf->AddPage('landscape');
// set the source file
$pdf->setSourceFile(ASSETS_PATH . '/certificate_template_pdfa.pdf');
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 297 mm
$pdf->useTemplate($tplIdx, 0, 0, 297);
// now write some text - FirstName LastName - on a layer above the imported page
$pdf->SetFont('Times');
$pdf->SetFontSize(29);
$pdf->SetTextColor(59, 59, 59); //grey
$pdf->SetY(60); //60 from the top
// $pdf->MultiCell(0, 28, "Darren-Lee Joseph",0, "C");
$pdf->MultiCell(0, 28, $certificate->RecipientName, 0, "C");
// now write some text - Name of Course - on a layer above the imported page
$pdf->SetFont('Times', 'I');
$pdf->SetFontSize(23);
$pdf->SetTextColor(60, 60, 60); //grey
$pdf->SetY(92); //92 from the top
// $pdf->MultiCell(0, 23, "Object-oriented PHP with the SilverStripe Framework",0, "C");
$pdf->MultiCell(0, 23, $certificate->CourseTaken, 0, "C");
// Insert a signature of authorised assessor
// $pdf->Image(ASSETS_PATH .'/signature.png',97,119,100,0,'','','',false,300);
$pdf->Image($certificate->AuthoritySignature()->absoluteURL,97,119,100,0,'','','',false,300);
// now write some text - Name of Assessor/Instructor and Position - on a layer above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetFontSize(13);
$pdf->SetTextColor(60, 60, 60); //grey
$pdf->SetY(142); //142 from the top
$pdf->MultiCell(0, 5, "{$certificate->AuthoritySignatoryName}, {$certificate->AuthoritySignatoryPosition}\n{$certificate->AuthoritySignatoryCompany}", 0, "C");
// now write some text - verify online: - on a layer above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetFontSize(12);
$pdf->SetTextColor(60, 60, 60); //grey
$pdf->SetXY(100, 160); //160 from the top
$pdf->Write(5, "verify online: ");
// now write the hyperlink - on a layer above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetFontSize(12);
$pdf->SetTextColor(43,154,197); //light blue
$pdf->SetXY(125, 160); //160 from the top
$pdf->Write(5, $certificate->absoluteLink(), $certificate->absoluteLink());
// now write some text - Completed: 23/11/2014 - on a layer above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetFontSize(11);
$pdf->SetTextColor(60, 60, 60); //grey
$pdf->SetXY(210, 172);
$pdf->Write(5, "Completed: ". $certificate->obj('CompletedDate')->Date());
// Insert a company logo seal in the bottom-right corner at 300 dpi
$pdf->Image(ASSETS_PATH .'/logo_seal_z1.png',221,137,25,0,'','','',false,300);
$pdf->Image(ASSETS_PATH .'/logo_seal_z2.png',221,136,25,0,'','','',false,300);
$pdf->Image(ASSETS_PATH .'/logo_seal_z3.png',221,136.5,25,0,'','','',false,300);
$pdf->Output();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment