Forum

Thread tagged as: Members

Secure Downloads (Members) to allow for PDF Preview

Right now I've moved a lot of PDFs into the secure bucket outside of the webroot. I'm trying to preview some of these files onto the server as a little region of the webpage. This following code worked fine when the files were inside the webroot, but now is not working properly.

<iframe src="download.php?file=/test.pdf" type="application/pdf" width="800" height="600">

Is there a way to modify the download.php so that it can also potentially preview files outside the webroot?

Rose Sankar

Rose Sankar 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, most likely. What is in download.php?

It is currently following the template from the documentations.

<?php 
    include('perch/runtime.php'); 
    $bucket_name = 'secure';
    $url_param   = 'file';
    $allow_download = false;
    if (perch_member_logged_in()) {
        $allow_download = true;
    }
    if ($allow_download) {
        perch_members_secure_download(perch_get($url_param), $bucket_name);
    }
    exit;
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are the files in the bucket called secure and is that bucket defined in your bucket list?

Yes, the files are in the secure bucket. The secure bucket works perfectly fine. I'm able to put files into it through the Perch UI and it works fine on the actual webpage. The issue comes when I try to embed a pdf from the secure bucket. It no longer embeds the pdf and just starts the download.

the bucket config file --

<?php
return array(
    'secure' => array(
        'web_path'  => '/download.php?file=',
        'file_path' => '/var/www/secure',
    ),
);
?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

What do you mean by embed in this context?

Previously we had a PDF preview box on a section of the webpage (w. width 800, height 600). It worked when the files were in the perch/resources folder but is no longer working now that we are grabbing the file through the download.php page.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, I understand - you were using the browser's capability to display a PDF instead of force-downloading the file. Got it.

Instead of this:

perch_members_secure_download(perch_get($url_param), $bucket_name);

do this:

perch_members_secure_download(perch_get($url_param), $bucket_name, false);

That code causes the PDF to be previewed in another window.

I'm trying to make it so the PDF will be previewed on a webpage along with other content. It's similar to how the html <object> tag works. It was working when I still had the PDF files on the resource bucket within the webroot but now lost its functionality when it needs to be referenced through the download.php.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What HTML would you output for a static PDF file?

This is essentially how the HTML should look like after perch provided the file path:

<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <head>
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <title>Test Page</title>
    </head>
</head>
<body>
<a style="text-decoration: none;" href="https://portal.teamcomcast.com/irj/portal" target="_blank">
    <button type="button" class="btn btn-primary">
        <center>
                Download Button
            <center>
    </button>
</a>
<center> <iframe src="download.php?file=/test.pdf" type="application/pdf" width="800" height="600"> </center>
</body>
</html>

There will be a button on the top of the page and then a PDF preview of the file directly below it.

The site is currently referencing the preview files without the download.php (via perch/resources) so that it will work. I would like to secure those PDF files with the members app and retain the ability to preview those files.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that should work. If not, do you have an example we can see?