0

I want to secure the display of my PDF : firstly hide the PDF's source link for the user, and secondly make PDF download impossible.
I was on the first point, I used :

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

That work very well on a page without any HTML, but if I only add :

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>TEST</title>
</head>
<body>
  <div>
    <!-- Some elements -->
  </div>
  <div>
    <?php 
    $file = "./src/file.pdf";
    header("Content-type: application/pdf"); 
    header("Content-Length: " . filesize($file)); 
    readfile($file);
    ?>
  </div>
</body>
</html>

That dosn't work at all
So how can I do ? Because I want the PDF at this place, surrounded by HTML.

If someone have the solution, or more than that, the solution for hidding PDF's source link and secondly make PDF download impossible.

I thank you very much for your time and your patience !

  • 1
    You need to avoid doing any HTML output if you want to output headers . See this https://stackoverflow.com/questions/423860/php-header-redirect-not-working – Ken Lee Dec 12 '20 at 00:50

1 Answers1

0

As Ken Lee pointed out you can't have any output before calling header.

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

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>TEST</title>
</head>
<body>
  <div>
    <!-- Some elements -->
  </div>
  <div>
    <?php readfile($file); ?>
  </div>
</body>
</html>
waterloomatt
  • 3,662
  • 1
  • 19
  • 25