Jump to content

Force pdf download


Recommended Posts


I have file field on a page wich contain pdf files.

As i understand in almost all modern browsers pdf files will be opened in it, but i need to force files download. 

I can use "download" attribute, but it does not work in IE.

Is there some build-in methods for that in PW.


Link to comment
Share on other sites

Hi, Horst and thank you for answer.
For now i implement solution based on url segments. 
if($input->urlSegment1 == 'download') {
    if($input->urlSegment2) {
        $names = array();
        $urls = array();
        foreach ($page->diagnostic_referral_repeater as $referral) {
            foreach ($referral->diagnostic_referral_files as $file) {
                array_push($names, $file->name);
                array_push($urls, $file->filename);
        $key = array_search($input->urlSegment2, $names);
        if($key !== false) {
        } else {
            throw new Wire404Exception();
} else if($input->urlSegment1) {
  // unknown URL segment, send a 404
  throw new Wire404Exception();
                            <?php foreach ($page->diagnostic_referral_repeater as $referral) { ?>
                                <div class="diagnostic-referral">
                                    <div class="diagnostic-referral__box">
                                        <div class="diagnostic-referral__title">
                                            <?php echo $referral->diagnostic_referral_category->title; ?>
                                        <?php echo $referral->render('diagnostic_referral_files', 'diagnostic-referral'); ?>
                                        <div class="diagnostic-referral__files">
                                            <?php foreach ($referral->diagnostic_referral_files as $file) { ?>
                                                <div class="diagnostic-referral-file">
                                                    <a href="<?php echo $page->path.'download/'.$file->name; ?>" class="diagnostic-referral-file__link">
                                                        <div class="diagnostic-referral-file__icon">
                                                            <span class="icon-download"></span>
                                                        <div class="diagnostic-referral-file__title">
                                                            <?php echo $file->description; ?>
                                            <?php } ?>
                            <?php } ?>

Do you have any recommendation to improve it? 


  • Like 1
Link to comment
Share on other sites

  • 9 months later...

I used a much simpler and faster method utilizing the pagefile class.

My link looks like this:

// Sorry for smarty language. but thats what I used
{if $page->files|@count > 0}
            {foreach $page->files as $file}
                    {if $file->description}

                    <a href="download/{$file->name}" class="link-icon">Download</a>
                    | {$file->filesizeStr}

and then in my _init.php I used the following code

if ($input->urlSegment1 == 'download') {
        $download_options = array(
            // boolean: halt program execution after file send
            'exit' => true,
            // boolean|null: whether file should force download (null=let content-type header decide)
            'forceDownload' => true,
            // string: filename you want the download to show on the user's computer, or blank to use existing.
            'downloadFilename' => '',
        if ($input->urlSegment2) {
            $pagefile = $page->files[$input->urlSegment2];
                wireSendFile($pagefile->filename, $download_options);

EDIT: The downside when implementing this in the _init.php is, that you can´t send a Wire404Exception, because this file gets included (prepended) and PHP doesn´t like exeptions in included files. If you paste this code into one of your templates you could add trowing a Wire404Exception.

Edited by jmartsch
  • Like 6
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...