1

I'm trying to figure out a problem with adding my own "modul" in Open Journal System. I need to edit a PDF document with FPDI a TCPDF functions - adding some informations to the PDF file.

I have my own page in OJS, where I want to upload a file, then input some information into form and after submit, there is some process, which add a informations into the PDF. But when I get my page i get this error:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Cannot open C:\xampp\htdocs\ojs245 !' in C:\xampp\htdocs\ojs245\pages\stamp\pdf_parser.php:192 Stack trace: #0 C:\xampp\htdocs\ojs245\pages\stamp\fpdi_pdf_parser.php(71): pdf_parser->__construct('C:\\xampp\\htdocs...') #1 C:\xampp\htdocs\ojs245\pages\stamp\fpdi.php(128): fpdi_pdf_parser->__construct('C:\\xampp\\htdocs...') #2 C:\xampp\htdocs\ojs245\pages\stamp\fpdi.php(108): FPDI->_getPdfParser('C:\\xampp\\htdocs...') #3 C:\xampp\htdocs\ojs245\pages\stamp\class.php(15): FPDI->setSourceFile(NULL) #4 C:\xampp\htdocs\ojs245\pages\stamp\lib\tcpdf\tcpdf.php(3543): PDF->Header() #5 C:\xampp\htdocs\ojs245\pages\stamp\lib\tcpdf\tcpdf.php(3210): TCPDF->setHeader() #6 C:\xampp\htdocs\ojs245\pages\stamp\lib\tcpdf\tcpdf.php(3122): TCPDF->startPage('schval.pdf', '', false) #7 [internal function]: TCPDF->AddPage('schval.pdf') #8 C:\xampp\htdocs\ojs245\pages\stamp\fpdf_tpl.php(367): call_user_func_array(Array, Array) #9 C:\xampp\htdocs\ojs245\pages\stamp\StampHandl in C:\xampp\htdocs\ojs245\pages\stamp\pdf_parser.php on line 192

I have this structure in ojs\pages: index.php, StampHandler.inc.php where is the main function, which should make changes in the PDF:

import('classes.handler.Handler');

    class StampHandler extends Handler {
        function stampingOperation($args, $request) {
            $templateMgr = TemplateManager::getManager($request);
            $templateMgr->display('stamp/stampingOperation.tpl');
            //echo 'Operace razitkovani';
            //$templateMgr->assign('test', $promenna);
            $templateMgr->assign('test', $request->getUserVar('test'));

require_once('lib/tcpdf/tcpdf.php'); 
require_once('fpdi.php');

// Original file with multiple pages 
require_once('class.php');

        // initiate PDF
        $pdf = new PDF();
                $pdf->fullPathToFile = "TestClanek.pdf";
        // add a page
        $pdf->AddPage("schval.pdf");
        //$pdf->AddPage();

        if($pdf->numPages>1) {
            for($i=2;$i<=$pdf->numPages;$i++) {
                $pdf->endPage();
                $pdf->_tplIdx = $pdf->importPage($i);
                $pdf->AddPage();
            }
        }

        // or Output the file as forced download
            $file_time = time();

            $pdf->Output("$file_time.pdf", "F");//, "I"); 
            echo "'<a href=$file_time.pdf>Edited file</a>'";      }
    }

Could anyone have some idea, what could be the problem?

Thanks, Zbynek

edit: class.php

class PDF extends FPDI {

var $_tplIdx;
var $fullPathToFile;

