Showing generated PDF problem - php

I have a problem with showing generated PDF. The pdf is saved in dataBase. I'm continuing work of a guy that started work that way, so I need to work that way. There's no problem with saving the file on computer,it's working fine, but I need to show it in new window when it's read from dataBase...
And sorry about my English. :)

<?
$pdf=pdf_new();
pdf_open_file($pdf,"");
pdf_begin_page($pdf,600,800);
$font=pdf_findfont($pdf,'Helvetica-Bold','host',0);
pdf_setfont($pdf,$font,30.0);
pdf_show_xy($pdf,"Sample Text",50,600);
pd_set_parameter($pfd,"openaction","fitpage");
pdf_close($pdf);
$buf=pdf_get_buffer($pdf);
$len=strlen($buf);
header("Content-Type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=sample.pdf");
echo $buf;
pdf_delete($pdf);
?>

More information and some code example would be helpful.
You'll need to set the correct header before outputting to the window.
header('Content-type: application/pdf');

Related

PHP Readfile() not working for me - maybe header problem?

I checked similar posts and here's the problem: a portion of my codes :
if($_GET['dl']) {
$file=$_GET['dl'];
$file="../../rep/".$file;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
readfile($file);
exit;}
This code runs when user submit form and redirects on current PHP file. The result opens in browser rather than download. I checked a lot of other headers or modifications with no clue.
So I made a separated PHP file : download.php and paste the above code exactly on it. then redirect user to this new file And Problem solved! (File downloads without any problem)
So my question is what's the problem exactly?
Thanks from comments, the reason founded!
I had started the PHP tag <?php from second line in the file like this:
...
php tag start here <?php
Perhaps first line considered as an output.removed first line and now download starts properly ;)

Unable to download file using readfile

I try to offer a pdf-file-download, handeled in dwhandler.php.
All this is supposed to do is open the pdf-file or start a download of it.
I tried:
<?php
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename='Paper.pdf'");
header("Cache-Control: must-revalidate");
readfile("Paper.pdf");
exit;
?>
I also tried some other headers, but none worked.
The browser keeps opening the file directly in some weird coding. Here is a pic of the first few lines:
http://www.directupload.net/file/d/3865/h8waniq9_jpg.htm
Has anybody an idea why this is happening? I have no idea what is left to try...
Thank you very much!
is html not a option? I know you can offer files to download with simply using html 5 by doing
<a href="example.pdf" download>download not open it</a>
Take a look into: Forcing to download a file using PHP
<?php
// Do your Sql query
header('Location: http://host/files/pdf/file.pdf'); // redirect user to actualy location
?>

Why is my PDF download corrupted?

I have a script that generates a pdf using fpdf, this file is saved correctly on the server on my computer. In that same php file I run the following code to download the file. When i download the pdf and I check it in notepad, everything in the pdf is the same, except for the fact that it contains a lot of my previous files html at the beginning of it. The file saved to my server doesn't have any of that.
What could cause something like this to happen? I have no idea where to look for the source of this error, can anyone point me in the direction to finding the problem?
<?php
$filename=($name.$ran.'.pdf');
$pdf->Output($name.$ran.'.pdf');
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octetstream');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($filename));
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
readfile($name.$ran.'.pdf');
?>
I managed to figure things out, thanks to hakre! Essentially I just needed to clear out the output buffer using ob_clean () before using readfile() and the code worked!

PHP send stream to browser as zip archive

I am making a call(PHP server) to an API and I can confirm that I am getting a response, my code at the point where I know I am getting a response is:
$binaryF = $rsObject->makeCall('get', "/JobData/{$_GET["jID"]}", "?format=bin");
//header("Pragma: public"); // required
//header("Content-Type: application/zip");
//header("Content-Disposition: attachment; filename=ss.zip");
//header("Content-Length: " . filesize($binaryF.length));
//header("Content-Transfer-Encoding: binary");
file_put_contents('C:\ss.zip', $binaryF);
If I keep the code as is and click on the link that takes me to the page with this code on it, ss.zip is created and I open it and confirm that it has the correct content. The data is coming from an API call on the first line and is basically a zip package. If I remove the comments and comment out the file_put_contents line then the browser opens a file save dialog box but if I save it the archive is 0 bytes?
How do I send the content to a browser after retrieving it from the api call? I do not want to save it to disk first, I want to send it to the browser making the request directly.
Thank you
Jack
Ok. I changed it to:
header("Pragma: public"); // required
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=ss.zip");
header("Content-Length: " . strlen($binaryF));
header("Content-Transfer-Encoding: binary");
and now it opens the file save. I save it and it is 8 K but when I try t oopen it I get a message of: "Windows cannot open the folder" It complains that the zip is invalid? At least I am getting somewhere, thanks for the help so far!
Try (untested):
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename=ss.zip');
echo $binaryF;
$binaryF would need to be a string value for the filepath to the file you are trying to serve up in order for filesize() to work.
You can use strlen() to get the filesize for use in the ContentLength header.
You of course still need to actually output the file to the client browser.
I had some similar problems with PDF's, for me the Content-Length couldn't be calculated correctly with strlen() because it doesn't support multibyte-characters.
instead try this:
header("Content-Length: " . mb_strlen($binaryF));
See the documentation for more detailed information: http://www.php.net/manual/en/function.mb-strlen.php
Ok, #zerkms helped me by pointing out that I should use: strlen($binaryF) instead of filesize($binaryF.length) He helped me in a chat session by telling me to open the zip archive(with something simple like notepad) that I did get with file_put_contents('C:\ss.zip', $binaryF); This showed that what was produced was only partially correct, there was some HTML content that slipped into the response of the actual call. So all credit goes to #zerkms, thanks.
What worked? the following worked for me:
header("Pragma: public"); // required
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=ss.zip");
header("Content-Length: " . filesize($binaryF.length));
header("Content-Transfer-Encoding: binary");
echo $binaryF

file force download

I want to force a download.. In IE I get an error but there are no problems in FF
header("Content-Type: application/force-download\n");
header("Content-Disposition: attachment; filename=test.txt");
echo 'test file';
I just googled about your curious application/force-download mime type. I dont know, who did "invented" this, but it seems, that it just dont exists. See http://mimeapplication.org/force-download.html
This means, the IE probably dont like it. Use application/octet-stream instead. As far as I remember firefox opens a download dialog for every mime-type, that is either registered with "show download dialog", or it simply doesnt know. In this case FF probably doesnt know the type.
This might help you , it works for me
header('Content-Type: application/force-download');
header("Content-disposition: attachment; filename=$filename");
flush();
readfile($filename);
exit;
you can add other headers if desired :)

Categories