"Strange" characters when saving to PDF - html

I have this pdf file:
SA Pias - Margaça Branco.pdf
I use this to access it:
href="SA Pias - Marga%E7a Branco.pdf"
It opens OK in the browser, but when the user is going to save it, those "strange" characters appears.
Is there any way to appear the real name (SA Pias - Margaça Branco.pdf) when the user is going to save the pdf?

Could you use PHP ?
If yes,
You only need to change the link to something like:
href="file.php?strName=Branco"
Then on the file.php
You will have to add:
header('Content-Disposition: attachment; filename="SA Pias - Margaça Branco.pdf"');

Related

Hiding download links in HTML

I am offering a pdf document in the form of a download from my website via a landing page.
I want to hide the URL/link that displays in the address bar and when i hover over the download button on the web page so that the link cant be shared.
What is the best way to do this? Please explain carefully.
Thanks
Ok, you cannot do that with plain HTML. You can use all kind of tricks but they can be a problem to the user experience, you are to use a server side language.
What you can do is create a php page, name it the way you want (let's say download.php), and link to that one. The page should be something like this:
// Path to the file
$path = '/home/folder/yourfile.pdf';
// This is based on file type of $path, but not always needed
$mm_type = "application/octet-stream";
//Set headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($path)) );
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header("Content-Transfer-Encoding: binary\n");
// Outputs the content of the file
readfile($path);
exit();
This way you just link to your download.php page and it downloads/opens the PDF, like so:
Download
Edited based on BenjaminC suggestions
The other chance you have is to connect this to a database. The database has a table named downloads_table and inside you have 2 fields:
secret: char(32)
downloaded: int(1) dafault 0
Then you create an md5 string
$secret = md5(rand(1000, 9999999));
Place it inside the secret field, create the link:
Download
The user receives/sees a link, when pressed you are to edit the first line of the above code to check in the db if downloaded field = to 0 than procede to download, otherwise the person sees an error page.
This is so that it can be downloaded only once.
(Edit)
If in the future, this gets useful for anyone, the functionality can be seen in this fiddle: https://jsfiddle.net/aznjr87g/
It downloads 2.1.3 jquery.min.js from google.
(Edit end)
This can be achieved using Html5 's Download attribute.
Download PDF
If you hover your mouse over that, it simply shows yoursite.com/#
Place this somewhere in the body of the webpage:
And place this somewhere in the webpage:
<script>
function download() {
document.getElementById("download").src = "/path/to/download";
}
<script>
Then, on the element of the button (In the example of a div) do this in the tag:
<div onclick="download()"> </div>
However if it's a link you will want to do:
An element needs a href to work properly.

Making on Offline form using JotForm

i'm a beginner and just learning about HTML. I just need a point in the right direction here. Any tutorials you can point me to would be greatly appreciated.
I am trying to create an offline version of this form I created on JotForm-http://form.jotform.us/form/40855701907154
I would like it to save the data a user inputs into an excel file.
I have tried saving the page offline by clicking 'save page as' but the 'submit' button doesn't appear.
I've been trying to understand this tutorial-http://diveintohtml5.info/offline.html.
Do I open the HTML file in notepad and try to edit it? If so, how do i edit it?
If you want to save the data to the Excel file.. you must store it first in database, then display your database in php with this header:
<?php
// excel raw data
header("Content-type: application/vnd-ms-excel");
// define the name "results.xls"
header("Content-Disposition: attachment; filename=results.xls");
// echo your database
include 'database.php';
?>
that is the simplest way for begginer... let me know if this work for you...

How to access image files outside project folder in html?

I put the user uploaded image files on separate disk partition on my local dev server(OS windows) which is outside of project folder. I need to access those image files in html pages of web app.
I tried out giving paths like: http://localhost:8080/C://tmp/thumbnails/pl_2001.JPEG but no help. Image is not fetched. How to properly access such files in html pages ?
You will have to use rewrites for this so you don't display sensitive folder names.
if you are using php you can use .htaccess rewrites for a slug something like images/someimage.mime and split/get what's after images/ and have a php function that takes the filename and makes sure it exists and if you want you can check if its a valid mime then send a header to say its a image/somemime so the browser can display the image instead of gibberish it will display without it.
$filename = "C://tmp/" . $file;
if(file_exists($filename)){
$img = getimagesize($filename);
if(in_array($img['mime'], array('jpg','jpeg','gif','png','etc'))){
header("Content-type: image/" . $img['mime']);
readfile($filename);
}else{
//default image
}
}else{
//default image
}
I haven't tested this but it should give you a idea on getting you started. If this isn't the language you are looking for try searching for something similar for your language you are using.

How can I force a link to a PDF to tell the browser to download it as a PDF?

My application generates a dynamic link to any PDF files that are associated with a product. The link is presented like this:
Brochure
If the user right-clicks and selects "Download Linked File As" (or its equivalent), the file is presented with a ".pdf.png" extension in Google Chrome and Safari. Firefox works appropriately, not sure about Internet Explorer.
I want Firefox and Chrome to know that it is a PDF. Because obviously users are going to try to download these, they are going to save it with the wrong extension, and they won't be able to open the file.
Assuming you are using "send_data" from within a rails controller to serve the file might I suggest:
send_data(
data,
:filename => "filename.pdf",
:disposition => "attachment",
:type => 'application/pdf'
)
Where "data" is the contents of the PDF.
For more information checkout the following link:
http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_data
send proper headers in some scripting lang like php or user .htaccess
<Files *.pdf>
ForceType application/pdf
Header set Content-Disposition attachment
</Files>
Have you heard of the content-disposition header? It allows you to tell the browser to ask the user what to do with the file, rather than try and handle it by itself. I don't think it is part of the HTTP spec, but it is documented by the IETF under RFC 2183.
You should be able to use whatever language you are using to alter the HTTP headers before they go to the client. The header you add will look something like this:
Content-Disposition: attachment; filename=filename.pdf
You might also need a Content-Type header:
Content-Type: application/pdf

Download a picture OnClick

How do you actually download a picture when you click on it? Is there some kind of a javascript code to do just that? Here is how i show the image with pure HTML.
<img src="myPic.png" border="0">
Assuming by "download" you mean "Cause the user's browser to throw up the 'save or open' dialogue" — you can't.
You could link to another URL offering the same file but with the Content-Disposition header set to attachment. The specifics of how you would provide such a resource at that URL would depend on the server side capabilities on offer to you.
Most people right-click on the image and choose "Save image as..."
The alternate is to link to use a server-side script that sets a "Content-type" and "Content-disposition" header. In PHP, that would be something like this example from the docs:
header('Content-Type: image/png'); // or 'image/jpg' or 'image/gif'
header('Content-Disposition: attachment; filename="filename.png"');
readfile('original.png');
UPDATE: Since you say the image is generated by a PHP script in the first place, there are a few options:
Put the URL (sig.php?...) as the parameter to readfile. This will mean double processing for anyone who clicks to download.
Cache the output from your image generation script to the filesystem, then pass that file to readfile.
Edit the image generation script to accept an extra parameter like mode=download and then where you are about to output the image, if the parameter is present, set those two headers above.
I trick this out a bit - I zip the picture and put it somewhere. Then, instead of using a fancy script or server-side stuff, I make the picture's link the location of the .zip file. The user get's warned/asked if they want to download this zip and voila...
But this is usually in an area where the user is someone who would want the image...
Do you want to open the picture in a new window/tab? Or do you want to download it to the users computer? If you want the user to save the image, then you need to set the content-type of the file they receive:
<?php
$file = $_GET['file'];
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
?>
Remember to check the input so people can't download source files.