TCPDF将文件保存到文件夹


TCPDF Save file to folder?

我使用TCPDF打印收据,然后用phpMailer将其发送给客户,但我遇到了一个问题:

我不知道如何将文件保存为pdf

我试过这个:

// reset pointer to the last page
$pdf->lastPage();
//Close and output PDF document
$pdf->Output('kuitti'.$ordernumber.'.pdf', 'I');
$this->Output("kuitit");

尝试这个

$pdf->Output('kuitti'.$ordernumber.'.pdf', 'F');

这将生成的pdf文件存储在项目的自定义文件夹中

$filename= "{$membership->id}.pdf"; 
$filelocation = "D:''wamp''www''project''custom";//windows
$filelocation = "/var/www/project/custom"; //Linux
$fileNL = $filelocation."''".$filename;//Windows
$fileNL = $filelocation."/".$filename; //Linux
$this->pdf->Output($fileNL, 'F');

$pdf->Output()接受第二个参数$dest,该参数接受单个字符。默认情况下,$dest='I'会在浏览器中打开PDF。

使用F保存到文件

$pdf->Output('/path/to/file.pdf', 'F')

唯一对我有用的东西:

// save file
$pdf->Output(__DIR__ . '/example_001.pdf', 'F');
exit();

对于存储文件有困难的人,路径必须一直通过root。例如,我的是:

$pdf->Output('/home/username/public_html/app/admin/pdfs/filename.pdf', 'F');

TCPDF使用fopen()保存文件。因此,传递给TCPDF的Output()函数的任何路径都应该是绝对路径。

如果要保存到相对路径,请使用例如__DIR__全局常量(请参阅此答案)。

如果你仍然得到

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

您可以通过将PDF数据放入变量并将此字符串保存到文件中来避免TCPDF的文件保存逻辑:

$pdf_string = $pdf->Output('pseudo.pdf', 'S');
file_put_contents('./mydir/myfile.pdf', $pdf_string);

nick的示例将其保存到本地主机
但您也可以将其保存到本地驱动器中
如果使用双反斜杠:

 $filename= "Invoice.pdf"; 
 $filelocation = "C:''invoices";  
 $fileNL = $filelocation."''".$filename;
 $pdf->Output($fileNL,'F');
 $pdf->Output($filename,'D'); // you cannot add file location here

p.S.在Firefox中(可选)工具>选项>常规选项卡>下载>总是问我在哪里保存文件

$pdf->Output( "myfile.pdf", "F");

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

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);
}
require __DIR__ . '/vendor/autoload.php';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$html = '<h2>Welcome toTcpdf</h2>'; 
$html.= '<p>Welcome toTcpdf</p>'; 
//$pdf->writeHTML($tbl, true, false, true, false, '');
$pdf->writeHTML($html, true, false, false, false, '');
$time = time();
$sFilePath = __DIR__ . '/upload/'.$time.'.pdf' ;
$pdf->Output( $sFilePath , 'F'); // Save to folder
//$pdf->Output( $sFilePath , 'I'); // view to browser
//$pdf->Output( $sFilePath , 'D'); // Download instant

this is working

您可以尝试;

$this->Output(/path/to/file);

所以对你来说,它会像;

$this->Output(/kuitit/);  //or try ("/kuitit/")

这对我很有效,保存到根目录下的子目录(temp_pdf):

$sFilePath = $_SERVER['DOCUMENT_ROOT'] . '//temp_pdf/file.pdf' ;
$pdf->Output( $sFilePath , 'F');

记住要使目录可写。