I'm new to PhP and need some help please
I have the following code to open a Staff member's PDF payslip however when it opens it is easy enough for the User to see the path in the address bar and then be able to manipulate the path to see other people's payslips.
This would however not be possible it the PDF file opened outside the Browser (maybe downloading the file and then opening it in Adobe or so).
(There is a login procedure before it gets to this code)
Can someone help with this please?
if(isset($_POST['openpayslip'])){
if(!file_exists('payslips/'.$_SESSION['username'].'.pdf')){
echo "Payslip or function does not exist";
}
if(file_exists('payslips/'.$_SESSION['username'].'.pdf')){
header('Location: payslips/'.$_SESSION['username'].'.pdf');
}
}
You load the PDF file into the PHP file and then output it with a PDF header, so that people can only see one file, with some authorisation variables given.
For example, you user name is an uneditable SESSION value (in a perfect world), so you display the payslip with:
payslip.php
and in this PHP file you load
if(file_exists('payslips/'.$_SESSION['username'].'.pdf')){
$output = file_get_contents("payslips/".$_SESSION['username'].".pdf");
header("Content-type:application/pdf");
print $output;
}
This is a VERY rough and sketchy example above, but with this approach you essentially are showing the PDF file through the PHP file so the source material is never revealed directly to the end user.
Related
I have a PHP application that generates a CSV file and redirect the user to a static page linking to the file, just the example below :
https://www.example.com/public_html/static/temp/myfile.csv
Problem is, Chrome is opening the file instead of saving it. I need Chrome to save this file, as it would do with any other file like a zip or mp3, for instance.
Here is what I tried :
header('location:https://www.example.com/public_html/static/temp/myfile.csv');
header('Content-Disposition: attachment; filename=myfile.csv');
But no luck, Chrome keeps showing the myfile.csv contents instead of downloading it.
Any ideas ?
Thanks
Your argumentation in the comments has one never-ending misunderstanding: the Location header instructs any client to perform a new request to the given URI. With that the current request is over. Headers from the current request (i.e. Content-Disposition) aren't magically carried over to the next request.
In other words: your "static page linking to the file, just the example below" must send your wanted header.
Ultimately I'm sure it's not a Chrome problem either, but affects all internet browsers, as they easily detect the CSV data as text, hence being able to render/display that data instead of only being able to save it to a file.
With html5 you can set the "download" attr in an element.
Download it!
Source : http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download
After struggling with this issue for some days, the only real solution i got is to ZIP the file and then redirecting to the ZIP file instead of the CSV. By doing this, Chrome will download the ZIP file instead of opening it :
header('location:https://www.example.com/public_html/static/temp/myfile.csv.zip');
Sorry if I did not get this title right.
I have in my server the redirect.php script that receives a URL passed by the client user, fetches the content using file_get_contents() function and them shows it to the user with echo() function.
The problem is when the user points directly to a PDF or JPG file in that URL and them the script shows the file contents as binary code.
When I set the code to recognize when the requested URL points directly to a downloadable file,
What should be the function or header to echo to the user so that his browser ask him to download the file insted of showing it?
Do I have to first put it into a file inside my server or can I do it directly from a command like file_get_contents()? If I can do that without writing it to my server, it would be a much better approuch.
I can't point directly to the server because some sites are blocked by my employee and the third party company that does this service thinks that StakExchenge sites are malicious and not constructive and were tegged as online communities like Facebook.
Try this:
$sourcefile = "http://www.myremotewebsite.com/myfile.jpg";
$destfile = "myfile.jpg";
copy($sourcefile, $destfile);
I have a php script that allows you to input your name and then create a certificate, this part works fine but wish the completed certificate to be downloaded as a pdf.
I have searched many forums and sites but cannot figure out how to do this and am very confused and baffled.
The php live test version can be seen at
http://gmdesign.org.uk/xxx/h
Its taken me 4 months to just create that as php is just not my thing and it was 4 month of stress, am asking out of desperation now if someone can add the correct code for me so the certificate downloads as a pdf the code is here:
http://gmdesign.org.uk/xxx/h/script1.zip
when you enter name and proceed then generate certificate it works fine but when you click download the generated pdf is all wrong it does not show your name and also shows rest of page as if its a print out
thanks
If you want to use Dragon's solution, use $_GET variables, example...
At top of your page, place:
$fullName = $_GET['fullname'];
then you can use: hxxp://gmdesign.org.uk/xxx/h/certificate.php?fullname=Full Name
The $fullName var will then be whatever you put in it in the URL.
Example: <? echo($fullName); ?> would echo "Full Name"
Then I think he is saying to then save it on pdfcrowd like "http://gmdesign.org.uk/xxx/h/certificate.php?fullname=Full Name" which will save your certificate template as a PDF with the name "Full Name"
However, if you don't want to use a third party to convert the page to PDF, check out TCPDF like Koen recommended, or look at http://wkhtmltopdf.org
You can use phpToPDF library to create PDF from html file
You have already done coding to generate certificate html file now you have to give that file in phpToPDF libarary so it convert that html to pdf & store it on your server then you have to give that link to download button for download that certificate pdf file..
For phoToPDF library you need API key that you get once you registered with that library.
I'm generating a pdf file with html2fpdf.
$pdf = new HTML2FPDF();
$pdf->HTML2FPDF("P","mm","A4");
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->output('sample.pdf');
This sample works great. But:
How do I delete the pdf after the output? I just want to have links in my tool, the users can download the pdf and after that it shoud be deleted on the server.
How can I 'clean up' after generating the pdf?
You can use PHP's file deletion function called unlink()
Call this function with the full path to the generated PDF file (or any file for that matter) and PHP will delete that file.
http://php.net/manual/en/function.unlink.php
You don't necessarily have to delete the file immediately after the user has downloaded it. You can just as easily place all the generated files in one central folder and have a cron job execute a more general clean up script simply removing the older files.
One method could be -
Scan the contents of the folder using scandir().
Iterate over its files in a foreach loop..
Inspect the creation time of each file using filemtime().
If the creation time was over hour ago, delete the file using unlink().
Because you are generating the PDF file yourself within your PHP code, I didn't mention the permissions consideration. Here would be a good place to mention that your PHP must have the correct file system permissions in order to perform any action on the file system. You are creating a PDF file so it's safe to assume that you have the correct permissions to make changes to the file system but if you plan on using this unlink() function in other scripts make sure that the files you are dealing with have the correct permissions set.
If you don't add the 'F' flag to the output function there will be no pdf files stored on the server at all:
$pdf->output('sample.pdf', 'F'); //stores PDF on server
In your case the script itself behaves like an actual pdf file. So, creating a link to the script is just like a link to the pdf, except that the PDF is created every time the script is requested. To tell the browser it's a PDF the content-type response header must be set to application/pdf:
content-type: application/pdf
This way the broser knows that it's a pdf even if the URL is ending in a .php. You can use rewrite engine to make it end in pdf or whatever else.
Sending the headers is done by the fpdf/tcpdf. In short: you don't have to do any cleanup, because no pdf file is stored on the server.
If you wonder what the name is for than, try saving the pdf file. The recommanded name when saving will be sample.pdf.
Reference:
PHP header() function, at the examples there is one for sending pdf
FPDF::Output()
TCPDF::Output()
I'm trying to user HTML2FPDF (http://html2fpdf.sourceforge.net/) to create a PDF of a page, but I can't seem to get it to work properly.
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
http://portal.flyingstartlifestyle.360southclients.com/leanne/leanne.php <- the graph with the html2fpdf code at the bottom of the page.
HTML2FPDF code:
function createPDF() {
define('ABSPATH', dirname(__FILE__).'/');
require(ABSPATH.'classes/pdf/html2fpdf.php');
$pdf = new HTML2FPDF();
$pdf->AddPage();
$html = ob_get_contents();
//$html = htmlspecialchars($html);
if ($html) {
$fileName = "testing.pdf";
$pdf->WriteHTML($html);
$pdf->Output("pdfs/".$fileName);
echo "<p>PDF file is generated successfully! Click here to open it.</p>";
} else {
echo "<p>There has been an error in creating your PDF.</p>";
};
};
If I unhide the line "$html = htmlspecialchars($html);" it prints the pdf the text of the page, otherwise it creates an empty PDF. Any ideas how I can transfer my graph to a PDF?
Cheers
A few years back, I've been beating my head against the wall trying to convert HTML into PDF for days. What I wanted to do was really simple - make an invoice for customers into a PDF file. An image (logo) up on top, a few paragraphs, and a table with a list of charges.
The hole shaped like my head on the wall is still there. All of the free libraries that convert things to PDF - they all suck. I found one that sucks the least, it's DomPDF. At least that one ended up doing the job, after a week of suffering and debugging. It's not fast by any means, though (if you want to generate a complex PDF, you might want to do it off-thread).
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
jQuery is interpreted by the browser and not by the server. When you send the HTML to be rendered into PDF, it will not run the Javascript. You'll need to find a way to actually generate the image some other way.
I guess I could see a situation where you could use ajax to make a remote call and send all of the html that the js sees.
The remote call then would write a file of that html. The remote call would send back a file name for the pdf to be generated.
Your js then could provide a link to the processing page of the html2pdf that references the file created from the remote call.
This would work, but it might be a bit much.
Regards.