TCPDF Save file to folder?
Asked Answered
V

12

42

I'm using TCPDF to print a receipt and then send it to customer with phpMailer, but I have a problem:

I have no idea how to save the file into a pdf.

I've tried this:

// reset pointer to the last page
$pdf->lastPage();

//Close and output PDF document
$pdf->Output('kuitti'.$ordernumber.'.pdf', 'I');
$this->Output("kuitit");
Varner answered 6/9, 2012 at 16:37 Comment(2)
Hi. Where do you want the pdf to save into? your server or somewhere else?Racialism
@blasteralfred on my server at folder kuititVarner
S
67

try this

$pdf->Output('kuitti'.$ordernumber.'.pdf', 'F');
Sachiko answered 6/9, 2012 at 16:44 Comment(5)
Works :) I will try saving it to a folder.Varner
@coosal This throws error TCPDF ERROR: Unable to create output file: ../invoice_job_6.pdfInveracity
@RNKushwaha Did you check folder permission? Please enable write permission for the destination folder.Sachiko
It has 755 permission, However @developer's answer helped me finally. But It need to check if its linux server(my client's live server is on linux hosting) or windows server(my localhost is on windows). Then if it was on linux then I used $_ENV['DOCUMENT_ROOT']Inveracity
It expects the file path to be from the folder where the main library file resides. Or use absolute path, that you can find using $_ENV['DOCUMENT_ROOT'], then make sure it is writable. Use the 'F' parameter for generating a file, 'I' is for loading the output in the same page. The documentation doesn't make these things clear..Asiatic
Y
31

this stores the generated pdf file in your custom folder of your project

$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');
Yettayetti answered 24/10, 2013 at 12:5 Comment(6)
The question is old, and has an accepted answer. No need to answer it.Varner
yes it is.. but I was not getting pdf into my required folder. with the answer. So, I thought it'd help some one in future.Yettayetti
Sigh... Right you couldn't just write the folder straight to here. $pdf->Output('/location/folder/'.$name.'.pdf','F')Varner
but its working for me.. and i'm sending it as attached file in email.. once it is generated.Yettayetti
So did I, I had no problem adapting.Varner
I modified to this to work for me. As my localhost was on windows and live server was on linux. if($_SERVER['HTTP_HOST']=='localhost') $filelocation="D:\\xampp\\htdocs\\mysite\\attachment"; else $filelocation= $_ENV["DOCUMENT_ROOT"]."/attachment";Inveracity
M
19

$pdf->Output() takes a second parameter $dest, which accepts a single character. The default, $dest='I' opens the PDF in the browser.

Use F to save to file

$pdf->Output('/path/to/file.pdf', 'F')
Messer answered 20/8, 2013 at 16:31 Comment(1)
for future reference i defined a UPLOAD_PATH and used it as $pdf->Output(UPLOAD_PATH.'example_001.pdf', 'FI');Endsley
R
18

Only thing that worked for me:

// save file
$pdf->Output(__DIR__ . '/example_001.pdf', 'F');
exit();
Rexrexana answered 10/5, 2015 at 12:11 Comment(1)
Thanks, man this is also the only thing that works for me. Thanks a lot, man!Droop
B
9

For who is having difficulties storing the file, the path has to be all the way through root. For example, mine was:

$pdf->Output('/home/username/public_html/app/admin/pdfs/filename.pdf', 'F');
Belaud answered 19/2, 2015 at 20:9 Comment(1)
Relative paths work just fine. If they don't there's probably a misconfiguration.Varner
I
5

TCPDF uses fopen() to save files. Any paths passed to TCPDF's Output() function should thus be an absolute path.

If you would like to save to a relative path, use e.g. the __DIR__ global constant (see this answer).

Infra answered 6/4, 2015 at 12:44 Comment(0)
Z
4

If you still get

TCPDF ERROR: Unable to create output file: myfile.pdf

you can avoid TCPDF's file saving logic by putting PDF data to a variable and saving this string to a file:

$pdf_string = $pdf->Output('pseudo.pdf', 'S');
file_put_contents('./mydir/myfile.pdf', $pdf_string);
Zared answered 7/3, 2018 at 8:26 Comment(0)
F
3

nick's example saves it to your localhost.
But you can also save it to your local drive.
if you use doublebackslashes:

 $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. In Firefox (optional) Tools> Options > General tab> Download >Always ask me where to save files

Flowerlike answered 11/10, 2013 at 11:16 Comment(1)
Old question already, but it was needed to save it to the server, and then mail it to user.Varner
B
3
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
Byssus answered 24/10, 2021 at 18:46 Comment(0)
R
2
$pdf->Output( "myfile.pdf", "F");

TCPDF ERROR: Unable to create output file: myfile.pdf

In the include/tcpdf_static.php file about 2435 line in the static function fopenLocal if I delete the complete 'if statement' it 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);
}
Redvers answered 17/9, 2015 at 10:58 Comment(0)
R
0

You may try;

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

So for you, it will be like;

$this->Output(/kuitit/);  //or try ("/kuitit/")
Racialism answered 6/9, 2012 at 16:42 Comment(1)
It actually needed the extension and FVarner
J
0

This worked for me, saving to child dir(temp_pdf) under the root:

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

Remember to make the dir writeable.

Jamilajamill answered 21/8, 2020 at 0:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.