Hi, I have a custom module that's a multi-page form that collects one value from a user for each page of the form (3 pages, 3 total values collected). On the final page of the form (the recommendations page) it displays recommended resources from our database that match the user's selections. Because we need to put all 'timely events' first, followed by 'quick study' events, and finally 'deep dive' events the recommendations page uses 3 SQL queries to output the data to the screen.

All that works fine. However, now we'd like to add some way to allow the user to EXPORT the recommendations they see on the screen to a CSV file (or some other type) with an 'export' link. That's why I'm here asking you guys...does anyone know a great way to make this happen?

Here's the recommendations page code:

// function to return results from DB based on what user entered
function pathfindertool_recommendations_page() {

	//first test if variables passed from Pathfinder tool
	if (isset($_SESSION['role']) AND isset($_SESSION['level']) AND isset($_SESSION['topic'])) {
		// set variables
		$role = $_SESSION['role'];
		$level = $_SESSION['level'];
		$topic = $_SESSION['topic'];
	
		// gather session variables and give them string values
		if ($role == 0) {
			$role = "Grant Writer";
		}
		elseif ($role == 1) {
			$role = "Nonprofit Administrator";
		}
		elseif ($role == 2) {
			$role = "Nonprofit Founder";
		}
		else {
			$role = "Consultant";
		}
	
		if ($level == 0) {
			$level = "Novice";
		}
		elseif ($level == 1) {
			$level = "Experienced";
		}
		else {
			$level = "Professional";
		}
	
		if ($topic == 0) {
			$topic = "Grants Research";
		}
		elseif ($topic == 1) {
			$topic = "Grant Writing";
		}
		elseif ($topic == 2) {
			$topic = "Grant Management";
		}
		else {
			$topic = "Strategic Planning";
		}
	
		// set $output var
		$output ='';
	
		// print and email link styling
		$output .= '<div class="print-link-rec"><span class="print_html"><a class="print-page" rel="nofollow" title="Display a printer-friendly version of this page." href="/print/pathfinder/recommendations"><img class="print-icon print-icon-margin" width="16px" height="16px" title="Printer-friendly version" alt="Printer-friendly version" src="/sites/all/modules/contrib/print/icons/print_icon.png" typeof="foaf:Image">Printer-friendly version</a></span></div>';
	
		// progress bar
		$output .= '<div class="progress-bar" id="progress-bar"><span id="step-one">Primary Role</span><span id="arrow1"> > </span><span id="step-two">Experience Level</span><span id="arrow2"> > </span><span id="step-three">Topics of Interest</span><span id="arrow3"> > </span> <span id="recommendations" style="background-color: #003863;">Recommendations</span></div>';
	
		// remind user what they selected in form
		// but first, set pro and grants research to the preferred edits
		if ($level == "Professional") {
			$level2 = "Advanced";
		} else {
			$level2 = $level;
		}
		if ($topic == "Grants Research") {
			$topic2 = "Grant Research";
		} else {
			$topic2 = $topic;
		}
		$output .= '<div class="selected">You selected <em>' . $role . '</em>, <em>' . $level2 . '</em>, and <em>' . $topic2 . '</em>.</div>';
	
		// sql for Timely Events
		$result = db_query("SELECT n.title, n.nid, n.status, n.type, fdfty.field_type_value, fdfi.entity_id, fdfi.field_icon_tid, fdfti.entity_id, fdfti.field_taxonomy_icon_fid, fm.uri, fm.fid, ua.source, ua.alias, el.field_experience_level_value, fdb.body_value, fdfr.field_role_value, fdft.field_topics_value, fdfed.field_event_dates_value, fdfed.field_event_dates_value2, fdfl.field_location_value, fdfl.entity_id, fdfo.field_organization_value, fdfo.entity_id FROM {node} AS n LEFT JOIN {field_data_field_type} AS fdfty ON n.nid=fdfty.entity_id LEFT JOIN {field_data_field_icon} AS fdfi ON n.nid=fdfi.entity_id LEFT JOIN {field_data_field_taxonomy_icon} AS fdfti ON fdfi.field_icon_tid=fdfti.entity_id LEFT JOIN {file_managed} AS fm ON fdfti.field_taxonomy_icon_fid=fm.fid LEFT JOIN {url_alias} AS ua ON CONCAT('node/', nid)=source LEFT JOIN {field_data_field_experience_level} AS el ON n.nid=el.entity_id LEFT JOIN {field_data_body} AS fdb ON n.nid=fdb.entity_id LEFT JOIN {field_data_field_role} AS fdfr ON n.nid=fdfr.entity_id LEFT JOIN {field_data_field_topics} AS fdft ON n.nid=fdft.entity_id LEFT JOIN {field_data_field_event_dates} AS fdfed ON n.nid=fdfed.entity_id LEFT JOIN {field_data_field_location} AS fdfl ON n.nid=fdfl.entity_id LEFT JOIN {field_data_field_organization} AS fdfo ON n.nid=fdfo.entity_id WHERE n.status=:status AND n.type=:resource AND (fdfty.field_type_value=:webinar OR fdfty.field_type_value=:workshoptraining OR fdfty.field_type_value=:conference) AND el.field_experience_level_value=:level AND fdfr.field_role_value=:role AND fdft.field_topics_value=:topic AND ((fdfed.field_event_dates_value IS NULL) OR (fdfed.field_event_dates_value >= CURRENT_DATE())) GROUP BY n.nid, n.title ORDER BY CASE WHEN fdfty.field_type_value='1' THEN 0 WHEN fdfty.field_type_value='2' THEN 1 WHEN fdfty.field_type_value = '3' THEN 2 END, fdfed.field_event_dates_value, n.title", array(':status' => '1', ':resource' => 'resource', ':webinar' => '1', ':workshoptraining' => '2', ':conference' => '3', ':level' => $level, ':role' => $role, ':topic' => $topic));
	
		// count how many results returned
		$num_rows = $result->rowCount();
		$counter = $num_rows;
	
		// test if results
		if ($num_rows > 0) {

			// output header
			$output .= '<h2 class="recheader" id="timevents">Timely Events</h2>';
	
			// return results to page
			while ($row = $result->fetchAssoc()) {
	
				// set variables
				$date1 = $row['field_event_dates_value'];
				$timestamp = strtotime($date1);
				$date2 = $row['field_event_dates_value2'];
				$timestamp2 = strtotime($date2);				
				$title = $row['title'];
				$nid = $row['nid'];
				$alias = $row['alias'];
				// set image to display correctly for category
				$icon = image_style_url('thumbnail', $row['uri']);
				// select price image
				$result2 = db_query("SELECT n.title, n.nid, fdfp.entity_id, fdfp.field_price_tid, fdfcm.entity_id, fdfcm.field_costs_money_fid, fm2.fid, fm2.uri FROM {node} AS n LEFT JOIN {field_data_field_price} AS fdfp ON n.nid=fdfp.entity_id LEFT JOIN {field_data_field_costs_money} AS fdfcm ON fdfp.field_price_tid=fdfcm.entity_id LEFT JOIN {file_managed} AS fm2 ON fdfcm.field_costs_money_fid=fm2.fid WHERE n.nid = :nid", array(':nid' => $nid));
				while ($row2 = $result2->fetchAssoc()) {
					if ($row2['uri'] == 'public://dollar-sign.png') {
						$dollar = image_style_url('thumbnail', $row2['uri']);
					} else {
						$dollar = "";
					}
				}
				$desc = $row['body_value'];
				$ftv = $row['field_type_value'];
				// set dates to display correctly and add other output to make page styled
				if ($date1 != $date2) {
					$output .= '<div class="recentry"><img src="' .$icon. '" id="icon" alt="Timely Event" title="Timely Event" />';
						if ($dollar != "") { 
							$output .= '<img src="' .$dollar. '" id="price" alt="Costs Money" title="Costs Money" />'; 
						} else { 
							$output .= ""; 
						} 
					$location = $row['field_location_value'];
					$organization = $row['field_organization_value'];
					$output .= '<h2 class="rectitle"><a href="../' . $alias . '">' . $title . '</a></h2><div class="recdates">' . date('l, F j, Y', $timestamp) . ' to ' . date('l, F j, Y', $timestamp2) . '</div><div class="recdesc">';
					if (($location != NULL) && (($ftv == '2') || ($ftv == '3'))) {
						$output .= '<div class="location">' . $location . '</div>';
					}
					if (($organization != NULL) && (($ftv == '1') || ($ftv == '2') || ($ftv == '3'))) {
						$output .= '<div class="org">' . $organization . '</div>';
					}
					$output .= '<div class="description">' . $desc  . '</div></div></div>';
				} elseif ($date1 == $date2) {
					$output .= '<div class="recentry"><img src="' .$icon. '" id="icon" alt="Timely Event" title="Timely Event" />';
						if ($dollar != "") { 
							$output .= '<img src="' .$dollar. '" id="price" alt="Costs Money" title="Costs Money" />'; 
						} else { 
							$output .= ""; 
						} 
					$location = $row['field_location_value'];
					$organization = $row['field_organization_value'];
					$output .= '<h2 class="rectitle"><a href="../' . $alias . '">' . $title . '</a></h2><div class="recdates">' . date('l, F j, Y', $timestamp) . '</div><div class="recdesc">';
					if (($location != NULL) && (($ftv == '2') || ($ftv == '3'))) {
						$output .= '<div class="location">' . $location . '</div>';
					}
					if (($organization != NULL) && (($ftv == '1') || ($ftv == '2') || ($ftv == '3'))) {
						$output .= '<div class="org">' . $organization . '</div>';
					}
					$output .= '<div class="description">' . $desc  . '</div></div></div>';
				} else {
					$output .= '<div class="recentry"><img src="' .$icon. '" id="icon" alt="Timely Event" title="Timely Event" />';
					$output .= '/>';
						if ($dollar != "") { 
							$output .= '<img src="' .$dollar. '" id="price" alt="Costs Money" title="Costs Money" />'; 
						} else { 
							$output .= ""; 
						} 
					$location = $row['field_location_value'];
					$organization = $row['field_organization_value'];
					$output .= '<h2 class="rectitle"><a href="../' . $alias . '">' . $title . '</a></h2><div class="recdesc">';
					if (($location != NULL) && (($ftv == '2') || ($ftv == '3'))) {
						$output .= '<div class="location">' . $location . '</div>';
					}
					if (($organization != NULL) && (($ftv == '1') || ($ftv == '2') || ($ftv == '3'))) {
						$output .= '<div class="org">' . $organization . '</div>';
					}
					$output .= '<div class="description">' . $desc  . '</div></div></div>';
				}
			}
		}

		// sql for Quick Study items
		$result = db_query("SELECT n.title, n.nid, n.status, n.type, fdfty.field_type_value, fdfi.entity_id, fdfi.field_icon_tid, fdfti.entity_id, fdfti.field_taxonomy_icon_fid, fm.uri, fm.fid, ua.source, ua.alias, el.field_experience_level_value, fdb.body_value, fdfr.field_role_value, fdft.field_topics_value, fdfl.field_location_value, fdfl.entity_id, fdfo.field_organization_value, fdfo.entity_id FROM {node} AS n LEFT JOIN {field_data_field_type} AS fdfty ON n.nid=fdfty.entity_id LEFT JOIN {field_data_field_icon} AS fdfi ON n.nid=fdfi.entity_id LEFT JOIN {field_data_field_taxonomy_icon} AS fdfti ON fdfi.field_icon_tid=fdfti.entity_id LEFT JOIN {file_managed} AS fm ON fdfti.field_taxonomy_icon_fid=fm.fid LEFT JOIN {url_alias} AS ua ON CONCAT('node/', nid)=source LEFT JOIN {field_data_field_experience_level} AS el ON n.nid=el.entity_id LEFT JOIN {field_data_body} AS fdb ON n.nid=fdb.entity_id LEFT JOIN {field_data_field_role} AS fdfr ON n.nid=fdfr.entity_id LEFT JOIN {field_data_field_topics} AS fdft ON n.nid=fdft.entity_id LEFT JOIN {field_data_field_location} AS fdfl ON n.nid=fdfl.entity_id LEFT JOIN {field_data_field_organization} AS fdfo ON n.nid=fdfo.entity_id WHERE n.status=:status AND n.type=:resource AND (fdfty.field_type_value=:articlereport OR fdfty.field_type_value=:blog OR fdfty.field_type_value=:recweborpodcast OR fdfty.field_type_value=:newslettermagazine) AND el.field_experience_level_value=:level AND fdfr.field_role_value=:role AND fdft.field_topics_value=:topic GROUP BY n.nid, n.title ORDER BY CASE WHEN fdfty.field_type_value='4' THEN 0 WHEN fdfty.field_type_value = '5' THEN 1 WHEN fdfty.field_type_value = '6' THEN 2 WHEN fdfty.field_type_value = '7' THEN 3 END, n.title", array(':status' => '1', ':resource' => 'resource', ':articlereport' => '4', ':blog' => '5', ':recweborpodcast'=> '6', ':newslettermagazine'=>'7', ':level' => $level, ':role' => $role, ':topic' => $topic));
	
		// count how many results returned
		$num_rows = $result->rowCount();
		$counter .= $num_rows;
	
		if ($num_rows > 0) {
			$output .= '<h2 class="recheader" id="quickstudy">Quick Study</h2>';
	
			//return results to page
			while ($row = $result->fetchAssoc()) {
		
				// set variables	
				$title = $row['title'];
				$nid = $row['nid'];
				$desc = $row['body_value'];
				$alias = $row['alias'];
				// set image to display correctly
				$icon = image_style_url('thumbnail', $row['uri']);
				$result2 = db_query("SELECT n.title, n.nid, fdfp.entity_id, fdfp.field_price_tid, fdfcm.entity_id, fdfcm.field_costs_money_fid, fm2.fid, fm2.uri FROM {node} AS n LEFT JOIN {field_data_field_price} AS fdfp ON n.nid=fdfp.entity_id LEFT JOIN {field_data_field_costs_money} AS fdfcm ON fdfp.field_price_tid=fdfcm.entity_id LEFT JOIN {file_managed} AS fm2 ON fdfcm.field_costs_money_fid=fm2.fid WHERE n.nid = :nid", array(':nid' => $nid));
				while ($row2 = $result2->fetchAssoc()) {
					if ($row2['uri'] == 'public://dollar-sign.png') {
						$dollar = image_style_url('thumbnail', $row2['uri']);
					} else {
						$dollar = "";
					}
				}
					$ftv = $row['field_type_value'];
					$output .= '<div class="recentry"><img src="' .$icon. '" id="icon" alt="Quick Study" title="Quick Study" />';
					if ($dollar != "") { 
						$output .= '<img src="' .$dollar. '" id="price" alt="Costs Money" title="Costs Money" />'; 
					} else { 
						$output .= ""; 
					} 
					$organization = $row['field_organization_value'];
					$output .= '<h2 class="rectitle"><a href="../' . $alias . '">' . $title . '</a></h2><div class="recdesc">';
					if (($organization != NULL) && ($ftv == '6')) {
						$output .= '<div class="org">' . $organization . '</div>';
					}
					$output .= '<div class="description">' . $desc  . '</div></div></div>';
				}
			}

		// sql for Deep Dive items
		$result = db_query("SELECT n.title, n.nid, n.status, n.type, fdfty.field_type_value, fdfi.entity_id, fdfi.field_icon_tid, fdfti.entity_id, fdfti.field_taxonomy_icon_fid, fm.uri, fm.fid, ua.source, ua.alias, el.field_experience_level_value, fdb.body_value, fdfr.field_role_value, fdft.field_topics_value, fdfl.field_location_value, fdfl.entity_id, fdfo.field_organization_value, fdfo.entity_id FROM {node} AS n LEFT JOIN {field_data_field_type} AS fdfty ON n.nid=fdfty.entity_id LEFT JOIN {field_data_field_icon} AS fdfi ON n.nid=fdfi.entity_id LEFT JOIN {field_data_field_taxonomy_icon} AS fdfti ON fdfi.field_icon_tid=fdfti.entity_id LEFT JOIN {file_managed} AS fm ON fdfti.field_taxonomy_icon_fid=fm.fid LEFT JOIN {url_alias} AS ua ON CONCAT('node/', nid)=source LEFT JOIN {field_data_field_experience_level} AS el ON n.nid=el.entity_id LEFT JOIN {field_data_body} AS fdb ON n.nid=fdb.entity_id LEFT JOIN {field_data_field_role} AS fdfr ON n.nid=fdfr.entity_id LEFT JOIN {field_data_field_topics} AS fdft ON n.nid=fdft.entity_id LEFT JOIN {field_data_field_location} AS fdfl ON n.nid=fdfl.entity_id LEFT JOIN {field_data_field_organization} AS fdfo ON n.nid=fdfo.entity_id WHERE n.status=:status AND n.type=:resource AND (fdfty.field_type_value=:tool OR fdfty.field_type_value=:bookorworkbook OR fdfty.field_type_value=:certificateordegreeprogram) AND el.field_experience_level_value=:level AND fdfr.field_role_value=:role AND fdft.field_topics_value=:topic GROUP BY n.nid, n.title ORDER BY CASE WHEN fdfty.field_type_value='8' THEN 0 WHEN fdfty.field_type_value = '9' THEN 1 WHEN fdfty.field_type_value = '10' THEN 2 END, n.title", array(':status' => '1', ':resource' => 'resource', ':tool' => '8', ':bookorworkbook' => '9', ':certificateordegreeprogram'=>'10', ':level' => $level, ':role' => $role, ':topic' => $topic));
	
		// count how many results returned
		$num_rows = $result->rowCount();
		$counter .= $num_rows;
	
		if ($num_rows > 0) {

			$output .= '<h2 class="recheader" id="deepdive">Deep Dive</h2>';
	
			//return results to page
			while ($row = $result->fetchAssoc()) {
		
				// set variables
				$title = $row['title'];
				$nid = $row['nid'];
				$desc = $row['body_value'];
				$alias = $row['alias'];
				// set image to display correctly
				$icon = image_style_url('thumbnail', $row['uri']);
				$result2 = db_query("SELECT n.title, n.nid, fdfp.entity_id, fdfp.field_price_tid, fdfcm.entity_id, fdfcm.field_costs_money_fid, fm2.fid, fm2.uri FROM {node} AS n LEFT JOIN {field_data_field_price} AS fdfp ON n.nid=fdfp.entity_id LEFT JOIN {field_data_field_costs_money} AS fdfcm ON fdfp.field_price_tid=fdfcm.entity_id LEFT JOIN {file_managed} AS fm2 ON fdfcm.field_costs_money_fid=fm2.fid WHERE n.nid = :nid", array(':nid' => $nid));
				while ($row2 = $result2->fetchAssoc()) {
					if ($row2['uri'] == 'public://dollar-sign.png') {
						$dollar = image_style_url('thumbnail', $row2['uri']);
					} else {
						$dollar = "";
					}
				}
					$ftv = $row['field_type_value'];
					$output .= '<div class="recentry"><img src="' .$icon. '" id="icon" alt="Deep Dive" title="Deep Dive" />';
					if ($dollar != "") { 
						$output .= '<img src="' .$dollar. '" id="price" alt="Costs Money" title="Costs Money" />'; 
					} else { 
						$output .= ""; 
					} 
					$location = $row['field_location_value'];
					$organization = $row['field_organization_value'];
					$output .= '<h2 class="rectitle"><a href="../' . $alias . '">' . $title . '</a></h2><div class="recdesc">';
					if (($location != NULL) && ($ftv == '98')) {
						$output .= '<div class="location">' . $location . '</div>';
					}
					if (($organization != NULL) && ($ftv == '98')) {
						$output .= '<div class="org">' . $organization . '</div>';
					}
					$output .= '<div class="description">' . $desc  . '</div></div></div>';
			}
		}
	
			if ($counter == "0") {
				// if no entries matched what user entered give them alternative text.
				$output .= '<div id="no-recs-returned"><p class="recdesc">' . "Sorry, your selections did not result in any recommendations at this time. " . '<a href=' . "/pathfinder" . '>' . "Go here to try again" . '</a></p></div>';
			}
	
		// return variable to display results
		return $output;
	
		// if user went directly to rec page without taking questionnaire
	} else {
	
		$output = "";
		
		// progress bar html/css styling
		$output .= '<div class="progress-bar" id="progress-bar"><span id="step-one">Primary Role</span><span id="arrow1"> > </span><span id="step-two">Experience Level</span><span id="arrow2"> > </span><span id="step-three">Topics of Interest</span><span id="arrow3"> > </span> <span id="recommendations" style="background-color: #003863;">Recommendations</span></div>';
		
		$output .= '<p class="recdesc">' . "You haven't yet taken the questionnaire; please " . '<a href="/pathfinder">' . "go here to get started" . '</a>.</p>';
		
		return $output;
	}
	
}

