Download pdf in javascript and embed it - javascript

I have a webservice that will respond with a pdf on certain post requests.
I need to be able, in javascript, to download this pdf and embed it on my page. How would I do that?
I don't have a direct URL only the data passed back by the AJAX

You can try out PDF.js. However, it doesn't support all the browser.
An alternative with IE support is thatyou could just insert a iFrame, and change the src to the location of the PDF.

Related

How to preview Pdf file without download

I have a url which makes a rest call and when I go to this url it downloads a pdf file.
What I want to do here is preview this file before download. In IE it works like this. It asks me to open or save. But I have a problem with Chrome because it downloads the file directly.
How can I solve this problem?
Can I solve it with javascript or should I make it with Java?
I also tried to open a stream and getting a bytearray but I couldn't achieve the preview like this.
Thanks in advance.
You may do this via javascript using pdfjs library from Mozilla. Here list of supported browsers. It will be look like this
#rest API
/some/url/to/pdf/file.pdf #GET rest call to download pdf file
/preview/some/pdf/file #GET rest call to retrieve html page with pdfs library
I may be misunderstanding your question. I think what you are saying is that Chrome just opens the pdf file in the frame, rather than asking you about open/save. If I'm interpreting right, you want the "Do you want to save this file?" option in all browsers. If so, you might want to set the Content-Disposition header to "attachment".

Download file through Android Chrome Browser

I'm trying to make it so that a file from my web server can be downloaded to an Android mobile device. I want the user to be able to press a Download button on the site, and then they will have the option to save it on there phone's sd card.
The only way I can think of doing this is by using FTP, and having the button navigate the user to ftp://username:pass#webserver/file.blah
Is there another way, using strictly HTML5 and JS?
I have tried using the download tag, but that doesn't seem to work. Instead of downloading the image it redirects them to the page with the image on it.
Link
HTML5 solution
To force downloading a file, if you are using HTML5, then you can use the download attribute by defining the anchor tag like this.
Download Link
Server-side solution
It is likely that the user's browser doesn't support the download attribute. Check this. So the other solution invloves the server-side. You can use the Content-Disposition HTTP header. Serve this as one of the headers of the HTTP response for the download route. This will force the browser to download the file as an attachment.
Content-Disposition: attachment; filename="download.png"
RFC Read section 19.5.1

How to communicate from a embedded pdf to the browser

I have a webpage that has pdf embedded in it, by a object tag.
Now I want to call a javascript method , present in the webpage, from pdf.
Basically, I want to communicate from pdf to browser. Is there a way to do it?
I found the solution myself.
Using a javascript API, hostContainer.postMessage provided by adobe, we can communicate between a hostContainer i.e. a browser and pdf.
For more information please check
http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf

Downloading HTML files without opening a browser

I have a URL which I don't want to open up in a browser. The servlet which my URL points to is able to download the JPEG image of the rendered HTML document using html2canvas library based on specific params.
How can I download the HTML as JPEG even without rendering the HTML on the client side. Is that possible?

Cache external embed pdf with javascript or html

I have the following external pdf embedded as shown below. The pdf displays fine, but how do I cache the pdf so that the page doesn't redownload the pdf each time I visit the page? Should I use an object tag instead?
<embed src="http://samplepdf.com/sample.pdf#toolbar=0&navpanes=0&scrollbar=0" width="500" height="375">
From my understanding, you have a pdf on your server that you would like to cache onto the client side so that the pdf is not redownloaded each time the user refreshes the page.
By sending the proper headers, you can enforce cache rules (or at least try to enforce them as any browser can overrule your rules).
Php or in your case, Django, can send the appropriate headers to tell the browser to cache this pdf. My recommendation would be that you link that you provide in the embed tag links to a script instead of the pdf diriectly. This django script would sent out 2 sets of headers. One tells the browser to cache this content and other tells the browser it is sending a PDF file. This ensures that the pdf gets cached. When you load a webpage, the typical headers' scope by not encompass all external files/pdfs. As each is a separate request.
The method that I prescribed adds extra work than simply embedding the pdf but it should get the job done.

Categories