Link to home
Start Free TrialLog in
Avatar of walker6o9
walker6o9

asked on

Force-download

Is there a way to force a download, prompt a save to download, or open in new window when a button is clicked or when a page is loaded?
ASKER CERTIFIED SOLUTION
Avatar of Michael701
Michael701
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of walker6o9
walker6o9

ASKER

That works fine.  However, when I try to incorporate just a simple version of that into my code, I get the following error:
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\annualReport\index.php:72) in C:\wamp\www\annualReport\index.php on line 278

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\annualReport\index.php:72) in C:\wamp\www\annualReport\index.php on line 279

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\annualReport\index.php:72) in C:\wamp\www\annualReport\index.php on line 280





 
<html>
<title>Coca-Cola 2007 Annual Report PDF's
</title>
 
<head>
 
<div align="left">
<font size="28" color="#DDDDDD" style="letter-spacing:-0.1em">2007 Annual Review</font>
<br>
<br>
<font size="20" color="#DDDDDD" style="letter-spacing:-0.1em">PDF Builder</font>
 
 
<style type="text/css">
.class1 A:link {text-decoration: underline; color:#F40009;}
.class1 A:visited {text-decoration: underline; color: #F40009;}
.class1 A:active {text-decoration: underline; color: blue;}
.class1 A:hover {text-decoration: underline; color: blue;}
 
 
.class2 A:link {text-align: left; text-decoration: underline; color:#FFFFFF; font-size: 12pt; }
.class2 A:visited {text-align: left; text-decoration: underline; color: #FFFFFF; font-size: 12pt; }
.class2 A:active {text-align: left; text-decoration: underline; color: blue; font-size: 12pt; }
.class2 A:hover {text-align: left; text-decoration: underline; color: blue;font-size: 12pt; }
 
</style>
 
 
 
</head>
<body>
 
<link rel="stylesheet" type="text/css" media="all" href="/css/main.css" />
  <link rel="stylesheet" type="text/css" media="print" href="/css/print.css" />
  <!--[if lte IE 6]><link rel="stylesheet" type="text/css" media="all" href="/css/ie5-6win.css" /><![endif]-->
  <!--[if IE 6]><link rel="stylesheet" type="text/css" media="all" href="/css/ie6.css" /><![endif]-->
  <!--[if IE 7.0]><link rel="stylesheet" type="text/css" media="all" href="/css/ie7.css" /><![endif]-->
<form name="myform" action="annual_report.pdf" method="POST">
 
<div style="width:500px"><br>
				<table  border="2" bgcolor="#990000" cellpadding="4" cellspacing="1">
				  <tr bgcolor="#999999">
					<td><font color="#FFFFFF">SELECT</font></td>
					<td><font color="#FFFFFF">Click To Preview</font></td>
						
				  </tr>
				  <tr bgcolor="#FFFFFF">
					 <td align="left" valign="top" nowrap><input type="checkbox" name="option[]" value="1"><br></td>
             		 <td width="100%" valign="middle"><span class="class1"><a href="/investors/pdfs/2007_annual_review.pdf" target="_blank" title="Opens in new window" onClick="javascript: pageTracker._trackPageview('/downloads/investors_2007_ar_completePDF');">Complete 
                2007 Annual Review</a></span></td>
					
				 </tr>
				  <tr bgcolor="#FFFFFF">
					<td align="left" valign="top" nowrap><input type="checkbox" name="option[]" value="2"><br></td>
					<td width="100%" valign="middle"><span class="class1"><a href="/investors/pdfs/2007_annual_review/Coke_AR07_Financial_Data.pdf" target="_blank" title="Opens in new window" onClick="javascript: pageTracker._trackPageview('/downloads/investors_2007_ar_financial_dataPDF');">Financial Data</a></span></td>
					  </tr>
				  
				</table>
 
 
 
 
 
<br>
<input type="submit" name="option[]" value="Generate PDF">
</div>
</div>
</form>
 
 
<?php
 
 
$page1=0;
$page2=0;
$page3=0;
$page4=0;
$page5=0;
$page6=0;
$page7=0;
$page8=0;
$page9=0;
$page10=0;
$run = 0;
 
if($_POST['option']){
   $opts = $_POST['option'];
   foreach ($opts as $o) {
      if($o == 1){$page1=1;}
      if($o == 2){$page2=1;}
      if($o == 3){$page3=1;}
      if($o == 4){$page4=1;}
      if($o == 5){$page5=1;}
      if($o == 6){$page6=1;}
      if($o == 7){$page7=1;}
      if($o == 8){$page8=1;}
      if($o == 9){$page9=1;}
      if($o == 10){$page10=1;}
      if($o == "Generate PDF"){$run=1;}
   }
}
 
 
 
 
 
/* $Id: starter_pdfmerge.php,v 1.1.2.1 2007/09/27 12:58:00 rjs Exp $
 *
 * PDF merge starter:
 * Merge pages from multiple PDF documents; interactive elements (e.g. 
 * bookmarks) will be dropped.
 *
 * required software: PDFlib+PDI/PPS 7
 * required data: PDF documents
 */
 
/* This is where the data files are. Adjust as necessary. */
 
 
if($page1 !=0 or $page2 !=0 or $page3 !=0 or $page4 !=0 or $page5 !=0 or $page6 !=0 or $page7 !=0 or $page8 !=0 or $page9 !=0 or $page10 !=0)
{
 
 
 
$searchpath = "C:\wamp\www\annualReport\data";
$outfilename = "C:\wamp\www\annualReport\starter_pdfmerge.pdf";
 
$pdffiles = array(
	
);
 
if($page1 ==1)
{
array_push($pdffiles, "/investors/pdfs/2007_annual_review.pdf");
}
if($page2 ==1)
{
array_push($pdffiles, "/investors/pdfs/2007_annual_review/Coke_AR07_Financial_Data.pdf");
}
if($page3 ==1)
{
array_push($pdffiles, "/investors/pdfs/2007_annual_review/Coke_AR07_Executive_Letter.pdf");
}
if($page4 ==1)
{
array_push($pdffiles, "/investors/pdfs/2007_annual_review/Coke_AR07_Business_Profile.pdf");
}
if($page5 ==1)
{
array_push($pdffiles, "/investors/pdfs/2007_annual_review/Coke_AR07_Company_Highlights.pdf");
}
if($page6 ==1)
{
array_push($pdffiles, "/investors/pdfs/2007_annual_review/Coke_AR07_Year_In_Review.pdf");
}
if($page7 ==1)
{
array_push($pdffiles, "/investors/pdfs/2007_annual_review/Coke_AR07_Marketing_Highlights.pdf");
}
if($page8 ==1)
{
array_push($pdffiles, "/investors/pdfs/2007_annual_review/Coke_AR07_Operating_Group_Highlights.pdf");
}
if($page9 ==1)
{
array_push($pdffiles, "/investors/pdfs/2007_annual_review/Coke_AR07_BoardofDirectors_Management.pdf");
}
if($page10 ==1)
{
array_push($pdffiles, "/investors/pdfs/2007_annual_review/Coke_AR07_ForwardLookingStatements_ShareownerInformation.pdf");
}
 
 
 
 
 
 
 
try {
    $p = new PDFlib();
 
 
    # This means we must check return values of load_font() etc.
    $p->set_parameter("errorpolicy", "return");
 
    $p->set_parameter("SearchPath", $searchpath);
 
    if ($p->begin_document($outfilename, "") == 0)
	die("Error: " . $p->get_errmsg());
 
    $p->set_info("Creator", "PDFlib starter sample");
    $p->set_info("Title", "starter_pdfmerge");
 
    foreach ($pdffiles as $pdffile) { 
	/* Open the input PDF */
	$indoc = $p->open_pdi_document($pdffile, "");
	if ($indoc == 0) {
	    printf("Error: %s\n", $p->get_errmsg());
	    continue;
	}
 
	$endpage = $p->pcos_get_number($indoc, "/Root/Pages/Count");
 
	/* Loop over all pages of the input document */
	for ($pageno = 1; $pageno <= $endpage; $pageno++) {
	    $page = $p->open_pdi_page($indoc, $pageno, "");
 
	    if ($page == 0) {
		printf("Error: %s\n", $p->get_errmsg());
		continue;
	    }
	    /* Dummy $page size; will be adjusted later */
	    $p->begin_page_ext(10, 10, "");
 
	    /* Create a bookmark with the file name */
	    if ($pageno == 1) {
		$p->create_bookmark($pdffile, "");
	    }
 
	    /* Place the imported $page on the output $page, and
	     * adjust the $page size
	     */
	    $p->fit_pdi_page($page, 0, 0, "adjustpage");
	    $p->close_pdi_page($page);
 
	    $p->end_page_ext("");
	}
	$p->close_pdi_document($indoc);
    }
 
    $p->end_document("");
    echo"<br>";
	echo "<div style='text-align:left'>";
   // echo "<span class='class2'><a href = 'annual_report.pdf' target='_blank'>Your PDF Has Been Created<img src='/images/lg_pdf.gif'></a></span>";
 
 
	echo "</div>";
	
	
	$filename = "annual_report.pdf";
 
header("Content-Length: " . filesize($filename));
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename=annual_report.pdf');
 
readfile($filename);
 
}
catch (PDFlibException $e) {
    die("PDFlib exception occurred in starter_pdfmerge sample:\n" .
	"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
	$e->get_errmsg() . "\n");
}
catch (Exception $e) {
    die($e);
}
 
$p = 0;
}
?>
 
 
 
</body>
</html>

Open in new window

Oh, that should say line 243, 244, and 245.  I cut out some stuff to make the posted code a little shorter.
That code MUST be a separate php program. Say you call it download_annual_report.php

then in your main page you can have <a href='download_annual_report.php'>Download report</a>
Is there a way I can make that part be called rather than having to click on a link to make it work?
you could use
location('download_annual_report.php');
at the end of your existing code.

Note: If you were to remove ALL html code output from the current program, then it could be included at the bottom of that code. and the very first line of your code MUST have <?PHP any white space will cause that header already sent message.
Why does the html code need to be removed?
Also, I tried adding
location('download_annual_report.php');

in place of the headers and I got the following error


Fatal error: Call to undefined function location() in C:\wamp\www\annualReport\index.php on line 194

oops, me not thinking, it's

header("location: download_annual_report.php");

but you know what, this still needs the html to be removed.

Headers can not be issued after the start of any html to the browser.
Is there anyway I can either
a) go to a new url
b) prompt to save

?

It seems like must be a way to do this even if you have html you need on the page
If you have html on the page, then there must be a reason for the user to see it. this means you'll have to make a <a href> link to click on once they've read the page.
I'm trying to do this without making a link.  I already did this with a link, but that adds an extra step and it makes for better UX without it.
well to do it without a link, you'll need to not send any html to the browser.

just go through and comment out all html references, if you get error "Headers already sent" then you'll know you missed something. Just make sure the very first line is <?PHP
I have html that has to come before the php though in order to set up the title and some checkboxes.
Ok then the user does have to view the page to see the title and check boxes. this means that you can't automatically just to the download page.
I don't want to automatically download the page.  I want it to automatically download a pdf when my php function runs.
Is the pdf function a separate program? If it's part of a program that generates html to the browser then you can't issue the header commands to force the download.
Okay, so I have an idea for how to fix this that you may be able to help me with.

Here's my issue.  I have a bunch of checkboxes that user may or may not click on.  When they press a submit button, that information needs to be passed through a php function, which creates a pdf.  After the pdf is created, it needs to be automatically loaded.

If there is a way I can pass the information involving whether or not the checkboxes have been clicked on to a separate php file, then I can use the header program to automatically load the pdf.  But I don't know how to do that.
so is your question how to know if a checkbox has been checked?

they are available (if checked) as $_POST['checkbox_name']['value']
no, i figured out what i was asking is not possible
Can you use fopen or any other function in php to open a file in a new window or save it to a disk?
not from php, you could use java script to open a new window.
I got it working.