php TCPDF 错误:无法创建输出文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29232736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:19:32  来源:igfitidea点击:

TCPDF ERROR: Unable to create output file

phppdftcpdffpdf

提问by Lingasamy Sakthivel

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

我正在尝试结合 TCPPDF 和 FPDI 生成 pdf。这是我的代码。

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.

在这里,最后一行$pdf->Output('output.pdf', 'F');用于保存文件。但它不起作用。当我只有 时$pdf->Output(),它在浏览器中显示 pdf。

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.

我试过$pdf->Output('output.pdf', 'D');下载,效果很好。似乎$pdf->Output('output.pdf', 'F');只是不工作,它显示了一个错误TCPDF ERROR: Unable to create output file: output.pdf

Note: there is no file permission issues

注意:没有文件权限问题

Can anyone point out the issue please.

任何人都可以指出这个问题。

回答by Puya Sarmidani

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

尝试把 ob_clean(); 正上方 $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');

回答by Atul Baraiya

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

在'include/tcpdf_static.php'文件中,静态函数'fopenLocal'中大约2435行,如果我删除完整的'if语句'......工作正常。

public static function fopenLocal($filename, $mode) {
    /*if (strpos($filename, '://') === false) {
        $filename = 'file://'.$filename;
    } elseif (strpos($filename, 'file://') !== 0) {
        return false;
    }*/
    return fopen($filename, $mode);
}

回答by mostafaznv

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

这是给使用 tcpdf 的 laravel 程序员的提示,如果你想将 pdf 保存到你的公共目录,只需使用这个:

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

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

回答by harrrrrrry

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

该问题是由文件路径引起的。我通过更改$pdf->Output('output.pdf', 'F');为绝对路径来解决这个问题$pdf->Output('/var/www/yourdomain/output.pdf', 'F');

回答by Charaf JRA

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

您必须放置完整路径而不是相对路径,使用 __DIR__ 的示例:

 $pdf->Output(__DIR__."/../invoices/invoice_".date('d-M-Y').".pdf", 'F');

回答by Mike Craig

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.

我发现这个错误也是由图像过多引起的。我试图创建一个包含 186 个图像的 PDF 并出现此错误。我尝试了上述所有选项,但仍然出现此错误。然后我减少了图像(并用 100 张图像进行了测试)PDF 创建正常。再次增加图像数量并再次出现错误。

回答by Divyesh Jesadiya

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

确保运行脚本时文件sample.pdf未在任何地方打开。如果它在其他地方打开,则 TCPDF 无法打开它。

回答by Syed Amir Bukhari

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

还要检查文件夹权限。因为我有同样的问题,我通过更改文件夹权限来解决它。