We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22448
    • 241 Posts
    This is one of those issues that caught me by surprise as i thought it would be easy to do.
    There are several threads on the subject but no working solution.

    The goal:
    Create a pdf resource that will automatically trigger a pdf download when navigated to.


    The problem:
    PDF download is 0KB in size and cannot be opened.

    The attempts:

    Attempt 1 - Failed
    Setup a resource with Resource Type: Web page, Media type: application/pdf, Content disposition: attachment, and blank template. In the content i had a call to a snippet like so:
    [!LoadFile?&file=`assets/files/file.pdf`!]


    to this little snippet:

    <?php
    # Snippet to include files from filesystem
    # [[LoadFile?&file=`assets/folder/file.tpl`]]
    
    $path=$modx->config['base_path'];
    if ( !isset($file) || $file== "" ) return "No file specified."; //check if there's a file given.
    
    
            if(is_file($file))  
            {  
                // required for IE  
                if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); }
                readfile($file);
                return;
            }
    ?>
    


    Attempt 2 - Failed
    Installed FileDownloadPE snippet from http://modx.com/extras/package/filedownloadpe
    Followed the snippet instructions to setup downloads template, tvs, and resources.


    I understand that Revo has static resources just for this. Unfortunately the client is reluctant to migrate so we have to deal with Evo for now.

    It seems that this is a very common use case. Does anybody have pdf resources working?

    Thanks in advance.



    [ed. note: outre99 last edited this post 14 years, 10 months ago.]
      • 33968
      • 863 Posts
      Are you able to see the headers that are being sent with the corrupted pdf?

      You're right, it's straightforward to setup in Revo...
        • 22448
        • 241 Posts
        Hey Lucas,
        thanks for taking the time. Below are the headers for the download. What's interesting is that filename is set to the resource alias and not to the actual name of the pdf file. But i'm not sure if that's the problem.
        http://temporary-warehouse.co.uk/downloads/planning-guide.pdf
        
        GET /downloads/planning-guide.pdf HTTP/1.1
        Host: temporary-warehouse.co.uk
        User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0
        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
        Accept-Language: en-us,en;q=0.7,zh-cn;q=0.3
        Accept-Encoding: gzip, deflate
        Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
        Connection: keep-alive
        Cookie: __utma=210324496.527957361.1320855097.1321100430.1321111163.8; __utmz=210324496.1320855097.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); yams_lang=en; __utmc=210324496; webfxtab_settingsPane=1; webfxtab_templatesPane=0; webfxtab_resourcesPane=3; webfxtab_documentPane=1; SN4b8b6dd563e3b=58399259a750dc8444e65179eb8f16b4; __utmb=210324496.1.10.1321111163
        
        HTTP/1.1 200 OK
        Date: Sat, 12 Nov 2011 15:20:12 GMT
        Server: Apache
        X-Powered-By: PHP/5.2.17
        P3P: CP="NOI NID ADMa OUR IND UNI COM NAV"
        Pragma: public
        Expires: 0
        Cache-Control: public
        Content-Description: File Transfer
        Content-Transfer-Encoding: binary
        Content-Disposition: attachment; filename=planning-guide.pdf
        Set-Cookie: SN4b8b6dd563e3b=58399259a750dc8444e65179eb8f16b4; path=/
        Content-Length: 0
        Connection: close
        Content-Type: application/pdf; charset=UTF-8
        
        


        I found this article http://perishablepress.com/press/2010/11/17/http-headers-file-downloads/ and updated my LoadFile snippet to this:

        <?php
        # Snippet to include files from filesystem
        # [[LoadFile?&file=`assets/folder/file.tpl`]]
        $path=$modx->config['base_path'];
        if ( !isset($file) || $file== "" ) return "No file specified."; //check if there's a file given.
        // http headers for zip downloads
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"".$file."\"");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".filesize($file));
        ob_end_flush();
        @readfile($file);
        ?>
        


        For a test i tried to use a text file and it worked properly by downloading and displaying the text file in notepad. Saving the text file to the local folder created a file with proper size. But doing the same thing with a pdf file results in the "Corrupt file" error. Trying to save the pdf file results in a file of size 0. So it appears that the problem is pdf specific.

        Is there anything that stands out for you in the headers? [ed. note: outre99 last edited this post 14 years, 10 months ago.]
          • 22448
          • 241 Posts
          PDF download must be a very common use case. If you have a working solution please share.
            • 32316
            • 387 Posts
            If you are trying to get a pdf to download rather than display I think the issue is that this is difficult to do - little to do with MODX

            The only reliable solutions I have seen are:
            a/ compress the pdf as a zip file and serve it up as file.zip - the user must of course unzip it
            b/ use a ftp link - assumes that you have anonymous ftp set up or use a link that includes the user and password - you have to set this up which may be problematic, I have NOT tried this.
            c/ just tell the user to right click or option click or ...
              • 22448
              • 241 Posts
              Thanks whistlemaker.
              I did not have a preference between download pdf or open it.
              Using 2 approaches i described in the original post it was not working either way.
              Those were the preferred methods as i wanted to manage pdf files through the corresponding resources, but alas, it was not to be.

              So i ended up using the invisible iframe method to force the file download, which worked most of the time, but not always. So the final solution was to add a link to the pdf file as the backup in case the iframe triggered download didn't work on a particular user's configuration.