0

Background

I am working on setting up an intranet webpage where the user can select an item from a list, and it will display information about that item and a PDF as a block element.

The PDFs will need to be updated and replaced by specific people, so they are stored on a separate file server on the internal network.

HTML Solution Attempt

I've tried using

<embed src="file:///myFileServer/PDFs/filename.pdf" width="1000" height="600" type="application/pdf">

but I get local resource errors:

  • Firefox: "Security Error: Content at myWebServer/file_read.php may not load or link to file:///myFileServer/PDFs/filename.pdf."
  • Edge/Chrome: "Not allowed to load local resource: file:///myFileServer/PDFs/filename.pdf"

I understand the error is because the PDF path is loaded by the browser, not the web server. This likely would be a problem even if it was allowed, because not all of the users would have access to the file server. Naturally, my next thought was to try using PHP to display the element so that the browser never needs to see a file path and the web server loads the PDF.

PHP Solution Attempt

I then tried using

<?php
    $filename = "file:///myFileServer/PDFs/filename.pdf";
        
    header("Content-type: application/pdf");
    header("Content-Length: " . filesize($filename));
        
    readfile($filename);
?> 

This gave me the following warning, which had to do with the PHP that is used to load the page's navigation bar.
"Cannot modify header information - headers already sent by (output started at C:\Apache24\htdocs\nav.php:66) in C:\Apache24\htdocs\file_read.php on line 81"

Removing <?php include 'nav.php' ?> in the file_read.php caused the PHP code mentioned above to work, but not as I wanted. It basically loads a full-page PDF view as if you selected a PDF to open it in the browser, instead of displaying it as a page element.

PHP PDF to Base64 Solution

Since the above did not work, I kept digging for a solution. It's a little hacky, but I did find one solution that seems to works.

<?php
    $fileLocation = "//myFileServer/PDFs/filename.pdf";
    $pdf = chunk_split(base64_encode(file_get_contents($fileLocation))); //Convert to Base64
    echo '<embed  src="data:application/pdf;base64,' . $pdf . '" style="height:500px;width:80%;" title="test"></embed>';
?>

While this does work and the code is rather clean, it's a bit slow and I feel like there has to be a more efficient way to do this.

My Question

Is there a better way to deal with the limitations of loading a local resource from another internal server than converting the PDF to Base64?

VeryColdAir
  • 107
  • 2
  • 8
  • You could go with the first PHP attempt but load it into an iframe within the main page – ADyson Sep 14 '21 at 15:29
  • [Cannot modify header information - headers already sent](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) See this for a fix for that attempt – RiggsFolly Sep 14 '21 at 15:46
  • Your Attempt 1, woudl require an Apache Alias to be setup in your Virtual Host definition for this site – RiggsFolly Sep 14 '21 at 15:53

0 Answers0