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


TCPDF ERROR: Unable to create output file

我正在尝试使用TCPDF和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');

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

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

注意:没有文件权限问题

有人能指出这个问题吗。

尝试放入ob_clean();$pdf的正上方->输出('Output.pdf','F');

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

如果那不起作用。你需要设置这样的路径:

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

如果你不知道绝对路径,试试这个:

$pdf->Output($_SERVER['DOCUMENT_ROOT'] . 'output.pdf', 'F');

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

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

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

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

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

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

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

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

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

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

为您的目录授予存储output.pdf文件的权限

chmod -R 0777 /yourdirectory

我也有同样的问题,但为了解决以下问题,我在XAMPP上声明了系统临时目录。

sys_temp_dir = /Applications/XAMPP/xamppfiles/temp/

en-Output柬埔寨:

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

a:

$f = fopen($name, 'wb');

也许您的文件是由另一个应用程序打开的