TCPDF ERROR: Unable to create output file
Asked Answered
T

13

9

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.

Telson answered 24/3, 2015 at 12:32 Comment(0)
T
26

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');
Togoland answered 25/3, 2015 at 15:26 Comment(2)
prepending $_SERVER['DOCUMENT_ROOT'] worked for me, thanks!Camerlengo
first check whether $_SERVER['DOCUMENT_ROOT'] contains trailing slash, if not than concat with '/' before filenameKacykaczer
F
9

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);
}
Flacon answered 16/9, 2015 at 13:37 Comment(2)
nice find, works!Asymptotic
In TCPDF Version 6.6.5 same file but line 1806+Penneypenni
P
7

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');
Paramagnetism answered 8/8, 2017 at 12:7 Comment(0)
I
5

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');

Ingather answered 25/4, 2016 at 20:59 Comment(0)
O
5

the issue is caused by file's path. I get this fixed by changing $pdf->Output('output.pdf', 'F'); to absolute path $pdf->Output('/var/www/yourdomain/output.pdf', 'F');

Overstuffed answered 26/2, 2017 at 20:54 Comment(1)
I can confirm that implementing this worked for me.Lucey
H
1

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.

Hemicycle answered 23/10, 2018 at 6:23 Comment(0)
M
1

Give your directory permission where you want to store your output.pdf file

chmod -R 0777 /yourdirectory
Martelli answered 13/5, 2021 at 3:40 Comment(0)
C
0

Check the folder permissions as well. Because i have the same issue and i resolve it by changing the folder permissions.

Chromite answered 17/8, 2016 at 11:18 Comment(0)
O
0

make sure that file sample.pdf is not open anywhere when you run script. if it's open somewhere else then TCPDF can't open it.

Outpour answered 10/1, 2019 at 13:9 Comment(0)
C
0

I having the same issue, but for my case to solve this following issue was declare the system temporary directory on XAMPP.

sys_temp_dir = /Applications/XAMPP/xamppfiles/temp/

Chartreuse answered 26/5, 2022 at 9:2 Comment(0)
C
0

en Output cambiar de :

 $f = TCPDF_STATIC::fopenLocal($name, 'wb');

a:

$f = fopen($name, 'wb');
Celinecelinka answered 1/6, 2022 at 17:7 Comment(0)
P
0

Since this cost me like an hour:
Make sure the folder exists and that you use the full path to the file.

TCPDF doesn't seem to create folders.

$year = date('Y');
$folder = __DIR__ . "/../invoices/$date";

if (!is_dir($folder)) {
    mkdir($folder, 0777, true);
}

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

Parr answered 11/9, 2023 at 17:17 Comment(0)
V
-2

Maybe your file was opened by another application

Vulpine answered 15/1, 2022 at 7:29 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Appealing
Same as https://mcmap.net/q/1116030/-tcpdf-error-unable-to-create-output-fileHalley

© 2022 - 2024 — McMap. All rights reserved.