0

Not able to locate requested PDF

Here is my function:-

function genpdf()
{
    $this->load->library('session');
    $this->load->library('PDFMerger');
    $a=$_POST['a'];
    $b=$_POST['b'];
    $pdf = new PDFMerger;
    $pdf->addPDF("samplepdfs/$a")
    ->addPDF("samplepdfs/$b")
    ->merge('file', 'http://localhost/student/samplepdfs/genpdf.pdf');
}

I need to merge different PDF into one. I am using fpdf & fpdi. I want it to be done with codeigniter. Another solutions(different methods than mentioned above) are also welcome.

Martin Schröder
  • 3,086
  • 3
  • 38
  • 69
Nilu
  • 1
  • 1
  • Add pdfs one by one and add debug output to find out exactly which pdf could not be found. – maxhb Dec 29 '15 at 13:17
  • How did you could use PDFMerger in CodeIgniter? Adding like a thirdparty library gives me bunch of errors. – Elber CM Dec 30 '16 at 16:47

1 Answers1

2

Its better to check if pdf file exists before trying to add it, as you are fetching the pdf filename from POST data, like:

...
$a=$_POST['a'];
$b=$_POST['b'];

$error = false;
$errorMsg = "";
$pdf = new PDFMerger;
if( file_exists("samplepdfs/$a") ) {
    $pdf->addPDF("samplepdfs/$a");
}
else {
    $error = true;
    $errorMsg .="File:".$a." not found <br />";
}
if( file_exists("samplepdfs/$b") ) {
    $pdf->addPDF("samplepdfs/$b")
}
else {
    $error = true;
    $errorMsg .="File:".$b." not found";
}

//merge pdf if no error
if( !$error ) {
    $pdf->merge('file', 'http://localhost/student/samplepdfs/genpdf.pdf');
}
else {
    echo $errorMsg;
}
Sudhir Bastakoti
  • 94,682
  • 14
  • 145
  • 149