Jump to content

PDF file search and embed


Intersysop2

Recommended Posts

Hello i am trying to embed a collection of pdf in a in a web page, i got the name to work but when i click on it, i get a 

Blank square, does anyone see anything wrong with my code ?

Thanks !

$dir = "C:\inetpub\wwwroot\Docs\Manuals\Honda"; // Change this to the directory containing the PDF files
$files = scandir($dir);

foreach ($files as $file) {
  if (substr($file, -4) == ".pdf") {
    $filename = substr($file, 0, -4);
    echo "<button class=\"collapsible\">$filename</button>";
    echo "<div class=\"content\">";
    echo "<embed src=\"$dir/$file\" width=\"100%\" height=\"500\">";
    echo "</div>";
  }
}
?>

<script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll.addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}
</script>
 

Link to comment
Share on other sites

This line:

 $filename = substr($file, 0, -4);
   

is not doing what I think you want.

Given 

file1.pdf

that substr using 0,-4 will give you just

file1

Are you trying to let the user select from a list of files (in the buttons) and have your script call another "display script" to put it on the screen for the user?  Or is that js code supposed to do something to the current page?

Link to comment
Share on other sites

So that function that simulates an Ajax call - does it already work for you somewhere else?  Do you know in fact that the event handler is being called?  Add an alert to it perhaps to test it out?

Of course this is after you get all the pdfs uploaded.

Edited by ginerjm
Link to comment
Share on other sites

23 minutes ago, requinix said:

You can't embed files through C:\ if you're running this from a website.

The "dir" that you use for the embed has to be a URL. Probably "/Docs/Manual/Honda". No, you can't also use that with scandir.

Can i amend or convert the string to a web address ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.