0

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 :)

Felipe Lima
  • 443
  • 1
  • 10
  • 19

2 Answers2

0

If I understand well, the problem is that you're redeclaring the FPDF class. To avoid this, you could verify if the class already exists, before trying to redeclare it, using the class_exists php function.

Try this:

if (!class_exists('FPDF')) {
    require('fpdf/fpdf.php');
}
  • If I do this return me the error: Cannot redeclare class PDF in C:\wamp\www\congress\gera-certificado-wm.php on line 21 I also commented the lines 3, 4, 5 and 19 but show error 'can not redeclare header on line 14' but I need to insert another header and footer to the second and third PDFs. In time, the first PDF is created normally. Thanks for your help! – Felipe Lima Feb 10 '15 at 19:55
0

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.

Felipe Lima
  • 443
  • 1
  • 10
  • 19