41

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

12 Answers12

65

try this

$pdf->Output('kuitti'.$ordernumber.'.pdf', 'F');
kushalbhaktajoshi
  • 4,640
  • 3
  • 22
  • 37
  • 1
    Works :) I will try saving it to a folder. –  Sep 06 '12 at 16:48
  • 3
    @coosal This throws error `TCPDF ERROR: Unable to create output file: ../invoice_job_6.pdf` – RN Kushwaha Jan 09 '15 at 12:17
  • 2
    @RNKushwaha Did you check folder permission? Please enable write permission for the destination folder. – kushalbhaktajoshi Jan 10 '15 at 06:39
  • 1
    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'] – RN Kushwaha Jan 11 '15 at 07:42
  • 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.. – eyal_katz Oct 17 '18 at 03:47
28

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');
Barry
  • 3,303
  • 7
  • 23
  • 42
Developer
  • 3,857
  • 4
  • 37
  • 47
  • The question is old, and has an accepted answer. No need to answer it. –  Oct 24 '13 at 13:17
  • 23
    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. – Developer Oct 24 '13 at 13:34
  • 1
    Sigh... Right you couldn't just write the folder straight to here. `$pdf->Output('/location/folder/'.$name.'.pdf','F')` –  Oct 24 '13 at 13:47
  • 1
    but its working for me.. and i'm sending it as attached file in email.. once it is generated. – Developer Oct 24 '13 at 13:56
  • So did I, I had no problem adapting. –  Oct 24 '13 at 14:40
  • 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";` – RN Kushwaha Jan 11 '15 at 07:44
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')
nick
  • 3,544
  • 1
  • 26
  • 22
  • 2
    for future reference i defined a UPLOAD_PATH and used it as `$pdf->Output(UPLOAD_PATH.'example_001.pdf', 'FI');` – alex Nov 18 '14 at 09:04
18

Only thing that worked for me:

// save file
$pdf->Output(__DIR__ . '/example_001.pdf', 'F');
exit();
user1477388
  • 20,790
  • 32
  • 144
  • 264
8

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');
CIRCLE
  • 4,501
  • 5
  • 37
  • 56
  • 3
    Relative paths work just fine. If they don't there's probably a misconfiguration. –  Feb 20 '15 at 10:21
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).

Jamy Mahabier
  • 400
  • 5
  • 16
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);
Mesa
  • 289
  • 2
  • 10
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

KarlosFontana
  • 188
  • 2
  • 7
  • Old question already, but it was needed to save it to the server, and then mail it to user. –  Oct 11 '13 at 11:27
3
$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);
}
TZHX
  • 5,291
  • 15
  • 47
  • 56
Atul Baraiya
  • 141
  • 1
  • 2
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
Zahid Gani
  • 169
  • 6
0

You may try;

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

So for you, it will be like;

$this->Output(/kuitit/);  //or try ("/kuitit/")
Alfred
  • 21,058
  • 61
  • 167
  • 249
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.

Nick
  • 138,499
  • 22
  • 57
  • 95
Greggo
  • 51
  • 1
  • 2