Read pdf without download and print it with html php - javascript

Good evening,
I would like to create a feature for my users that allows them to read a pdf file without downloading or printing it.
They will only be able to read the pdf file.
I tried pdf.js which did not work,
I also tried to convert my pdf to html via a script,
I also tried this php code but the download and print button appears:
<?php
// The location of the PDF file
// on the server
$filename = "/path/to/the/file.pdf";
// Header content type
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
// Send the file to the browser.
readfile($filename);
?>
Do you have any solutions or technologies to suggest to me,
I use PHP, HTML and JS.

Try this one:
<?php
// Store the file name into variable
$file = '/path/to/the/filename.pdf';
$filename = 'filename.pdf';
// Header content type
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Read the file
#readfile($file);
?>
PHP uses a standard code to display the pdf file in web browser. The process of displaying pdf involves location of the PDF file on the server and it uses various types of headers to define content composition in form of type, Disposition, Transfer-Encoding etc. PHP passes the PDF files to read it on the browser. Browser either shows it or download it from localhost server then display pdf.
Note: PHP is not actually reading the PDF file. It does not recognize File as pdf. It only passes the PDF file to the browser to be read there. If copy the pdf file inside htdocs folder of XAMPP then it does not need to specify the file path.

The php script you offered only can download the file. if you use php ,you can try the demo ,which is using pdf2text here
or try the method in this link read-pdf-files-with-php

Related

Force IE11 to save download CSV file

I am trying to download a CSV file to the browser.
I am unable to get it to properly download the file in IE11. Instead it opens a new tab and write the CSV contents to the tab.
I am using just the following javascript:
window.open(sUrl, '_target');
In the IE Developer tools, I can see the Response Header shows:
Content-Type: text/csv; charset=UTF-8
I would like it to prompt the user to save the file.
I've changed the browser settings to always download, yet it still doesn't do it.
I would like to fix this from a change to the Javascript, if possible.
How can I do that?
As far as I know, it's not possible to do this with JavaScript. Take a look at this answer if you're comforable using PHP to download the file.
According to that answer, your PHP should look something like this:
<?php
$file_url = 'http://www.myremoteserver.com/file.csv';
header('Content-Type: text/csv; charset=UTF-8');
header("Content-Transfer-Encoding: Quoted-Printable");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
?>

Download file from URL, then save to user computer via dialog

How can I download file (I just know the URL - the file can have x MB) and then save via dialog to user computer? (Dialog = user can choose the directory, where he want to save the file). Just like in screen.
I have absolutely no idea how to do it. I Google it and I found only how to load the file via FILE input, but I do not need that.
File suffix is "*.db", thanks guys, I'm not such a web guy.
You can do that pretty easily.
Make a php file, let's call it index.php. And this should be all the code you need:
<?php
$file = "/your/location/file.db";
header("Content-Type: application/octet-stream");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='" . basename($file) . "'");
readfile ($file);
?>
I hope I've understood what you actually need. This should do the trick.

PHP CSV create then echo javascript redirect

I am creating a CSV file in PHP, no biggie there, and then force downloading it for the user, again no biggie there. The problem I am coming across is the redirect portion. I know that you cannot create a file and push to the user and then use php redirect afterwards, as it kills the CSV creation due to the HTTP protocols. So instead, I am doing a javascript redirect as follows:
echo '<script>';
echo ' window.location = "'.base_url().'link/'.$item_id.'"';
echo '</script>';
die;
This works fine (it reloads the page), but the portion above that is echoed gets added on to the CSV file when it is downloaded. Here is the php code that creates the CSV and force downloads it:
fwrite($csv, $csv_data);
fclose($csv);
header('Content-type: application/vnd.ms-excel');
header('Content-Length: ' . strlen($csv_data));
header("Content-disposition: attachment; filename=$file_name");
readfile($file_path);
What am I missing in terms of not having the <script> text getting appended to the CSV?
Thanks in advance.

C JAVA PHP TXT ... read instead download

I have website, and I already can highlight code that is written in HTML.
I can store documents on my website, but when I store document (like .php , .c, , .cpp) I can only download them, not opening in next window.
I need some idea how to pass raw code of that file to another page.
I have apache server and I use PHP
I'm not 100% sure what you're trying, but how about posting the file name to a PHP script and reading the file contents using fopen/fread and then just echo it? You should of course make sure that it'll only output files in a specific folder, so this can't be used to hack your site!
<?php
if(isset($_POST['filename']) && is_allowed_to_be_shown($_POST['filename']))
{
$filename = $_POST['filename'];
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
}
?>
Try accessing the pages ending in .php, .c, .cpp, etc via typing in "localhost/[path to file]" in the address bar. That way the files are read by the sever, before being outputted to the browser, meaning that .php (and the like) files will be handled correctly rather than just HTML files.

How do i rename a file on download using HTML5?

i have been working on a website which will allow users to upload and download files. The file is renamed on upload and stored on my server. But when it is downloaded, it has to be renamed i.e to its original name. Presently i am using the following to do so :
Download
But the "download" attribute does not work with firefox. Any alternative using javascript or jquery ? I am using php for server side.
Edit:
Thanks for the solution using php. But that is not what i am looking for. I am using a custom file viewer using javascript. On clicking the thumbnail of the file, the viewer is displayed. An AJAX request is sent to get the link to the file. The response (the link) is used to display the file, also is added to the tag for download. A php solution would mean a page reload on every request, which i want to avoid. So any javascript or jquery solutions ?
On the server use header("Content-Disposition: attachment; filename='Original name'")
Try Below Code
header('Content-Disposition: attachment; filename='.basename('newfilename.txt'));
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize('filename.txt'));
ob_clean();
flush();
readfile('filename.txt');

Categories

Resources