Skip to main content

perl : viewing pdf file out of web folder

How to view a pdf file on your web page?
If the pdf file parked in a web folder like /cgi-bin/files/example.pdf, you can easily show it in a page using the following code

<embed src="/cgi-bin/files/example.pdf#toolbar=0&navpanes=1&scrollbar=0" width="100%" height="500">

so the page will show the pdf file embedded on the web page like the following















but lets say that, the pdf file is not accessible on web page, that means its parked out of web directory for the sake of security. then how to get the file viewed on the web page?

I did the following thing to achieve showing the pdf file out of web directory for testing.
  1. created a tempfile handler within web directory
    use File::Temp qw/ tempfile /;
    use File::Copy;
    use File::Basename;

    my($fh, $fname) = tempfile('TEMP_XXXX', SUFFIX => '.pdf', DIR =>'d:/Inetpub/cgi-bin/temp/');
  2. copied the file to the temp file
    copy("d:/files/example.pdf","$fname") or die "Copy failed: $!";
    $output_file = "temp/".basename($fname);

  3. embedded the temp file on the web page
    < embed src="$output_file#toolbar=0&navpanes=1&scrollbar=0" width="100%" height="500" >


Now the page shows the pdf file out of web folder. one more thing to consider on this is to remove the temp file after usage. by default, the temp file will be removed if application closed. but its better to unlink them after session ends.
this method is not preferred to show secured documents.

cheers!

Comments

Popular posts from this blog

VBScript for URL link creation

We had a need to create URL link of our project on users's desktop using a script. First stage the users had enough rights on their PC's so we included the required icon file also with the script and passed the following VBScript to users to click on it to create the desktop icon link. ' Definie shell object Set objWSHShell = WScript.CreateObject("Wscript.Shell") strDesktop = objWSHShell.SpecialFolders("Desktop") ' Copy the icon file to windows Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFileCopy = objFSO.GetFile( objWSHShell.CurrentDirectory&"\icon.ico") objFileCopy.Copy ("C:\WINDOWS\system32\") ' Define link properties and create link strShortcutName = "Log In" ' Shortcut Name - Edit for the wanted name strShortcutPath = "http://thesystem.com/Login.aspx" ' Link URL - Edit to the wanted URL strIconPath = "C:\WINDOWS\syst...

MySQL Dump cronjob on linux server

Creating mysql dump for production databases is very important. In case of any failure or malefic attack on DB, The only way to restore to previous state is by having proper mysql dump file. Follows an example mysql dump shell script with auto naming with dumping time stamp. Its important to create the shell scripts in unix mode, if you create on a PC, It'll add some unwanted character encoding, and it'll lead to some erroneous response on the script. #!/bin/bash today=`date +%Y%m%d%H%M` backup="cmtdbbackup-$today.sql" mysqldump -u user -ppassword mydb > /apps/mysql_dump/$backup Its important to create the shell script carefully. No spaces, unless its required. in between -u and user name, there is a space, but no space between -p and password. As per the usage of system, we can decide on which interval the cronjob could be set. normally the format of a crontab entry is like the following. * * * * * /apps/scripts/scheduled_job.sh >> /dev/null 2>...

Perl: Read Text Files and Filter Data

Perl is the language for text manipulation. Its designed in a way, to handle the data in whatever the format, and process out the data into the desired format. Lets see some text processing examples from an external text file. Step One: Open a text file for reading,  #!/usr/bin/perl  open (FILE, 'data.txt');  while ( ) {    chomp;    print $_;  }  close (FILE);  exit;