Ninjakreborn Posted April 26, 2007 Share Posted April 26, 2007 Ok, I have a form leading to a page where a pdf file is outputted (created). Form <p class="memWel memGrn">View Statements</p> <p class="memDate"> <br /><br /> <p class="memDate"> To view and/or print a specific monthly statement, select a statement period from the drop-down list below and click <strong>Go</strong>. </p> <form name="statement" id="statement" action="transpdf.php" method="post"> <select name="month"> <option>Select A Billing Cycle</option> <option>----------------------</option> <?php // something quick here, it simply outputs a drop down menu of the last 3 months. // It also accounts for leap year, and special days, and all of that other good // stuff. list($m, $y) = explode('-', date('m-Y')); for ($i = -3; $i < 1; $i++) { $ts = mktime(0,0,0,$m+$i,1,$y); $n = date('F', $ts); $days = date('t', $ts); echo "<option value=\"$ts\">$n 1 - $days</option>\n"; } ?> </select> <input name="go" type="image" src="../common/images/button_go.jpg" /> </form> <p class="memDate"> The statement you select will open up as an Adobe PDF file. You will need Adobe Reader to view the file.<br /> <a href="http://www.adobe.com/products/reader"><img border="0" src="<?php echo $wwwpath; ?>common/images/button_getadobereader.gif" align="left" /></a> </p> PDF OUTPUT FILE <?php session_start(); header("application/pdf object"); //IE 6 Fix if(!$_SESSION['cymember']) { header('Location: index.php'); exit; } require_once('../common/config.php'); require_once('../common/functions.php'); require_once('../common/class.ezpdf.php'); function getlastday($month, $year) { for ($day = 28; $day < 32; $day++) { if (!checkdate($month, $day, $year)) return $day-1; } } ?> <?php $pdf =& new Cezpdf(); $pdf->selectFont('../common/fonts/Helvetica.afm'); $monthvar = mysql_real_escape_string($_POST['month']); // get post //echo "<br />Month var: " . $monthvar; $month = date("m", $monthvar); // get month itself //echo "<br />Month: " . $month; $year = date("Y", $monthvar); // get year itself //echo "<br />Year: " . $year; $first = "1/" . date("m/Y", $monthvar); // get first day<br /> //echo "<br />first 1: " . $first; $first = strtotime($first); // convert first day to timestamp //echo "<br />first 2: " . $first; $last = getlastday($month, $year); //echo "<br />last 1: " . $last; $last = $last . date("/m/Y", $monthvar); //echo "<br />last 2: " . $last; $last = strtotime($last); //echo "<br />last 3: " . $last; //exit; $select = "SELECT * FROM transactions WHERE submitted >= '$first' and submitted <= '$last'"; $query = mysql_query($select); while ($row = mysql_fetch_array($query)) { $pdf->ezText(" -- Start Transaction -- User Id: " . $row['userid'] . " Biller/Account: " . $row['biller_or_account'] . " Type: " . $row['type'] . " Amount: " . $row['amount'] . " Date: " . date("d/m/Y", $row['date']) . " Date Submitted: " . date("d/m/Y", $row['submitted']) . " Status: " . $row['status'] . " -- Stop Transaction -- "); } $pdf->ezStream(); ?> This works completely, it brings them to the page, and it outputs a PDF with the data. Is there a way to "instead" make it when they click the button for the form, it prompts that same pdf file as a download instead. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/ Share on other sites More sharing options...
Ninjakreborn Posted April 26, 2007 Author Share Posted April 26, 2007 Ok, I modified my view page to this. <?php session_start(); // We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf readfile('transpdf.php'); if(!$_SESSION['cymember']) { header('Location: index.php'); exit; } require_once('../common/config.php'); require_once('../common/functions.php'); require_once('../common/class.ezpdf.php'); function getlastday($month, $year) { for ($day = 28; $day < 32; $day++) { if (!checkdate($month, $day, $year)) return $day-1; } } ?> <?php $pdf =& new Cezpdf(); $pdf->selectFont('../common/fonts/Helvetica.afm'); $monthvar = mysql_real_escape_string($_POST['month']); // get post //echo "<br />Month var: " . $monthvar; $month = date("m", $monthvar); // get month itself //echo "<br />Month: " . $month; $year = date("Y", $monthvar); // get year itself //echo "<br />Year: " . $year; $first = "1/" . date("m/Y", $monthvar); // get first day<br /> //echo "<br />first 1: " . $first; $first = strtotime($first); // convert first day to timestamp //echo "<br />first 2: " . $first; $last = getlastday($month, $year); //echo "<br />last 1: " . $last; $last = $last . date("/m/Y", $monthvar); //echo "<br />last 2: " . $last; $last = strtotime($last); //echo "<br />last 3: " . $last; //exit; $select = "SELECT * FROM transactions WHERE submitted >= '$first' and submitted <= '$last'"; $query = mysql_query($select); while ($row = mysql_fetch_array($query)) { $pdf->ezText(" -- Start Transaction -- User Id: " . $row['userid'] . " Biller/Account: " . $row['biller_or_account'] . " Type: " . $row['type'] . " Amount: " . $row['amount'] . " Date: " . date("d/m/Y", $row['date']) . " Date Submitted: " . date("d/m/Y", $row['submitted']) . " Status: " . $row['status'] . " -- Stop Transaction -- "); } $pdf->ezStream(); ?> It's coming back with an error saying it was corrupted or something (AFTER) it's downloaded. So basically this prompts for downlaod but it's corrupted, and before when it was just showing it as a page, it created it properly and showed the data from it. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239080 Share on other sites More sharing options...
trq Posted April 26, 2007 Share Posted April 26, 2007 So... your question is? Your title doesn't seem to match your problem. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239135 Share on other sites More sharing options...
Ninjakreborn Posted April 26, 2007 Author Share Posted April 26, 2007 The current problem is when I show the current pdf file (as shown above) it streams through the browser. I need it to prompt to download to teh person's computer but nothing is working, any advice? Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239138 Share on other sites More sharing options...
Ninjakreborn Posted April 26, 2007 Author Share Posted April 26, 2007 Anybody, because I am fresh out of ideas. I spent almost a full day getting the pdf creation class up and running, and getting use to it. Finally got the query working and all those date calculations done, and now he's telling me he wants it to be a download instead (like when they click on the submit button to the form, I need to do all the calculations, get it to do what it's doing now). however instead of streaming through the browser it's needs to prompt as a download, any advice. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239179 Share on other sites More sharing options...
KevinM1 Posted April 26, 2007 Share Posted April 26, 2007 Anybody, because I am fresh out of ideas. I spent almost a full day getting the pdf creation class up and running, and getting use to it. Finally got the query working and all those date calculations done, and now he's telling me he wants it to be a download instead (like when they click on the submit button to the form, I need to do all the calculations, get it to do what it's doing now). however instead of streaming through the browser it's needs to prompt as a download, any advice. You probably have to do something with http headers. Take a look at the following link: http://www.daniweb.com/code/snippet75.html Remember, if all else fails, use Google. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239182 Share on other sites More sharing options...
Ninjakreborn Posted April 26, 2007 Author Share Posted April 26, 2007 I tried that, it's not working with what I am trying to do. I have the headers on there, I tried them in multiple way's. When the thingcomes to the browser, all it's doing is outputting it, one time I got it to force the download, and then all of a sudden the file was corrupt. I am going in circles with this one, and a deadline looming over my head, I didn't know he was going to ask for it to be uploadable. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239185 Share on other sites More sharing options...
roopurt18 Posted April 26, 2007 Share Posted April 26, 2007 This is a code snippet I used to dump a PDF file created with PDFLib. Do not just blindly copy and paste this expecting it to work. Take note of the headers and try to apply them to your situation. function DirectOut(){ while (@ob_end_clean()); $p = $this->PDF['PDF']; $buf = PDF_get_buffer($p); $len = strlen($buf); header("Content-disposition: attachment; filename=Catalog.pdf"); header("Cache-Control: cache, must-revalidate"); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Pragma: public"); header("Expires: 0"); print $buf; exit(); } Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239188 Share on other sites More sharing options...
Ninjakreborn Posted April 26, 2007 Author Share Posted April 26, 2007 Ok. I went through and reformatted all of my code, and re-commented everything to make sure everything was very readable. Then using your example I recreated my headers again carefully trying to make sure everything was in order. In the end it didn't work. Again it's still streaming straight into the browser without any prompts for uploading or anything. Here is the new code, with the headers changes based off your example. <?php session_start(); // session for verifying login // headers to force download header("Content-disposition: attachment; filename=download.pdf"); header("Cache-Control: cache, must-revalidate"); header("Content-type: application/pdf"); // end headers to force download // check that they are logged in if(!$_SESSION['cymember']) { header('Location: index.php'); exit; } // require config file, functions file, and pdf class (config/functions has db info) require_once('../common/config.php'); require_once('../common/functions.php'); require_once('../common/class.ezpdf.php'); $pdf =& new Cezpdf(); // start pdf class $pdf->selectFont('../common/fonts/Helvetica.afm'); // get font $monthvar = mysql_real_escape_string($_POST['month']); // get post $month = date("m", $monthvar); // get month itself $year = date("Y", $monthvar); // get year itself // first day of the month $first = "1/" . date("m/Y", $monthvar); // get first day<br /> $first = strtotime($first); // convert first day to timestamp // last day of the month $last = getlastday($month, $year); $last = $last . date("/m/Y", $monthvar); $last = strtotime($last); // run query here (fully tested, this part works) $select = "SELECT * FROM transactions WHERE submitted >= '$first' and submitted <= '$last'"; $query = mysql_query($select); // while it's going through rows while ($row = mysql_fetch_array($query)) { // put information into the pdf processor $pdf->ezText(" -- Start Transaction -- User Id: " . $row['userid'] . " Biller/Account: " . $row['biller_or_account'] . " Type: " . $row['type'] . " Amount: " . $row['amount'] . " Date: " . date("d/m/Y", $row['date']) . " Date Submitted: " . date("d/m/Y", $row['submitted']) . " Status: " . $row['status'] . " -- Stop Transaction -- "); } // this is where we throw out the pdf file information. $pdf->ezStream(); ?> Any Advice? Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239203 Share on other sites More sharing options...
roopurt18 Posted April 26, 2007 Share Posted April 26, 2007 I notice you left off three of the headers, which could make a difference. Other than that I suppose it looks pretty solid. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239212 Share on other sites More sharing options...
KevinM1 Posted April 26, 2007 Share Posted April 26, 2007 I believe your problem is your $pdf -> ezStream(); statement. From what I've seen, that streams the pdf in question to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239214 Share on other sites More sharing options...
Ninjakreborn Posted April 26, 2007 Author Share Posted April 26, 2007 I didn't have the variables because I wasn't using output buffering. I know there are 2 functions with this class I am using the ez_Streaming and ez_Output The output shoots out the raw pdf code, the streaming streams it. I really don't understand why nothing is working, and at time's when it does prompt for download it's always corrupted. I believe your problem is your Quote $pdf -> ezStream(); statement. From what I've seen, that streams the pdf in question to the browser. Ok, that is a true statement, the definitions (based on what the class say's are Output As an alternative to streaming the output directly to the browser, this function simply returns the pdf code. No headers are set, so if you wish to stream at a later time, then you will have to manually set them. This is ideal for saving the code to make a pdf file, or showing the code on the screen for debug purposes. If the 'debug' parameter is set to 1, then the only effect at the moment is that the compression option is not used for the content streams, so that they can be viewed clearly. Stream Used for output, this will set the required headers and output the pdf code. The options array can be used to set a number of things about the output: 'Content-Disposition'=>'filename' sets the filename, though not too sure how well this will work as in my trial the browser seems to use the filename of the php file with .pdf on the end. 'Accept-Ranges'=>1 or 0 - if this is not set to 1, then this header is not included, off by default this header seems to have caused some problems despite the fact that it is supposed to solve them, so I am leaving it off by default. 'compress'=> 1 or 0 - apply content stream compression, this is on (1) by default. Ok based on that I just ran a test, same code except with the output instead of the stream. It ALWAYS prompts for download like this, however after I download it to my computer I try to open it and I get the following message. Adobe Reader could not open "download.pdf" because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachemnt and not correctly decoded) That is always the error message I get from adobe when trying to get it to open on my computer. In the end I am stuck with the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239223 Share on other sites More sharing options...
KevinM1 Posted April 26, 2007 Share Posted April 26, 2007 Is there any way you can contact the person who wrote the pdf class? Maybe the output method has known problems (and hopefully a fix) you're not aware of. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239226 Share on other sites More sharing options...
Ninjakreborn Posted April 26, 2007 Author Share Posted April 26, 2007 No problems with it, I am 100% sure of that. I have looked all over his forums, and it seems the class is pretty populater because it's number 1 in google for a bunch of search queries. http://www.ros.co.nz/pdf/ I know it's something I am doing, I just have to figure out what it is. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239235 Share on other sites More sharing options...
roopurt18 Posted April 26, 2007 Share Posted April 26, 2007 There is a high likelihood that data not belonging in the PDF is being sent as well as the PDF information. Whether its your code sending this data or the class is hard to determine from our standpoint. You might try inserting this line from the snippet I pasted somewhere near the top of your script: while (@ob_end_clean()); Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239237 Share on other sites More sharing options...
Ninjakreborn Posted April 26, 2007 Author Share Posted April 26, 2007 I got it, I found a contribution in the forums, that was specifically for downloads. Works perfectly, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/48784-solved-forcing-upload/#findComment-239252 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.