I need help solving a minor problem I have run into while creating a pdf embed page.
The goal is to click on an anchor tag which sets a Session variable with the directory of the pdf file (to be displayed). The problem I am having is that the src="[directory]" is not working for me. It does work if I hardcode a pdf that is in the current directory, but if i use a directory like "C:\folder\pdf.pdf" the src will not show up.
<?php session_start(); ?>
<html style="display:inline;">
<body>
<div style="display: inline; position: relative; float: left; width: 69%">
<embed type="application/pdf" src="<?php echo $_SESSION['pdfLocation']?>" style="width: 100%; height:620px;"></embed>
</div>
My Answer:
attribute in the embed tag is not able to access files outside of its current directory.
To solve this issue, you can use the PHP `file_get_contents()` function to read the contents of the PDF file and then use base64 encoding to embed the PDF file directly into the HTML code. Here's an example of how you can achieve this:
1. Create a PHP file (e.g., `pdf_embed.php`) that will handle the embedding of the PDF file:
php
session_start();
if(isset($_GET['pdf'])) {
$_SESSION['pdf'] = $_GET['pdf'];
}
if(isset($_SESSION['pdf'])) {
$pdf = $_SESSION['pdf'];
$pdfData = file_get_contents($pdf);
$pdfBase64 = base64_encode($pdfData);
echo '';
} else {
echo 'PDF file not found.';
}
?>
2. In your HTML file, create an anchor tag that will set the Session variable with the directory of the PDF file when clicked:
html
View PDF
Replace `path/to/your/pdf/file.pdf` with the actual path to your PDF file.
Now, when you click on the anchor tag, the PDF file will be embedded in the HTML page using base64 encoding. This way, you can display PDF files from any directory in your WAMP server.
Rate this post
4 of 5 based on 5787 votesComments