Jump to content

PDF out of web root and encoding error view


brosjr

Recommended Posts

Hi all,

 

On my project some PDF documents and images are storage outside the web public folder, so I just can't reach then just with a simple link and I need to display then on a page.

 

$fileDir = '../data/contas';

 

For images I use the code below (this solution I found on web):

 

			
$fileDir = '../data/contas/';

function data_uri($conta, $mime) 
{
$contents = file_get_contents($conta);
$base64 = base64_encode($contents);
return "data:$mime;base64,$base64";
)

<img src="<?php echo data_uri($conta, 'image/png');?>" width="720">

 

When I see the HTML code I got <img src="data:image/png;base64,/9j/4AAQSkZJRgABA...(huge amount of string)...FXYq/wD/2Q=="  width="720"> and the images are displayed correctly.

 

The problem occurs on PDF files, I use the same function, as follow below and the PDF document is not displaying, there is no error message just a gray background where the document were to be shown.

 

<EMBED SRC='<?php  echo data_uri($conta, 'application/pdf');?>#view=FitH,top&toolbar=1&navpanes=0&scrollbar=1&zoom=scale' WIDTH="720" HEIGHT="900" ALIGN="TOP"></EMBED>

 

When I look at the HTML code it shows: <EMBED SRC='data:application/pdf;base64,JVBERi0xLjIK...(huge amount of string)...YK#view=FitH,top&toolbar=1&navpanes=0&scrollbar=1&zoom=scale' WIDTH="720" HEIGHT="900" ALIGN="TOP"></EMBED>

 

Any ideia how can I show the PDF files?

 

Or there is another way to access the files outside web public folder on server to display then?

 

Thankx

Danilo Jr.

Link to comment
Share on other sites

I've never really tried a data uri in an embed, it could be that the browser does not support it there (or maybe the plugin that handles the embed).

 

A fairly typical method of access files outside the web root is to just create a proxy script.  Eg:

 

example.com/serveFile.php?file=blah

<?php

$file = $_GET['file'];
//Make sure you sanitize $file to ensure it points to a valid file

//do any other checks such as ensure the user is logged in and has permission if necessary

//if all checks are pass then
header('Content-type: application/pdf'); //or vary based on file type which you'll have to get somehow
header('Content-length: '.filesize($file));
header('Content-disposition: attachment; filename="'.basename($file).'"'); 
readfile($file);
?>

 

Link to comment
Share on other sites

Thank you kicken but that worked just for downloading the pdf as file it didn't help to opening the document on the page.

 

But I thied the below object tag and it worked, not as I expected once I can't handle with it's zoom options, maybe because of encoded strings, but it's fine.

 

<object data='<?php echo data_uri($conta, 'application/pdf');?>'  type='application/pdf'  width='720'  height='900'> </object>

 

Thankx

Danilo Jr.

Link to comment
Share on other sites

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.