Comments

Jaypan’s picture

You'll need to create the file on your server. You can do this with file_put_contents(), then transfer that file using file_transfer().

junestag’s picture

@Jaypan thank you for your response. I tried playing around with your suggestions, but I'm finding it difficult to implement.

A question:

1) Will using the file_transfer function in my custom module work with my 'return' statement at the end? (ie. can I add the results of file_transfer to the end of my return statement so that when it returns the $output to the page it displays the created file_transfer hyperlink, say, at the bottom of the page as well as the rest of the $output? saying something like 'download your results now') ... my understanding from reading the documentation is that file_transfer stops all other code abruptly...so I can either have my page $output OR my file transfer but not both.

For example (which doesn't work):

$ft = file_transfer($path, array('Content-Type' => 'application/octet-stream',
      'Content-Disposition' => 'attachment; filename="' . 'users.csv' . '"',
      'Content-Length' => filesize($path)));

$output .= $ft;
return $output;
Jaypan’s picture

Yes, you can only have one or the other. Have you ever been to a page that says 'your download will start soon, if it does not, click this link'? The page is the output. The link is the link to instigate the download. They are instigating that download automatically with javascript, but if a user does not have JS enabled, it won't work, which is why they also include the link.

junestag’s picture

@Jaypan ok, i see. thank you for the follow up. i don't think that solution will work for me then. have you ever heard of forena reports module?

I'm going to try and implement that.

Jaypan’s picture

But at the start you said this:

All that works fine. However, now we'd like to add some way to allow the user to EXPORT the recommendations they see on the screen to a CSV file (or some other type) with an 'export' link.

You would like to the script that forces the download, using the 'export' link. Why do you also need to show content at that point? Isn't the user already looking at the page?

junestag’s picture

@Jaypan yes, that's true. Maybe I'm confused. My point was that the page in question was created in a custom module. The end of the module is the output of the content. So I'm not sure where and how I would add the link in the way you are suggesting.

The module is a multi-page form. On the third 'page' of the form the user clicks submit and that calls the recommendations page to be loaded (a bunch of sql is run, recommendations are added to the $output var) and finally the $output var is output to the screen.

I'm still fairly new to Drupal. This is my first big module. Is there a way to then add the code you suggested after I return the $output var at the end of the module?

Jaypan’s picture

To answer your question, yes and no.

The way everyone does it goes like this:
1) When the user accesses the page, your content is printed to the page.
2) In that content, you add a link, that when clicked, will download the file
3) Add a disclaimer that if the link does not start automatically to click the link
4) On that same page, you add some JS that forces the download automatically

