How to preview Pdf file without download - javascript

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".

Related

Is there any way to open file in its default application using JavaScript?

I want to open a file in its Windows default application.
Actually, the scenario is as below:
A file is saved in Database and it is shown as a preview in CKEditor
When I double click on that file, it should open in its default application. The file can be a Word or Excel or PDF file
Then the user will edit the file in that default application
And when the user saves that file or closes the document, it should be updated on the server also it should refresh the file preview in CKEditor
I just want to know if there is any way to open the file in its default application.
Things I have tried:
I tried to use the ActiveX object to open the file, but it is not supported in Chrome and Firefox. And anyway, many forums are saying that it is the end of ActiveX era.
Tried to find if there is any CKEditor plugin, but did not find any.
Thank you in advance for your answer.
Short answer: no. Can you imagine the security nightmare if JavaScript was allowed to execute arbitrary binary files on a user's machine?
Your solution is going to be to find a JavaScript library that allows for handling those files, similar to how PDF.js was made for PDFs in-browser.

display the pdf in the same window

there is a requirement where i have to show the pdf in the same window (may be inside the embed/object/iframe).
i am getting the pdf from the service in array buffer format, when i get it i am converting it to blob and generating a temporary src for this using
URL.createObjectURL(file)
I know that createObjectURL is not going to work in IE as it is not allowed there.
now the only option i have left with is to use any third party library like pdf.js to make this thing work.i have tried the same and i had some success in it.But as it comes with the big worker.js library i am trying not to use it for just one thing.
can someone please suggest me if is there any other way of achieving this task?
i have following things or questions in my mind.
host this pdf online and just return the pdf link to the front end(i am not a bakend developer so i dont know if it is possible to host pdf dynamically somewhere in the same domain or at any third party host)
get something equivalent to 'URL.createObjectURL' for explorer and then use object/embed/iframe to show it
render the pdf in a page at server side and just return the whole document to front-end.
try a heavy library like pdf.js and make it work(last option for me)
any other solution which i am not aware of.
thanks in advance for your help.

Opening files of different files types in browser

In a single page we have multiple links to files of different file types. A single link looks like this:
download sandglass
On click users simply get the file downloaded and can then open it in their devices.
The result that we want to achieve is to open the file inside the browser in a new tab.
Question is: how to achieve a similar result to how attachments are opened in gmail for example?
File types that need to be opened in a new tab are usually these: pdf, doc, docx, xls, xlsx, ppt, pps, jpg, png.
Any help or guidance is much appreciated.
Can you manipulate these a tags?
If yes, you can remove the download attribute, and add a target attribute with the value of _blank to open the file in another tab.
download sandglass
But Chrome and other browsers can't open preview of some files that you listed
here natively, doc, pps and xls for example.
A solution for these unsupported files would be to resave them to PDF so the user can preview them in browser. Maybe you can resave them server-side with services like http://www.aspose.com/products/words
Take a look at this answer, maybe can help you too:
How do I render a Word document (.doc, .docx) in the browser using JavaScript?

How to use JavaScript/JQuery to download a PDF from a URL, then POST the file to a different site

I currently have a URL to a PDF file like this: http://www.example.com/HelloWorld.pdf
What I would like to do is use this URL to download the file and store it momentarily on the browser so that I can POST it to http://www.example2.com/api/Uploads (I don't want to save it on the user's machine)
My goal is to achieve this using JavaScript/JQuery/AJAX. I'm currently reading documentation for Blobs and also going through this post.
Will downloading the PDF into a blob and trying to upload the raw data even work? Any help with this is greatly appreciated.
You would face with security issues in your browser. For example: "Cross origin request". You can't just run a script that would download arbitrary files without jumping hell a lot of hoops and even then, the feasibility would be questionable.

How can I force a "save as" option when the user clicks a link

I have a link to a PDF on my web page and I want to know if it's possible to always ask the user if they want to download the PDF or open it in a new window. Is this possible? I realise that people would not want the PDF to automatically download, but 2 links or an option would be great.
There is quite a lot of documentation about this on the internet, some even saying I'd have to have PHP and this is not possible on the server I use.
I need a javascript, jQuery solution or alternative suggestion. Not asking for much I know.
Thanks very much for reading this and I hope it makes sense.
All the best!
T.
It can be done, but not in clientside javascript. If you have a server side script that feeds the file, it can send a Content-Disposition: attachment header along with the response. This will trigger a save as dialog in the browser.
If they are static files, you can also change the configuration of your server. For Apache use the FilesMatch directive. There's a nice example for PDF files here:
http://www.thingy-ma-jig.co.uk/comment/7045

Categories