Search code examples
phppdffpdfmultiple-processes

FPDF multiple pdf with PHP data - Different header and footer


I'm trying to create 1, 2 or 3 PDFs depending on the data coming from DB - with FPDF and PHP. If I have to create 1 PDF that's all ok. But if I need to create 2 or 3 I'm having problems because can not redeclare some classes.

Bellow the PHP code that test how many PDF's have to be generated:

include "gera-certificado.php"; //creates the first PDF - ok

if($workshop_manha != ''){
    include "gera-certificado-wm.php"; // if this var is not empty, then create the second PDF -> problem
}

if($workshop_tarde != ''){
    include "gera-certificado-wt.php"; // if this var is not empty, then create the third PDF -> problem
}

Below the FPDF code - consists in 2 images (header and footer) and only 2 or 3 lines of text with the name var. In first PDF generates Ok but in the second and third returns me error because is redeclared some classes of FPDF.

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<?php
define('FPDF_FONTPATH','fpdf/font/');
 require('fpdf/fpdf.php');
 class PDF extends FPDF {
 function Header() {
 $this->SetFont("Arial","I",10);
 //escreve no pdf largura,altura,conteudo,borda,quebra de linha,alinhamento,preenchimento fundo

 //altura
 $this->SetY(0);
 $this->Image("img/topo-certificado.jpg",0,0,297,100); //Image($arquivo);

 }
 function Footer(){
 $this->SetY(-15);
 $this->Image("img/rodape-certificado.jpg",0,140,297,70); //Image($arquivo);
 }
 }

 $pdf=new PDF('L','mm','A4');
 $pdf->Open();
 $pdf->AddPage();
 $pdf->SetMargins(0,0,0);
 $pdf->SetAutoPageBreak(false);
 $pdf->SetAuthor('Dr. Ricardo Wainer');
 $pdf->SetTitle('Certificado');

 $pdf->SetFont('Arial','',13);
 $pdf->SetTextColor(27,67,161);     // Definir cor do texto
 $pdf->SetDrawColor(0,0,0); // Definir cor do traço?
 $pdf->SetXY(30,106);
 $pdf->MultiCell(236,8,"Certificamos que ".strtoupper($nome)." participou do I Congresso Wainer de Psicoterapias Cognitivas, I Congresso Sul-Brasileiro de Terapias Integradas e de 3ª Onda e I Simpósio Brasileiro de Terapia do Esquema, realizado no dias 27, 28 e 29 de agosto de 2015, com carga horária de 22 horas.",0,'C');

 $pdf->Output("../../certificados/".$id.".pdf");

 ?>

Ok. Thats it! The second and third FPDF codes are the same that above only being different the image dir because the PDF is header and footer different. Thanks in advance for your help and time :)


Solution

  • I solved the problem by doing the following:

    After includind gera-certificado.php (file that generate first certificate) instead of including the next one file (gera-certificado-wm.php) I redirected using get to take advantage of the variables on the next page.

    So after the first certificate was generated this is the flow:

            if($workshop_manha != '' AND $workshop_tarde != ''){
                echo "<META HTTP-EQUIV='REFRESH' CONTENT=\"0; URL='gera-certificado-wm.php?id=".$id."&oc=".$oc."&nome=".$nome."&email=".$email."&wm=".$workshop_manha."&wt=".$workshop_tarde."'\">";
            }
            if($workshop_manha != '' AND $workshop_tarde == ''){
                echo "<META HTTP-EQUIV='REFRESH' CONTENT=\"0; URL='gera-certificado-wm.php?id=".$id."&oc=".$oc."&nome=".$nome."&email=".$email."&wm=".$workshop_manha."'\">";
            }
            if($workshop_manha == '' AND $workshop_tarde != ''){
                echo "<META HTTP-EQUIV='REFRESH' CONTENT=\"0; URL='gera-certificado-wt.php?id=".$id."&oc=".$oc."&nome=".$nome."&email=".$email."&wm=".$workshop_manha."&wt=".$workshop_tarde."'\">";
            }
    
            if($workshop_manha == '' AND $workshop_tarde == '' AND $oc != '0'){
                echo "<META HTTP-EQUIV='REFRESH' CONTENT=\"0; URL='../../action/envia-certificado-congresso.php?id=".$id."&nome=".$nome."&email=".$email."'\">";
            }
    

    Resuming: If the person enrolled in the morning and afternoon courses, I redirect to the morning certificate and then to the afternoon certificate and lastly I send all certificates.

    If the person enrolled only in the morning or afternoon, I redirect to the specific certificate and then send all certificates.

    And if the person did not enroll in any course in the morning or afternoon, I only send the certificate of participation.

    Maybe the code is not so beautiful, but at the time it worked and solved the needs. I leave the resolution here. Maybe it'll help someone else.