So you'll need to register two page paths:
1) The path to access the page
2) The path to download the content

Here is an example:

function MYMODULE_menu()
{
  $menu['path/to/page/%'] = array
  (
    'title' => 'My Page',
    'page callback' => 'MYMODULE_page_callback',
    'page arguments' => array(3),
    'access arguments' => array('access content'),
  );
  $menu['path/to/download/%'] = array
  (
    'title' => 'My Page',
    'page callback' => 'MYMODULE_download_callback',
    'page arguments' => array(3),
    'access arguments' => array('access content'),
  );

  return $menu;
}

function MYMODULE_page_callback($uid)
{
  $account = user_load($uid);
  $page = array
  (
    'message' => array
    (
      '#prefix' => '<p>',
      '#suffix' => '</p>',
      '#markup' => t('Hi @user, your download will begin shortly', array('@user' => $account->name)),
    ),
    'message_link' => array
    (
      '#prefix' => '<p>',
      '#suffix' => '</p>',
      '#markup' => t("If it doesn't, please click !this_link", array('!this_link' => l(t('this link'), 'path/to/download/' . $account->uid))),
    ),
    '#attached' => array
    (
      'js' => array
      (
        'type' => 'file',
        'file' => drupal_get_path('module', 'MYMODULE') . '/js/script-name.js',
      ),
    ),
  );

  return $page;
}

function MYMODULE_download_callback($uid)
{
  $account = user_load($uid);
  // Force a download of some file using
  // file_put_contents() and file_transfer()
}

And then in script-name.js you can do this:

(function($, Drupal)
{
  function clickDownloadLink()
  {
    // Find any links that link to the download page, and
    // click the link (with JS)
    // Note that use of the .once() function ensures that this only happens
    // once.
    $("a[href*='path/to/download/']").once("click-download-link", function()
    {
      $(this).click();
    });
  }

  // Drupal.behaviors will run on page load
  Drupal.behaviors.myModule = {
    attach:function()
    {
      // Trigger the function that will
      // force the download
      clickDownloadLink();
    }
  };
}(jQuery, Drupal));

This will click your link on page load, triggering the download of the file.