Embed PDF file in the html from /var/www/pdf/ directory - php

I want to embed pdf file in my website from /var/www/pdf/ directory. I have hosted my webpage at /var/www/html/portal/index.php. I tried following ways
1) using embed
2) using iframe
<iframe src="../../pdf/ebook.pdf" width="900" height="750" frameborder="0" allowfullscreen></iframe>
3) using object
<object data="../../pdf/ebook.pdf#scrollbar=0&toolbar=0&navpanes=0" width="900" height="750" type="application/pdf">
<p>PDF cannot be displayed</p>
</object>
None of them working, but if I place the pdf file /var/www/html/pdf/ folder it is working fine.
For my requirement, I cannot keep the file in the web directory because public can access the PDF file directly using URL without login in to my web portal. And Also google also can index my PDF file from public search
Please help me to embed pdf
Thanks in advance

You can do it by .htaccess
First : prevent direct access to open pdf file
Refer this questions and answers
Second: prevent Search engine indexing
Refere this
Hope it will work. I try to give this information in comment but it is not displaying properly there.

I found solution using php script
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=filename.pdf");
#readfile('../../pdf/ebook.pdf');

Related

How to embed pdf file in html?

I want to embed pdf files in my html ( either using the embed tag or the iframe tag ).
I've tried the following two. All work well on firefox but not on chrome, chromium and android browsers.
<embed type="application/pdf" src="myfile.pdf"></embed>
<iframe class="embed-responsive-item" src="myfile.pdf" allowfullscreen></iframe>
I've also tried reading the pdf file with php and then using an iframe to display it like this
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$_GET['pdf']);
#readfile($CONFIG['PDF']."/".$_GET['pdf']);
Again, this doesn't work on chrome / chromium and mobile browsers. It only works on desktop firefox.
I'm not sure if this is important information but I've also Set the X-Frame-Options "ALLOW-FROM mydomain.com"
Here you will find an example of what the issue is.
Is there any other way that I can embed a pdf file on a web page?
The Chrome browser on Android no longer supports PDF embeds. You can get this by using the Google Drive PDF viewer:
<embed src="https://drive.google.com/viewerng/
viewer?embedded=true&url=http://example.com/the.pdf" width="500" height="375">
You can also use Google PDF viewer for this purpose. You need to upload your PDF somewhere before and just use its URL:
<iframe src="http://docs.google.com/gview? url=http://example.com/mypdf.pdf&embedded=true" style="width:718px; height:700px;" frameborder="0"></iframe>

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?
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");
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".
Use the following html tag:-
<embed src="path_to_pdf_file.pdf" width="800px" height="2100px" />
You first need to go to root dir using "../"
<embed src="../docs/pdf/fileName.pdf" type="application/pdf" width="100%" height="500px" />

How to show pdf file coming from cloud server

I have pdf link file coming from cloud server and i put it into iframe to show it but when i open the page, it direct to download this pdf file and didn't show the file itself, also i used embed and object and it doesn't appear anything.
<iframe src='{$finalurl}' width='1000' height='1000' align='center'></iframe>
Are you required to use an i-frame?
And do you just want to show it, or download it to your 'own' server and show it? That's a big difference.
If you just want to show it (and keep it where it is):
<embed src="http://cloudservice/yourpdf.pdf" width="1000" height="1000" type='application/pdf'>
Good luck

How to view the uploaded(.doc) file in a div?

I uploaded the .doc file using php code and saved it in a folder ,the path is stored in database.
When i tried to make a view of the doc file in a div ...a dialog box appears asking whether to save or openwith ..
I'm using wampserver ....I just tried like this
<iframe name="awindow" frameborder=2 width=580 height=440 src="www/siva_example/pdf/1_siva.doc"></iframe>
Any help regarding..
Is it possible to view .doc file in browser or i have to convert it to pdf format...
It's not possible to display a .doc[x] in HTML. You can try to convert the .doc[x] to HTML or to an image.
you can view doc file by adding this to your header at request time
<?php
header('Content-disposition: inline');
header('Content-type: application/msword');
// not sure if this is the correct MIME type
readfile('MyWordDocument.doc');
exit;
You could try using google docs for this, use the below code, it works perfectly for ppts, have not been able to test it for a doc yet
<iframe src="http://docs.google.com/gview?url=http://yourdomain.com/document.doc&embedded=true" style="width:550px; height:450px;" frameborder="0"></iframe>
Ofcourse you would have to update the url as per your program using PHP

Embed the PDF in a webpage without using the built-in PDF viewer

Currently I am using the standard way to embed an pdf to the browser, however, the built-in pdf viewer for my target browser is not working as expected. I would like to force (Chrome, Firefox and IE8 (if possible, but IE9+ is also ok)) to use the adobe reader. The problem is , I can only change this option manually. Is there any way to change the option in HTML/ JS/ PHP ? Thanks.
<OBJECT data="YourFile.pdf" TYPE="application/x-pdf" TITLE="SamplePdf"
WIDTH=200 HEIGHT=100>
shree
</object>
I try to find the solution and someone suggested header, not working unfortunately e.g.
Content-Type: application/pdf
Content-Disposition: inline; filename.pdf
You can use Google PDF viewer to embed pdf files on to your website. Use this link https://docs.google.com/viewer
Example:
<iframe src="https://docs.google.com/viewer?url={HTTP PATH OF THE PDF FILE}&embedded=true" width="600" height="780" style="border: none;"></iframe>
https://docs.google.com/viewer?url=http://research.google.com/archive/bigtable-osdi06.pdf
Check out PDFObject which is a Javascript library to embed PDFs in HTML files. It handles browser compatibility pretty well and will most likely work on IE8.
In your HTML, you could set up a div to display the PDFs:
<div id="pdfRenderer"></div>
Then, you can have Javascript code to embed a PDF in that div:
var pdf = new PDFObject({
url: "https://sample.pdf",
id: "pdfRendered",
pdfOpenParams: {
view: "FitH"
}
}).embed("pdfRenderer");
Cheers
Trick Chrome and Firefox (and maybe other browsers) into displaying the PDFs using the Adobe Reader plugin (for full PDF Open Parameters support among other benefits) by using one of the following 'Adobe PDF in XML Format' types in your embed code:
application/vnd.adobe.pdfxml
application/vnd.adobe.x-mars
This works fine as of my answer today and I'm hopeful it will continue to work fine. I'm using it currently with standard PDF files as a workaround for embedding PDF files in the browser that need to use the Adobe PDF plugin rather than the browser's built-in PDF rendering. Even though my PDF files are standard (non-XML) files, they appear to load just fine with this new application type parameter.
<OBJECT data="YourFile.pdf" TYPE="application/vnd.adobe.pdfxml" TITLE="SamplePdf"
WIDTH=200 HEIGHT=100>
shree
</object>

Categories