Show a Pdf Files in Users Browser Via PHP/Perl

Open PDF in browser that is stored outside the webroot

I tried to just comment on your post but I am a noob to overflow. I just wanted to share some google search results. Have you tried @readfile?

Show a PDF files in users browser via PHP/Perl

open PDF in browser with PHP

http://php.net/manual/en/function.readfile.php

Hope it helps!

Display PDF on browser

you can use PDF Merger library to achive your goals.
Here is the link to original library (outdated).
Prefer myokyawhtun fork which is maintained

and a sample code will be as following

include 'PDFMerger.php';
$pdf = new PDFMerger;
$pdf->addPDF('path_to_pdf/one.pdf', '1, 3, 4') //include file1 - pages 1,3,4
->addPDF('path_to_pdf/two.pdf', '1-2') //include file2 - pages 1 to 2
->addPDF('path_to_pdf/three.pdf', 'all') //include file3 - all pages
->merge('browser', 'test.pdf'); // OUTPUT : make sure you choose browser mode here.

Supported modes - browser, file, download and string.


Edit : As i can see you have tagged CI you can put PDFMerger.php in applications/libraries.
and load it in autoload.php or in controller

and then can use it like $this->pdfmerger->addPDF() and merge() functions.

displaying pdf on a website

Well, there's always a third way: serve the PDF itself and leave the rest to the visitor.

PHP to view PDF on web page and disable user to download

Method 01

Implementing that useing Google books

<iframe frameborder="0" scrolling="no" style="border:0px" src="https://books.google.com.kh/books?id=e5MkzETNcsgC&lpg=PP1&dq=typography&pg=PA11&output=embed" width="500" height=500>
</iframe>

Sample Image

In above image it shows Embed that will be the code. And Download or Print option is not available on this. Google dosc can prevent download but its allow to Save to Drive option. Then in drive i can download it. But Google books not allow any of that.

in view

02

Method 02

Using Google Drive

Right click on pdf and goto Share(below image)

03

Then go to Advanced option in left bottom

05

Tick Both check boxes. After copy embed link and paste it to your src. No download and Save drive option is not allowed

Note: Method 01 and Method 02 is Tested

Perl : Scrape website and how to download PDF files from the website using Perl Selenium:Chrome

Here is some working code, to help you get going hopefully

use warnings;
use strict;
use feature 'say';
use Path::Tiny; # only convenience

use Selenium::Chrome;

my $base_url = q(https://www.fda.gov/drugs/)
. q(warning-letters-and-notice-violation-letters-pharmaceutical-companies/);

my $show = 1; # to see navigation. set to false for headless operation

# A little demo of how to set some browser options
my %chrome_capab = do {
my @cfg = ($show)
? ('window-position=960,10', 'window-size=950,1180')
: 'headless';
'extra_capabilities' => { 'goog:chromeOptions' => { args => [ @cfg ] } }
};

my $drv = Selenium::Chrome->new( %chrome_capab );

my @years = 2017..2021;
foreach my $year (@years) {
my $url = $base_url . "untitled-letters-$year";

$drv->get($url);

say "\nPage title: ", $drv->get_title;
sleep 1 if $show;

my $elem = $drv->find_element(
q{//li[contains(text(), 'PDF')]/a[contains(text(), 'Untitled Letter')]}
);
sleep 1 if $show;

# Downloading the file is surprisingly not simple with Selenium (see text)
# But as we found the link we can get its url and then use Selenium-provided
# user-agent (it's LWP::UserAgent)
my $href = $elem->get_attribute('href');
say "pdf's url: $href";

my $response = $drv->ua->get($href);
die $response->status_line if not $response->is_success;

say "Downloading 'Content-Type': ", $response->header('Content-Type');
my $filename = "download_$year.pdf";
say "Save as $filename";
path($filename)->spew( $response->decoded_content );
}

This takes shortcuts, switches approaches, and sidesteps some issues (which one need resolve for a fuller utility of this useful tool). It downloads one pdf from each page; to download all we need to change the XPath expression used to locate them

my @hrefs = 
map { $_->get_attribute('href') }
$drv->find_elements(
# There's no ends-with(...) in XPath 1.0 (nor matches() with regex)
q{//li[contains(text(), '(PDF)')]}
. q{/a[starts-with(@href, '/media/') and contains(@href, '/download')]}
);

Now loop over the links, forming filenames more carefully, and download each like in the program above. I can fill the gaps further if there's need for that.

The code puts the pdf files on disk, in its working directory. Please review that before running this so to make sure that nothing gets overwritten!

See Selenium::Remove::Driver for starters.


Note: there is no need for Selenium for this particular task; it's all straight-up HTTP requests, no JavaScript. So LWP::UserAgent or Mojo would do it just fine. But I take it that you want to learn how to use Selenium, since it often is needed and is useful.

Show pdf file into browser without Adobe Reader

You can use SWFTools to convert PDFs to Flash. But SWFTools default skins are not looking good at all, so you can check Flexpaper as a viewer.
In case you are going to make your own skin, then could be useful to read: SWFTools (pdf2swf) to properly work with Flex

Another solution is pdf.js (as Quentin point out), but AFAIR this script was not usable(back then, when I looked for) for PDFs with complex structures (complex gradients for example).

If you need to publish documents as "private" you should look at services as Issuu or you will have to protect files from downloading.

How to open an application via php and perl?

Try this:

my @args = ('C:/Program Files (x86)/Adobe/Reader 10.0/Reader/AcroRd32.exe');
if (system(@args) != 0) {
# Can't run acroread. Oh Noes!!!
die "Unable to launch acrobat reader!\n";
}

The thing about system() is that it does two different things
depending on the number and type(s) of argument it gets. If the
argument is an array or if there are multiple arguments, Perl assumes
the first is the program to run with the rest as its arguments and it
launches the program itself.

If, however it's just one string, Perl handles it differently. It
runs your command-line interpreter (typically CMD.EXE on Windows) on
the string and lets it do what it wants with it. This becomes
problematic pretty quickly.

Firstly, both Perl and the shell do various kinds of interpolation on
the string (e.g. replace '//' with '/', tokenize by space, etc.) and
it gets very easy to lose track of what does what. I'm not at all
surprised that your command doesn't work--there are just so many
things that can go wrong.

Secondly, it's hard to know for sure what shell actually gets run on
Windows or what changes Perl makes to it first. On Unix, it usually doesn't matter--every shell does more or
less the same with simple commands. But on Windows, you could be
running raw CMD.EXE, GNU Bash or some intermediate program that
provides Unix-shell-like behaviour. And since there are several
different ports of Perl to Windows, it could well change if you
switch.

But if you use the array form, it all stays in Perl and nothing else
happens under the hood.

By the way, the documentation for system() and $? can be found here and here. It's well worth reading.

how to open pdf file that is placed in direct access blocked directory in iframe html

I have Updated my code to the below code and all got resolved..

    session_start();
?>
<?php
$mime = mime_content_type($_GET['filename']);
if(strstr($mime, "image/")){
?>
<img src="fileViewer.php?filename=<?php echo $_GET['filename'] ?>" style="width: 70%;margin-left: 15%;">
<?php
}
else if(strstr($mime, "application/pdf")){
$file = $_GET['filename'];
$filename = $_GET['filename']; /* Note: Always use .pdf at the end. */
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);

?>
<?php
}
else{
?>
<iframe src="fileViewer.php?filename=<?php echo $_GET['filename'] ?>" style="width: 100%;height: 100vh;"></iframe>
<?php
}
?>

Thank you all



Related Topics



Leave a reply



Submit