• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

TCPDF - Mehrere Dateien erstellen mit einer Funktion

sveka

Mitglied
Liebe Gemeinde,

ich möchte gerne mehrere PDF-Dateien per TCPDF erstellen. Leider erstellt er immer nur die erste und bricht dann ab. Hat jemand eine Idee?

PHP:
function pdf($pfad , $neuerDateiname, $output)
    {     
            // Extend the TCPDF class to create custom Header and Footer
            class MYPDF extends TCPDF
            {
                // Page footer
                public function Footer()
                {
                    // Position at 15 mm from bottom
                    $this->SetY(-15);
                    // Set font
                    $this->SetFont('helvetica', 'I', 8);
                    // Page number
                    $this->Cell(0, 10, '123'.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
                }
            }

            $obj_pdf = new MYPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 
            $obj_pdf->SetCreator(PDF_CREATOR); 
            $obj_pdf->SetTitle($neuerDateiname); 
            $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING); 
            $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); 
            $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); 
            $obj_pdf->SetDefaultMonospacedFont('helvetica'); 
            $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); 
            $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);
            $obj_pdf->setPrintHeader(false); 
            $obj_pdf->setPrintFooter(true); 
            $obj_pdf->SetAutoPageBreak(TRUE, 10); 
            $obj_pdf->SetFont('helvetica', '', 10); 
            $obj_pdf->AddPage(); 
            $content = $output;
            $obj_pdf->writeHTML($content); 
            //$obj_pdf->Output("C:\Apache24\htdocs\bildung-service.com_2022\data\user\SvenKasubek11\Gutschriften\/" . $monat .'_' . $jahr . '\/' .$dateiname , 'F');
            //$pdf_string = $obj_pdf->Output('pdftmp.pdf', 'S');
            //file_put_contents($pfad . '/' . $neuerDateiname, $pdf_string);
            //unset($obj_pdf);
            $obj_pdf->Output($pfad . '/' . $neuerDateiname , 'F');
            unset($obj_pdf);

    }

Wird in einer WHILE-Schleife aufgerufen wenn ich die Daten zur Ausgabe beisammen habe.

PHP:
while(...)
{
    .. Daten sammeln
     pdf($ordnerpfad , $neuerDateiname, $output);
}
 
Werbung:
Was auffällt: deine abgeleitete Klasse in einer Funktion zu deklarieren, halte ich für extrem ungünstig. Das würde ich als erstes ändern. Ansonsten Error-Reporting hochgedreht?
 
Ich habe das jetzt nicht testen können, aber ich hätte es so versucht:
PHP:
<?php
 
   // Extend the TCPDF class to create custom Header and Footer
   class MYPDF extends TCPDF
   {
      // Init
      public function init()
      {
         SetCreator(PDF_CREATOR);        
         SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
         setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
         setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
         SetDefaultMonospacedFont('helvetica');
         SetFooterMargin(PDF_MARGIN_FOOTER);
         SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);
         setPrintHeader(false);
         setPrintFooter(true);
         SetAutoPageBreak(TRUE, 10);
         SetFont('helvetica', '', 10);
      }
     
      // Page footer
      public function Footer()
      {
         // Position at 15 mm from bottom
         $this->SetY(-15);
         // Set font
         $this->SetFont('helvetica', 'I', 8);
         // Page number
         $this->Cell(0, 10, '123'.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
      }

      public function create_pdf($pfad , $neuerDateiname, $output)
      {    
         AddPage();
         SetTitle($neuerDateiname);
         writeHTML($output);
         Output($pfad . '/' . $neuerDateiname , 'F');
     
         return getNumPages();
      }
   }

   $pdf = new MYPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
   $pdf->init();

   while(...)
   {
       $page_count = $pdf->create_pdf($ordnerpfad , $neuerDateiname, $output);
   
       while ($page_count--)
          $pdf->deletePage(0);
   }
?>
 
Zuletzt bearbeitet:
Werbung:
Habe gerade ein paar Minuten Zeit gehabt, hier eine getestete Version:
PHP:
<?php
   include("./TCPDF/config/tcpdf_config.php");
   include("./TCPDF/tcpdf.php");
 
   // Extend the TCPDF class to create custom Header and Footer
   class MYPDF extends TCPDF
   {
      // Init
      public function init()
      {
         $this->SetCreator(PDF_CREATOR);      
         $this->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
         $this->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
         $this->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
         $this->SetDefaultMonospacedFont('helvetica');
         $this->SetFooterMargin(PDF_MARGIN_FOOTER);
         $this->SetMargins(PDF_MARGIN_LEFT, '5', PDF_MARGIN_RIGHT);
         $this->setPrintHeader(false);
         $this->setPrintFooter(true);
         $this->SetAutoPageBreak(TRUE, 10);
         $this->SetFont('helvetica', '', 10);
      }
   
      // Page footer
      public function Footer()
      {
         // Position at 15 mm from bottom
         $this->SetY(-15);
         // Set font
         $this->SetFont('helvetica', 'I', 8);
         // Page number
         $this->Cell(0, 10, '123'.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
      }

      public function create_pdf($pfad , $neuerDateiname, $output)
      {  
         $this->AddPage();
         $this->SetTitle($neuerDateiname);
         $this->writeHTML($output);        
         $this->Output($pfad . '/' . $neuerDateiname , 'F');
      }
   }

   $files = array("../index.html", "../infobox.html", "../test.html");
 
   $i = 0;
   $ordnerpfad = "D:/webserver/Apache-2.4.58/htdocs/pdf/pdf";

   foreach ($files as $file)
   {
       $pdf = new MYPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
       $pdf->init();
 
       $neuerDateiname = "xx" . $i . ".pdf";
     
       $i++;
     
       $pdf->create_pdf($ordnerpfad , $neuerDateiname, file_get_contents($file));
     
       unset($pdf);
   }
 
   echo "Ready";
?>
 
Werbung:
Zurück
Oben