Jump to content

How can I display a PDF on a page if the PDF folder is protected?


Recommended Posts

Hi,

 

I've been going around in circles and have tried a few things to display a PDF in an iFrame even though the folder is password-protected. I couldn't find a way to enter the password before displaying the PDF.

 

I'd like for the folder that contains the PDFs is password protected and would like to not allow someone get access to the entire folder once they see the path in the source of the page.

 

Is there a way to accomplish this using PHP?

 

Thanks in advance!

why not just include the pdf from above the webroot?  no password needed

say your site is in /htdocs/site

and the pdf is in /htdocs/pdf

 

 

include("../pdf/file.pdf");

 

That way there never can be a direct link to the pdf, your script has to include it.

I just tried what you suggested and it's not working for me. It says that the file can't be found.

 

I have this in my HTML page too and am not using an include, but either way it's not working:

 

<iframe src= "../../../private/pdf/pdf1.pdf#toolbar=0" width="800" height="600"></iframe>

 

Any other ideas?

<?php
$p = PDF_new();

/*  open new PDF file; insert a file name to create the PDF on disk */
if (PDF_begin_document($p, "", "") == 0) {
    die("Error: " . PDF_get_errmsg($p));
}

PDF_set_info($p, "Creator", "hello.php");
PDF_set_info($p, "Author", "Rainer Schaaf");
PDF_set_info($p, "Title", "Hello world (PHP)!");

PDF_begin_page_ext($p, 595, 842, "");

$font = PDF_load_font($p, "Helvetica-Bold", "winansi", "");

PDF_setfont($p, $font, 24.0);
PDF_set_text_pos($p, 50, 700);
PDF_show($p, "Hello world!");
PDF_continue_text($p, "(says PHP)");
PDF_end_page_ext($p, "");

PDF_end_document($p, "");

$buf = PDF_get_buffer($p);
$len = strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $buf;

PDF_delete($p);
?> 

load it into a string

send a pdf out header

print string

 

$buf=file_get_contents('folder/yourpdf');
$len = strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $buf;

load it into a string

send a pdf out header

print string

 

$buf=file_get_contents('folder/yourpdf');
$len = strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $buf;

 

Thank you very, very much! It works perfectly!!!

The solution works great except with some browsers, the PDF is downloaded to disk? Is there any way to override that? I looked at some other websites, like my bank, and they can't seem to display the PDF in the window with some browsers. So maybe that's just the way it is. :-(

 

If anyone can confirm this for me, I'd appreciate it.

 

Thanks!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.