I am creating a web application where users can upload/download/view online pdfs. I want to change the name of the pdf file to view in new tab link (like we see in all websites).
Does anyone know how to make any field in the database that gives a link that when user clicks, it opens the pdf in a new tab?
if($result) {
if($result->num_rows == 0) {
echo '<p>There are no files in the database</p>';
}
else {
// Print the top of a table
echo '<table width="100%">
<tr>
<td><b>Name</b></td>
<td><b>type</b></td>
<td><b>Size (bytes)</b></td>
<td><b>Created</b></td>
<td><b> </b></td>
<td><b>view><b/></td>
</tr>';
// Print each file
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>{$row['name']}</td>
<td>{$row['type']}</td>
<td>{$row['size']}</td>
<td>{$row['created']}</td>
</tr>";
}
// Close table
echo '</table>';
}
// Free the result
$result->free();
}
else
{
echo 'Error! SQL query failed:';
echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
The solution doesn't have to be within the database.
You can use an anchor tag to redirect the user to the link :
<a href="{$path_to_pdf}" target="_blank" >Click here to view the PDF</a>
Put that in a cell in the table you are generating.
I could be mistaken - but I do believe that the users browser is also a factor here. The browser has to be able to display pdf's. The latest and greatest* browsers have it baked in but user preferences' might also be a factor.
* Let your imagination run wild
You can use target="_blank" in your a tag
PDF
This works in Google Chrome, not sure if it works in other browsers.
You can look here http://www.allaccessliving.com/residences/floorplans#1bed15bath:35A
There's a pdf link and you see for yourself if you inspect that element.
When outputting the link to the PDF, use this:
Visit StackOverflow
setting target will tell the browser to open the link in a new tab.
Related
Im trying to allow my user to download the file that they select in my system. The code i have used downloads something but it says failed forbidden. I haven't used directories to store the files but rather stored the file in a longblob data type directly in the database. If someone could please show me how to get it to download successfully I would appreciate it.
<tbody>
<?php
require_once 'dbcon.php';
$query = $conn->query("SELECT fileID, filename, file, dateuploaded from empfiles WHERE eemail = '$_SESSION[login_employee]'");
$count = 1;
while($fetch = $query->fetch_array()){
?>
<tr>
<td style="height:50px;width:100px"><?php echo $count++?></td>
<td style="height:50px;width:200px"><?php echo $fetch['filename']?></td>
<td style="height:50px;width:100px"><?php echo $fetch['dateuploaded']?></td>
?>
</td>
<td colspan="2">
<td>
echo"<a download href=\"file/{$row['file']}\">Download this file</a>";
</td>
</tr>
<?php
}
?>
</tbody>
The value of the href attribute needs to be a URL.
You can't just take a blob of PDF out of the database and coerce it into a string.
You need to either base64 encode it and then convert it to a data URL (noting that there is a good change this will result in a URL that is too long for the browser to accept).
or
Create a new webservice that extracts the PDF from the database and generates the download based on, for example, the row id. Then link to that webservice with the row id in the query string.
OK this is my first post here so, hello! and please go easy on me if this is a simple solution.
I am creating a document database using apache2,mysql and php. I have only had previous experience with html,css and a small amount of javascript so this has been a steep learning curve and a good challenge for me.
I have successfully created the database and uploaded documents into it. i can search and link to the documentment required in a html/php screen but i only seem to be able to use/search, recall one extension type. this would be either .PHP, .PDF, .html, or .docx etc. In reality not all documents are or will be PDF's, below is my script:
/* -- end of connection script --*/
if(isset($_GET['search'])) {
$get_value = $_GET['user_query'];
if($get_value=='') {
echo "<center id='alert'><b>nothing in the search field</b></center>";
exit();
}
if($get_value=='Search...'){
header('Location: index.php');
}
$result_query = "SELECT * from search WHERE site_keywords LIKE '%$get_value%' OR site_title LIKE '%$get_value%' OR site_description LIKE '%$get_value%' ";
$run_result = mysql_query($result_query);
?>
<h3 class="total"><?php echo $row_result; ?>Results for '<?php echo $get_value; ?>'</h3>
<?php
if(mysql_num_rows($run_result)<1) {
echo "<center id='alert'><b>Oops! no search results found</b></center>";
exit();
}
while($row_result=mysql_fetch_array($run_result)) {
$site_id=$row_result['site_id'];
$site_title=$row_result['site_title'];
$site_link=$row_result['site_link'];
$site_description=$row_result['site_description'];
$site_file=$row_result['site_file'];
echo"<div class='results'>
<h3><a href='$site_id.php' target='_blank'>$site_title</a></h3>
<p align='justify'>$site_description</p>
</div>";
}
}
?>
as you can see from the echo results the href file requires an extension (in this case .php) to correctly display a .php document but not other extension types. If i change this to .pdf it will display the .pdf but not .php and so on. I am at a loss.
I'm using the fpdf library to output a pdf from html. The .pdf is being created because I can email it to myself and it arrives in the correct format but if I want to download the .pdf as an option without emailing, the output is illegible. The output appears in a browser window(see attached screen shot) and I'm unsure how to fix this issue.
I've also attached a screenshot of how we have our report options set up. 1. HTML 2. PDF 3. Download 4. Email -- the HTML and Email options work, the PDF and Download options do not. I'm focusing on the Download option in this question.
This is the output code that I've tried to test out but no luck
//$pdf->Output("D","D:/example2.pdf");
//$content = $pdf->Output("","S");
//$pdf->Output(); //Outputs on browser screen
//Outputs on browser screen
$pdf->Output();
//echo file_get_contents($pdf);
//readfile($pdf);
the $pdf->Output(); is generating the illegible code the readfile
echo and file_get_contents throw errors
the $pdf->Output(... gives me an error that says Incorrect Output
Destination (see attached screenshot)
Need guidance -- thanks for any help.
here is the full code:
<?php
$m_header = '<link href="shared/report.css" rel=stylesheet type="text/css">';
$m_body_tag = ' scroll=no';
require_once($DOCUMENT_ROOT."inc/top-2.inc.php");
$i_get_sid = isset($_GET["sid"]) ? (int)$_GET["sid"] : $i_sid;
$i_get_pass = isset($_GET["a"]) ? $_GET["a"] : $_SESSION['r_pass'];
$i_get_pass = addslashes($i_get_pass);
$i_pdf_file_url = 'report.php?sid='.$i_get_sid.'&a='.urlencode($i_get_pass).'&b=/report.pdf';
echo '<table cellpadding=0 cellspacing=0 border=0 width="100%">';
echo '<tr vAlign=top><td height=7><img src="images/1x1.gif" width=1 height=7></td></tr>';
echo '<tr height=28 style="background: url(images/bookm-bg.gif) repeat-x"><td width="100%"><nobr>';
echo '<img src="images/1x1.gif" width=5 height=1><img src="images/bookm-42.gif" width=67 height=28 border=0><img src="images/bookm-s1.gif" width=10 height=28 border=0><img src="images/bookm-51.gif" width=65 height=28 border=0> <img src="images/button-downloadpdf.gif" width=80 height=28 border=0> <img src="images/button-emailpdf.gif" width=80 height=28 border=0>';
echo '</nobr></td><td><nobr><font style="font-size: 10px;">Close Window </font></nobr></td></tr></table>';
(This is what I added as a workaround)echo '<p><a href="'.$i_pdf_file_url.'" name="plugin" width=100% height=100% fullscreen=yes style="position: absolute;">Click Here to open the PDF</p>';
(This is what should display the PDF in the browser but wont' work)
echo '<p><embed type="application/pdf" src="'.$i_pdf_file_url.'" name="plugin" width=100% height=100% fullscreen=yes style="position: absolute;"></p>';
require_once($DOCUMENT_ROOT."inc/btm-2.inc.php");
?>
From documentation:
Destination where to send the document. It can be one of the following:
I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.
The default value is I.
If you want to force download with given name, create a link that points to your pdf script and use this:
$pdf->Output("__name__","F");
If you want to display pdf for preview use this:
$pdf->Output("__name__","I");
and in your html inside PDF tab use iframe to embed pdf:
<iframe src="pdf_preview.php" frameborder="0"></iframe>
There are other ways to do this, but this should be the easest.
I am trying to get my image to display when clicked via the link. However when I click on the link it finds the image id as displayed in the url link ok but does not display any image. Can you please help?
<?php
//sets up thisPage
$pageSize=10;
if (isset($_POST["thisPage"])) $thisPage=$_POST["thisPage"];
else $thisPage=1;
//selects all distinct expenses that have been uploaded
$dbQuery="SELECT * FROM images WHERE user_id = '$userID' ";
$dbResult=mysqli_query($db_connection, $dbQuery) or die(mysqli_error($db_connection));
echo "<table cellspacing=\"5\" class=\"recordsTableBG\"> <thead
class=\"recordsTableHeader\">";
echo '<tr> <th>ID</th><th>Amount</th><th>Description</th><th>Filename</th>
<th>Project ID</th><th>Status</th></tr></thead>';
echo '<tr class="alternateRowColor">';
'<tr>';
while ($dbRow=mysqli_fetch_array($dbResult)){
echo "<img src = 'uploaded/$image' width = '200' height = '200'>";
// display row with expense
echo '<td>'. $dbRow['id'] .'</td>';
echo '<td>'. $dbRow['user_id']. '</td>';
echo '<td><a href='.$_SERVER['PHP_SELF'].'?imageid='.$dbRow['id'].'>
Click here to view image</a></td>';
}
echo "</table>";
echo "</form>";
?>
<!-- add submitt button
close form -->
</div>
You have a mixture of two approaches going on here, and that's where the confusion lies.
First, notice your
<img src='uploaded/$image' width = '200' height = '200'>
The browser is going to see something like
<img src='uploaded/picture_of_cat.jpg' width='200' height='200'>
and render a 200px by 200px image of a cat. I'm assuming that's your thumbnail. (As an aside, I notice that it's not enclosed in <td></td> even though it's in the table row).
In another part of the table, you have
<a href='.$_SERVER['PHP_SELF'].'?imageid='.$dbRow['id'].'>Click here to view image</a>
which is going to render as something like
<a href='http://example.com/index.php?imageid=1234'>Click here to view image</a>
When the user clicks that link, it's going to make a GET request to the server with imageid equal to 1234. What is the server going to do with that imageid? In the code you've posted, nothing.
You have two choices:
if you want to keep the link as it is, you'll have to write some code that will take the imageid value, find the appropriate image, and return it to the browser as image data - that means setting the appropriate headers and sending the data back as binary data.
the simpler way to do it would be to replace the URL in the link with the same one you have in your <img> tag - when the user clicks on it, the server will just return the image.
This might have been already answered, yes, I used Google, and the search here stackoverflow, but the problem remains...
I'm using TCPDF library to generate PDF file, it works just fine, but I have the following situation, in order for my to generate a PDF ot just the HTML I need to put a few foreach and while's and IF's inside the HTML so that I can get the layout that the user is requesting...
so...
if(isset($_POST['submit'])) {
$ids = htmlentities($_POST['id'], ENT_NOQUOTE;
$con = conectionDB();
$query 'SELECT * FROM books WHERE id = "$ids"';
$doit = $con->query($query);
// at this point everything is file
// a few if's and we are done with the fetching "books" data
// a few other tuff that is required from the library nothing fancy...
// now here is the hard part
// next line will build my layout to display my PDF
$build_HTML = <<<EOD
<style>
.clasess {...}
.nother {...}
</style>
<table width="100%" border="1" cellpadding="0" bordercolor="#333333" >
<tr>
<td>Author</td>
<td>Books</td>
</tr>
<tr>
<td> Jong </td>
<td>
<table>
<tr>
<td>Title</td><td>Year</td>
</tr>
// Here is the problem I need to put a query to fetch the related data from
// another table
$books = conectionDB();
$bookQ = "SELECT * FROM titles WHERE name = '$author_name'";
$doitTitles = $books->query($bookQ);
if ($doitTitles->num_row > 1) {
while($dos = $doitTitles->fetch_assoc()){
// my td's, but this doest work...
}
}
</table>
</td>
</tr>
</table>
EOD;
$pdf->writeHTML($build_HTML, true, false, false, false, '');
} else {
// Go back...
}
As you can see I need that query right there, you may have notice that I have $doitTitles->num_row > 1 why? because if there is more than 1 tittle the layout would different if there is only 1 record...
We know that, that wont work, so the question is, is there another way to do that?
now, before the user go to the PDF, I display the information in plain html, which mean that the HTML that I use before the user go in to the PDF will be the same... so I was wondering, there another library that I can use to render the HTML in to PDF instead of building the PDF from inside the file... the user is able to see the result so those result just put 'em in a PDF...
Thank you!
I have the solution, so the thing is that I'm trying to output an html that is been build with dynamic content, if that was to be show as any other page, my_page.html there is no problem, but in PDF using <<
$raw_html = 'Tables';
// a few while and foreach's and queries
$raw_html .='Closed tables';
// Ready the HTML
$html = <<<EOD
$raw_html
EOD;
$pdf->writeHTML($html, true, false, false, false, '');
$pdf->Output('pdf_file_with_dynamic_html.pdf', 'I');
// the end.
by preparing the html before the output it gives me the data I need the way i need it...
that solved my problem.