无法使用js或php生成要下载的文件

I know there are a lot of tutorials, but for some reason I am not managing to focus the search to my specific issue.

I want to have a link to download <a href="#" download="cal.ics">click</a>

For some reason it fills the file with all the echo's from my page instead of a string that i can prepare.

What am I missing?

Edit:

I want the file to contain the return of this function:

function createICS(){
header("Content-Type: text/Calendar; charset=utf-8'");
header("Content-Disposition: inline; filename=calendar.ics");
$ical = "BEGIN:VCALENDAR
    VERSION:2.0
    PRODID:-//Geektike/eventsboard//IL
    BEGIN:VEVENT
    UID:123456789
    SUMMARY:asd
    DTSTART:20140520T210000Z
    DTEND:20140520T220000Z
    LAST-MODIFIED:20111115T103014Z
    LOCATION:here
    DESCRIPTION:cool
    END:VEVENT
    END:VCALENDAR";

return $ical;
exit;
}

This: <a href="#" download="cal.ics">click</a> will need to be this: <a href="#" download="cal.php">click</a>.

Then create the cal.php file and have this code inside it:

<?php
  print createICS();
?>

(That's as long as the cal.php file has access to the createICS() function, otherwise put that function in to the cal.php file as well).

You must use output buffering to omit the data you don't want in the file. See http://ch1.php.net/manual/en/function.ob-start.php