  function Header() {
      echo $this->fullPathToFile;

                if(is_null($this->_tplIdx)) {

                // THIS IS WHERE YOU GET THE NUMBER OF PAGES
                $this->numPages = $this->setSourceFile($this->fullPathToFile);
                $this->_tplIdx = $this->importPage(1);
                } 

                if($this->page > 1) {      
                    $this->SetFont('helvetica', 'I', 10); 
                    $this->SetTextColor(0);
                    $this->Write(15, $_POST["casopis"] .", Cislo: ". $_POST["rocnik"]."/0".$_POST["cislo"].", Autor: ".$_POST["autor"]."\n");  // ."\n"  border, 1);

                    // generování času a podmínka pro časové razítko
                    SetLocale(LC_ALL, "Czech");
                    $cas = StrFTime("%H:%M:%S", Time());
                    if($_POST["cas_razitko"]=="ok") {
                        // pozice   $this->SetXY(30, 30);
                        $this->Write(0, "Cas upravy: ".$cas); //."\n");          //." ", '', 0, 'L', true, 0, false, false, 0     15, 
                    }
                    // pokud je vlastní text - potom vytiskni:
                    if($_POST["text_razitko"]=="ok") {
                        //$this->SetXY(30, 30);
                        $this->Write(0, $_POST["text"]);
                    }

                    $style = array(
                      'border' => 1,
                      'padding' => 'auto',
                      'fgcolor' => array(0,0,0),
                      'bgcolor' => array(255,255,255)
                      );

                    if($_POST["qr_kod"]=="ok") {
                    $this->write2DBarcode($_POST["casopis"].", ".$_POST["autor"], 'QRCODE,L', 180, 3, 20, 20, $style, 'N');
                    }

                    $this->Image('logo.png', 100, 2, 75,7);          


                }

      $this->useTemplate($this->_tplIdx, 0, 0,200);

    }

    }
Zbyněk Dufka
  • 37
  • 1
  • 7
  • What's the parameter of the addPage() method? This method does not expect a filename! The setSourceFile() call is in the TCPDF.php class with the parameter NULL... why is it there?! And you will also find the problem there. As you'd extended FPDI you should use a Header implementation in this class. – Jan Slabon Apr 18 '15 at 18:34
  • sorry, i forgot about posting class.php (i had to separate it because of Fatal error: Class declarations may not be nested in C:\xampp\htdocs\ojs245\pages\stamp\StampHandler.inc.php on line 67) - I added into the main question – Zbyněk Dufka Apr 18 '15 at 18:58

1 Answers1

1

The var $fullPathToFile is not global in your calling class, which results in passing null to setSourceFile() in your PDF class... you should change this to a property instead:

$pdf->fullPathToFile = "...";

later in your PDF class:

$this->setSourceFile($this->fullPathToFile);
Jan Slabon
  • 4,736
  • 2
  • 14
  • 29
  • When I change it, i'm getting same error: Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Cannot open C:\xampp\htdocs\ojs245 !' – Zbyněk Dufka Apr 18 '15 at 22:35
  • So please update your code. A general hint: Simply read through the error message and trace. All needed information to fix this are in there. – Jan Slabon Apr 19 '15 at 10:06
  • I've updated the code in main question. What confuse me, that when i have the script out of OJS, that's ok, it works fine. But when I implement it in the structure of OJS, I'm getting this error.I'll try to figure it out with your tip. Thanks a lot for your help. – Zbyněk Dufka Apr 19 '15 at 10:25
  • If I understand it well, still it's a problem with loading the file TestClanek.pdf. I thought it could be problem with access rights to the folder ojs245, but I set it correctly - on localhost for all users. Try to change the path from absolute, relative, but not working for me. – Zbyněk Dufka Apr 19 '15 at 10:56
  • `FPDI->setSourceFile(NULL)` shows you that NULL is passed to that method. The file "TestClanek.pdf" seems never to be passed to this method but NULL. – Jan Slabon Apr 19 '15 at 11:55
  • Thanks for comment, Yes, I understand this, but I cannot find out, where is the problem. The file is in the same folder as script. – Zbyněk Dufka Apr 20 '15 at 15:11
  • I just recognized that you'd updated your post with the current code. You pass the property to an non existing object... move it below the constructor call... You should also enable your error reporting to catch such things your own! – Jan Slabon Apr 21 '15 at 06:48
  • I updated the code, make it more synoptic. Now it should be ok, but still I get an: Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Cannot open TestClanek.pdf !'. But echo $this->fullPathToFile return TestClanek.pdf. – Zbyněk Dufka Apr 27 '15 at 21:47
  • It's a simple fopen() call which will end in this error message. So the error message says everything: The file cannot be opened because it is not at the given path or the rights are not granted. – Jan Slabon Apr 28 '15 at 09:01
  • I've changed the path to absolute, and it works now! thanks a lot for helping. – Zbyněk Dufka Apr 28 '15 at 12:37