Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF ERROR: Unable to create output file

Tags:

php

pdf

fpdf

tcpdf

I'm trying to generate pdf with the combination of TCPDF and FPDI. Here is my code.

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

$fileName = '../sample.pdf';

class PDF extends FPDI {
/**
 * "Remembers" the template id of the imported page
 */
var $_tplIdx;
var $numPages = 0;

/**
 * Draw an imported PDF logo on every page
 */
function Header() {

    global $fileName;

    if (is_null($this->_tplIdx)) {
        $this->setSourceFile($fileName);
        $this->_tplIdx = $this->importPage(1);
        $this->numPages = $this->setSourceFile($fileName);
    }
    $size = $this->useTemplate($this->_tplIdx);
}

function Footer() {
    // emtpy method body
}
}

// initiate PDF
$pdf = new PDF($fileName);
$pdf->setFontSubsetting(true);

// add a page
$pdf->AddPage();

// save file
$pdf->Output('output.pdf', 'F');

Here, the last line $pdf->Output('output.pdf', 'F'); is for saving the file. But it is not working. When I was having only $pdf->Output(), it was showing the pdf in browser.

I've tried $pdf->Output('output.pdf', 'D'); for downloading and it worked fine. Seems $pdf->Output('output.pdf', 'F'); is only not working and it shown an error TCPDF ERROR: Unable to create output file: output.pdf.

Note: there is no file permission issues

Can anyone point out the issue please.

like image 257
Linga Avatar asked Mar 24 '15 12:03

Linga


5 Answers

Try putting ob_clean(); right above $pdf->Output('output.pdf', 'F');

ob_clean();

// save file
$pdf->Output('output.pdf', 'F');

if that dont work. Than you need to set a path like this:

$pdf->Output('yourpath/output.pdf', 'F');

if you dont know the absolute path try this:

$pdf->Output($_SERVER['DOCUMENT_ROOT'] . 'output.pdf', 'F');
like image 132
Puya Sarmidani Avatar answered Nov 17 '22 17:11

Puya Sarmidani


I found that this error was also caused by too many images. I was trying to create a PDF with 186 images and got this error. I tried all the options above and still got this error. I then reducing the images (and did a test with 100 images) PDF created ok. Increase the number of images again and got the error again.

like image 29
Mike Craig Avatar answered Sep 19 '22 23:09

Mike Craig


In the 'include/tcpdf_static.php' file about 2435 line in the static function 'fopenLocal' if I delete the complete 'if statement'... works fine.

public static function fopenLocal($filename, $mode) {
    /*if (strpos($filename, '://') === false) {
        $filename = 'file://'.$filename;
    } elseif (strpos($filename, 'file://') !== 0) {
        return false;
    }*/
    return fopen($filename, $mode);
}
like image 6
Atul Baraiya Avatar answered Nov 17 '22 18:11

Atul Baraiya


You have to put the full path instead of relative one, example of usage with__DIR__:

 $pdf->Output(__DIR__."/../invoices/invoice_".date('d-M-Y').".pdf", 'F');
like image 6
Charaf JRA Avatar answered Nov 17 '22 18:11

Charaf JRA


this is a tip for laravel programmers who using tcpdf if you want to save pdf to your public directory, just use this:

PDF::Output(public_path('/uploads/pdf/hello_world.pdf'),'F');

like image 4
mostafaznv Avatar answered Nov 17 '22 16:11

mostafaznv