Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving file into a prespecified directory using FPDF

Tags:

php

pdf

fpdf

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...

like image 284
Sharmin Avatar asked Dec 02 '10 20:12

Sharmin


People also ask

What is FPDF library?

PyFPDF is a library for PDF document generation under Python, ported from PHP (see FPDF “Free”-PDF, a well-known PDFlib-extension replacement with many examples, scripts and derivatives).

What is FPDF error?

The FPDF Error Message will point you to the PHP Line that is sending some content. If you get no hint what File & Line send some content you probably have an encoding mismatch in your include / require Files.


2 Answers

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

like image 184
user2559331 Avatar answered Sep 28 '22 06:09

user2559331


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...

like image 26
David Najman Avatar answered Sep 28 '22 05:09

David Najman