25

I want to save the PDF file into a user specified directory. I am using FPDF. And the code is as below:

<?php
//echo "<meta http-equiv=\"refresh\" content=\"0;url=http://www.train2invest.net/useradmin/atest_Khan.php\">";
require('fpdf.php');

//create a FPDF object
$pdf=new FPDF();

//set font for the entire document
$pdf->SetFont('times','',12);
$pdf->SetTextColor(50,60,100);

//set up a page
$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');

//Set x and y position for the main text, reduce font size and write content
$pdf->SetXY (10,60);
$pdf->SetFontSize(12);
$pdf->Write(5,'Dear Ms.XYX');

 $filename="test.pdf";
//Output the document
$dir = "/G:/PDF/test.pdf/"; // full path like C:/xampp/htdocs/file/file/
$pdf->Output($dir.$filename,'F');
?>

Now even if I put "G:\PDF\" in the filename it doesn't save it!! I have tried the following:

$filename="G:\PDF\test.pdf";
$pdf->Output($filename.'.pdf','F');


$filename="G:\\PDF\\test.pdf";
$pdf->Output($filename.'.pdf','F');

$filename="G:/PDF/test.pdf";
$pdf->Output($filename.'.pdf','F');


$filename="/G:/PDF/test.pdf/";
$pdf->Output($filename.'.pdf','F');

I have checked that the directory i am trying to write has write/read permission and it's there. IT STILL DOESN'T WORK!

PLEASE help somebody...

Community
  • 1
  • 1
Sharmin
  • 251
  • 1
  • 3
  • 3
  • use formatting `code block` its difficult to read your question and `php` tag to get attention of right people – Ish Dec 02 '10 at 21:02
  • Have you tried outputting it to the browser to see if the `Output()` routine is working at all? Switch the 'F' to a 'D' to try. – Orbling Dec 02 '10 at 21:40
  • I have tried that but I want the PDF to be saved into a local folder, I don't want the user to save it under a folder of their choice, I want the PDF to be saved under a folder that I would specify within the code.....but it's not happening. – Sharmin Dec 07 '10 at 13:54
  • make sure that PHP has the sufficient permission to write in that path – Mehedi Hasan Apr 04 '23 at 23:48

9 Answers9

52

You are using the F option incorrectly, F is designed to save the PDF locally on the Server not in a specific directory on the users machine. So you would use something like:

$filename="/home/user/public_html/test.pdf";
$pdf->Output($filename,'F');

This will save in in the public_html directory of your webserver

Deshan
  • 2,112
  • 25
  • 29
user2559331
  • 529
  • 4
  • 3
6

Check the syntax here: http://www.fpdf.org/en/doc/output.htm It is: string Output([string dest [, string name [, boolean isUTF8]]]), so you have to write:

$pdf->Output('F', $filename, true); // save into the folder of the script

or e.g.:

$pdf->Output('F', '/var/www/html/wp-content/' . $filename, true); // save into some other location

or relative path:

$pdf->Output('F', '../../' . $filename, true); // to parent/parent folder

However, I am not sure if you can use windows absolute path...

David Najman
  • 487
  • 4
  • 7
2

Having struggled with this myself, there are three things one has to ensure, two of which are mentioned in other posts on this topic:

  1. Command: output('F', "xyz_file");
  2. Permissions on the server's destination directory need to allow writes for non-escalated privileges (i.e. drwxrwxrwx)
  3. Definition of content type: header("Content-type:application/pdf");
John
  • 93
  • 2
  • 8
9finger
  • 21
  • 1
1

Its because you are trying to save the file somewhere it doesnt want you to. Most probably because your havent set the permissions of the directory to 777.

If your PHP script is executed from a web-page (served by Apache, it is), then this code will be executed by the Apache (sometimes called www-data) user.

So, your Apache user needs to be able to write to the directory you're trying to write to.

Typically, you might have to give the write privilege to the other users of your system, using something like this from a command-line :

chmod o+w your_directory

The software you're using to upload your source files, if doing so using a GUI, should allow you to do that with a couple of chekboxes -- you need to check the "write" checkbox for the "others" users.

Vinicius Santana
  • 3,936
  • 3
  • 25
  • 42
1

Here is a simple solution to help you save the file to specified directory as well as having the url of the file which you can save to database.

$invoice = new phpinvoice();
...
...

$path = $_SERVER["DOCUMENT_ROOT"].'/uploads/';
$url = $_SERVER['HTTP_HOST'].'/uploads/';

$invoice->render($path.'.pdf','F');
return $url.'.pdf'; // print_r($url.'.pdf')

I hope this helps.

Harrison O
  • 1,119
  • 15
  • 20
0

Have you tried file upload? I think you and I might be trying to do the same thing and this seems to work. I am working on a shared drive as well.

http://php.net/manual/en/features.file-upload.post-method.php

J Noel
  • 1
  • 3
0

Likely permissions of your Apache service:

http://www.php.net/manual/en/function.opendir.php#87479

Paul Norman
  • 1,621
  • 1
  • 9
  • 20
  • Apache is installed into the Linux machine....so I am not yet sure how to check file permission from a linux machine... – Sharmin Dec 07 '10 at 13:50
  • @Sharmin switch to the user under witch Apache is running, list dir 'ls -al' see what permissions has the user on that dir, take care that parent dir needs to have write permissions for the Apache user, basically all the dirs and sub dirs from the path needs to allow write permissions to the Apache user . – Starlays Dec 04 '16 at 19:40
0

I solved like this:

public functon GeneratePdf(){
    ...
    PDF::Output("C:/xampp/htdocs/MyProject/doc.pdf","F"); 
}

I copied all directory path into Output method and I did not set further permissions for this.

I hope it helps you!!

-1

change the 'F' to 'D'. D forces download. So your $pdf->output line should look like this.

$pdf->Output($path,'D');
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/19197372) – Derek Brown Mar 22 '18 at 15:13
  • Sorry for the confusion. Hope this helps clarify it enough. – Ashen Deimos Mar 22 '18 at 15:56