0

I can render the PDF in normal conditions, but I want to merge other pdf files to my main pdf file. So I added a bit of code. (check the section in my code: PART THAT CRASHES MY PDF.....)

the PHP error says:

PHP Fatal error: Uncaught Error: Call to undefined method FPDI::setSourceFile().

I can find the setSourceFile method in the FpdiTrait.php file which is in the FPDI lib. I also required the tcpdf which works. And also used the autoload for the FPDI library. I also tried to use both latest versions from GitHub.

Why doesn't it see this method? What did I do wrong?

<?php
    require_once(__DIR__ . "/../../include/init_controller.php");
    $plugin_url = "WoonWerkVerkeer/ExportOpenbaarVervoerVergoedingen";
    $plugin_page_type = "plugin_mainpage";
    $httpContext = new HttpContext();
    $httpContext->validate_session($plugin_url, $plugin_page_type);
    $logging = new Logging(); 
    
    try
    {
        // Include the main TCPDF library (search for installation path).
        require_once(__DIR__ . '/../../libs/tcpdf/tcpdf.php');
        require_once(__DIR__ . '/../../libs/FPDI/autoload.php');
        
        
        $config_purifier = HTMLPurifier_Config::createDefault();
        $purifier = new HTMLPurifier($config_purifier);
        
        $id = (int)$purifier->purify($_GET["id"]);
        
        $database = new Database();
        $sql = "a query which is working..";
        $database->query($sql);
        $database->bind(':id', $id); 
        $item = $database->single();
        $database->close(); 
        
        $database = new Database();
        $sql = "a query which is working..";
        $database->query($sql);
        $database->bind(':img_tablename', "tblOpenbaarVervoerVergoedingen"); 
        $database->bind(':img_table_id', $item["id"]); 
        $images = $database->resultset();
        $database->close();
        
        $database = new Database();
        $sql = "a query which is working..";
        $database->query($sql);
        $database->bind(':doc_tablename', "tblOpenbaarVervoerVergoedingen"); 
        $database->bind(':doc_table_id', $item["id"]); 
        $docs = $database->resultset();
        $database->close();
        
        try
        {
            $ibanBic = new IbanBic(); 
            $item["rekeningnr"] = $ibanBic->convert_BBAN_to_IBAN($item["rekeningnr"]);
        }
        catch(exception $e)
        {
            
        }
        
        // Extend the TCPDF class to create custom Header and Footer
        class FPDI extends TCPDF 
        {
            //set custom sql item object
            protected $item;
            public function setItem($item)
            {
                $this->item = $item;
            }
            
            //Page header
            public function Header() 
            {
                // Set font
                $this->SetFont('helvetica', 'B', 12);
                // Title
                $this->WriteHTML('<div>Aanvraag openbaar vervoer vergoeding: ' . $this->item["einddatum_jaar"] . '/' . $this->item["einddatum_maand"] . ' ' . $this->item["voornaam"] . ' ' . $this->item["naam"] . '</div>');
                //$this->SetTopMargin(35);
            }
        
            // 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, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
            }
        }
        
        // create new PDF document
        $pdf = new FPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->setItem($item);
        
        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('');
        $pdf->SetTitle($item["einddatum_jaar"] . '/' . $item["einddatum_maand"] . ' Openbaar vervoer vergoeding ' . $item["voornaam"] . ' ' . $item["naam"]);
        $pdf->SetSubject('Openbaar vervoer vergoeding');
        $pdf->SetKeywords('PDF');
        
        // set default header data
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
        
        // set header and footer fonts
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        
        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
        
        // set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP - 10, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
        
        // set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
        
        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
        
        // set some language-dependent strings (optional)
        if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
            require_once(dirname(__FILE__).'/lang/eng.php');
            $pdf->setLanguageArray($l);
        }
        
        // ---------------------------------------------------------
        
        // set font
        $pdf->SetFont('helvetica', '', 10);
        
        // add a page
        $pdf->AddPage();
        
        // define some HTML content with style
        $html = '';
        //$html .= '<style>'.file_get_contents(_BASE_PATH.'stylesheet.css').'</style>';
        $html .= '<style>
            h3 
            {
                /*font-size: 10pt;*/
            }
        </style>';
        
        $html .= '<h3>Overzicht openbaar vervoer vergoeding</h3>
        <table class="" cellpadding="1" cellspacing="1">
            <tr>
                <td width="30%" align="left">Voornaam en naam:</td>
                <td align="left">' . $item["voornaam"] . ' ' . $item["naam"] . '</td>
            </tr>
            <tr>
                <td align="left">Stamboeknummer:</td>
                <td align="left">' . $item["stamnummer"] . '</td>
            </tr>
            <tr>
                <td align="left">Rekeningnummer:</td>
                <td align="left">' . $item["rekeningnr"] . '</td>
            </tr>
            <tr>
                <td width="30%" align="left">Ticket datum:</td>
                <td align="left">' . $item["einddatum_maandnaam"] . ' ' . $item["einddatum_jaar"] . '</td>
            </tr>
            <tr>
                <td width="30%" align="left">Ticket type:</td>
                <td align="left">' . $item["type_naam"] . '</td>
            </tr>
            <tr>
                <td align="left">Bedrag:</td>
                <td align="left"><b>' . $item["round_bedrag"] . ' euro </b></td>
            </tr>
            <tr>
                <td align="left">Begindatum:</td>
                <td align="left">' . $item["begindatum"] . '</td>
            </tr>
            <tr>
                <td align="left">Einddatum:</td>
                <td align="left">' . $item["einddatum"] . '</td>
            </tr>
            <tr>
                <td align="left">Aantal dagen:</td>
                <td align="left">' . $item["aantal_dagen"] . '</td>
            </tr>
        </table>';
        $html .= '<div>Bijgevolg is de werkgever mij, volgens de tabel, de som verschuldigd van <b>' . $item["round_bedrag"] . ' euro </b> </div>';
        $html .= '<hr>';
        
        $html .= '<div>';
        $html .= '<input type="checkbox" name="agree" value="1" checked="checked"/>';
        $html .= '<label>Ik heb op eer verklaard kennis genomen te hebben van het feit dat misbruiken kunnen bestraft worden - zie punt 4.6 van de omzendbrief 13AC/CR/JVM/js van 22-12-2000.</label>';
        $html .= '</div>';
        $html .= '<span>';
        $html .= 'Datum: ' . $item["indienen_datum"];
        $html .= '</span>';
        $html .= '<hr>';
        
        foreach($images as $image)
        {
            $html .= '<img src="../../' . $image["img_path_med"] . '"> <br>';
        }

        // output the HTML content
        $pdf->writeHTML($html, true, false, true, false, '');
        
        // reset pointer to the last page
        $pdf->lastPage();
        
        //PART THAT CRASHES MY PDF.....
        if(count($docs) > 0)
        {
            foreach($docs as $doc)
            {
                $pageCount = $pdf->setSourceFile(ROOT . $doc["doc_path"]); //THIS LINE IT CRASHES!
                for($i = 1; $i <= $pageCount; $i++) 
                {
                    $tplIdx = $pdf->importPage($i);
                    $pdf->AddPage();
                    $pdf->useTemplate($tplIdx);
                }
            }
        }
        //END PART

        //Close and output PDF document
        ob_end_clean();
        $filename = $item["einddatum_jaar"] . '-' . $item["einddatum_maand"] . '_' . $item["login"] . '_openbaar_vervoer_vergoeding.pdf';
        $pdf->Output($filename, 'I');
    }
    catch(Exception $e)
    {
        $logging->write_logfile("PdfExportOpenbaarVervoerVergoeding.php", "tblOpenbaarVervoerVergoedingen", 0, "get", "try catch: " . $e->getMessage(), $_SESSION["admnet"]["login"], date("Y-m-d H:i:s"), $httpContext->get_client_ip());
        $obj->callback_error = "try catch script_error";
        echo json_encode($obj);
    }
?>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

0 Answers0