So I need a little help with a php script I am writing that loads a PDF in a new tab through a PHP script. I am doing it this way to make sure that the PDF can only be accessed if the user is logged in and has a link to it, otherwise the .htaccess in the parent directory to the PDF blocks access.
I am able to read from the PDF using the code below:
$file = '../path-to-file.pdf';
$filename = 'filename.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
@readfile($file);
The result of this is then passed into an AJAX function shown below which generates a new tab and throws the response onto it:
$.ajax({
url: '../includes/function.projects.php?view-media=' + item,
success: function(data) {
$(window.open().document.body).html(data);
}
})
This all works smoothly, it's only when the new tab opens, because I am writing the response straight to the documents HTML, I am getting a screen full of text like this:
%PDF-1.5 %���� 2 0 obj << /Linearized 1 /L 17007 /H [ 687 126 ] /O 6 /E 16732 /N 1 /T 16731 >>
So my question is quite simply this; Has anyone tried managed to do this before? If so, are there any libraries I should be using/headers I'm missing etc
I have already seen this question here but I cannot get the answers people are listing to work so I am out of ideas.
Any help would be greatly appreciated, Thanks in advance!