Carlos
Carlos

Reputation: 352

how can I display a pdf retrieved using file_get_contents in php?

I want to display, embedded in the HTML, a pdf file which is in a directory up to the document root.

Document root points to /var/www/web1/web and the pdf file is in /var//www/web1/docs/pdf, so I can't point it from the html.

I'm using PHP and I tried some suggestions like get_file_contents, base64_encode, .... but didn't worked.

All examples I find are to make download the pdf or to convert it into a jpg, but this is not what I need.

Any suggestions?

Upvotes: 3

Views: 22959

Answers (4)

GrowingBrick
GrowingBrick

Reputation: 751

Simply load the pdf content by a php page, we call it viewer.php, then embed the PHP page as it would be the document itself: <embed src="viewer.php" width="80%" height="900px" />

In the viewer.php file:

<?php
//Load file content
$pdf_content = file_get_contents('../unreachable_file_outside_webserver.pdf');
//Specify that the content has PDF Mime Type
header("Content-Type: application/pdf");
//Display it
echo $pdf_content;

Otherwise another solution for bigger files (easier on RAM due to bufferized read/output without storing content in RAM) use readfile

<?php
header("Content-Type: application/pdf");
readfile("../unreachable_file_outside_webserver.pdf");

Upvotes: 9

Dipanshu Mahla
Dipanshu Mahla

Reputation: 152

You first need to go to root dir using "../"

<embed src="../docs/pdf/fileName.pdf" type="application/pdf" width="100%" height="500px" />

Upvotes: 0

nandal
nandal

Reputation: 2634

Use the following html tag:-

<embed src="path_to_pdf_file.pdf" width="800px" height="2100px" />

Upvotes: 0

matiit
matiit

Reputation: 8017

You can embed PDF on your website using a library, for example there's a great one called PDF.js

PDF is a binary format so you cannot exactly "display it".

Upvotes: 0

Related Questions