How to make PDF preview directly without having to download it - java

I have to upload a PDF file on my website. But I want my users to have a preview of it when shared over sms, email, etc without having to download it as browsers usually do. Can you please tell me how can I do this?

If the content-type header is set correctly it should trigger the browser to display the pdf.
Depending on your application you may also wish to use a javascript viewer like ViewerJS for example however this may be overkill for your needs.

Try this code in your html page. Only support chrome and mozilla browser.
<object type="application/pdf" data="http://www.orimi.com/pdf-test.pdf" width="100%" height="147px">
<p>Your browser does not support PDFs.
Download the PDF.</p>
</object>

Related

Open a PDF in browser disable the embedded Javascript

I created a wicket page which allow to the user to open PDF files. These PDF files are forms, which the user fill and send back. There is also Javascript which is used to automatically fill some fields.
The file is correctly open in the browser but there is a problem: Javascript is disabled and I don't know why. Is it normal? Is it for security reason?
When I download the file and open in Acrobat Reader, it works correctly.
I also disabled the option in Acrobat Reader to force the plugin to be used in place of the default reader of the browser. It works only in Firefox.
Is it possible to activate Javascript?
If not, is it possible to open automatically Adobe Reader?
I prefer not to save directly the file if possible.
If you talk about smartforms, you have to enable JavaScript in Acrobat. It has to be similar in browser or standalone. Also there can be many security reasons in different browsers due to enable/disable JS itself. One of the common reason is XSS voluentabirity. Also you have to know if the browser or its plugin supports JS. Not all browsers use Acrobat reader as well. However see http://www.smartforms.business.gov.au/DeveloperResources/Documents/JavaScript_Instructions.pdf how to enable JS in Acrobat Reader.
Generaly I do not recommend you to use PDF forms instead of HTML forms.

Java How to close PDF from using pdf javascript?

I'm using a servlet to send a pdf stream to a browser.
There is a request that the pdf must open the print dialog when I show it to the user.
I was successful doing this using iText api. Something like this:
stamper.setPageAction(PdfWriter.PAGE_OPEN, new PdfAction(PdfAction.PRINTDIALOG), 1);
Now I do need to close the pdf file after print. I tried using pdfactions, but I'm can't get it. What I tried is:
writer.setAdditionalAction(PdfWriter.DID_PRINT, PdfAction.javaScript("app.execMenuItem('Close');", writer));
or
writer.setAdditionalAction(PdfWriter.DID_PRINT, PdfAction.javaScript("app.close();", writer));
I don't necessarily need to use pdfActions, but I don't see how to get it after user sent the pdf to printer.
Do you have any ideas?
There's a reason why app.execMenuItem('Close'); and app.close() don't work. These methods are designed to close the standalone version of Adobe Reader/Acrobat. I guess you're viewing the PDF in a browser, in which case you use Adobe Reader as a plug-in, Chrome's PDF viewer, pdf.js in Firefox, or any other PDF viewer.
Problem #1: you need to close the browser window from a PDF document. PDFs don't have the power to control your browser. Suppose they would: wouldn't that be a serious security issue?
Problem #2: you embed the PDF inside a HTML page (e.g. using an <object> tag) and establish a communication between the JavaScript in the PDF and the JavaScript in the HTML. I've described how to do this in my book, but: it won't work with all browsers on all OSs. For instance: Chrome's PDF viewer and Firefox's pdf.js will completely ignore your commands.
You are asking a solution using our iText library, but you're asking something that can't be done with any software.

How to display a word-document from google drive on website?

I would like to display some files from google drives embedded on a website.
I'm downloading the pdf's so they use the adobe plugin.
<object data="https://docs.google.com/..." width="100%" height="100%"></object>
But I would like to display other files (word/excel,...) on the website too.
At the moment I'm using preview but I can't find a way to zoom in/out,download button,...
<iframe src="https://docs.google.com/file/d/googleId/preview" width="100%" height="100%"></iframe>
I could also use /edit in stead of /preview but this gives the entire drive ui in an Iframe.
Is there another way to display a drive file on a website?
My application has all the drive Id's stored of the files it has to display. All the users who use the application have read access to the file!
You can easily embed any document using Google Docs Viewer, like this:
<iframe src="http://docs.google.com/viewer?url=https%3A%2F%2Fdocs.google.com%2Fuc%3Fexport%3Ddownload%26id%3D0BzEbtMoF6IXbRGE1MUtHYTI1Rkk&embedded=true" width="450" height="350"></iframe>
where 0BzEbtMoF6IXbRGE1MUtHYTI1Rkk is the file ID.
The other way to display these files would be to export them as PDF. For this, you can use the exportLinks of a file resource.
Google provide an example of how to fetch these export link in the document about downloading files with the Drive SDK.

How do I ensure nicely formatted HTML when I 'view source' on the browser?

When I 'view source' using different browsers my web pages seems to always have lots of whitespace and are badly formatted. I am developing a Java web application using JSPs.
How can I ensure that when I 'view source' I see nicely formatted HTML?
Edit:
When I say formatted I mean typically:
I get this kind of thing:
<div>
<p>some text</p>
</div>
when I want:
<div>
<p>some text</p>
</div>
In my JSPs (which use includes) everything is formatted nicely.
View Source is only going to show whatever the page gives it in browsers that I know of, so if the HTML isn't formatted nicely, you won't see it formatted nicely.
Your best bet would be to use the developer tools in whatever browser you are in. The HTML is normally structed nicely in there when viewing the HTML. In Chrome or Internet Explorer, F12 should open the developer tools and I think F12 opens Firebug in Firefox if you have it installed.
Note: The source/HTML elements typically viewed by developer tools isn't exactly what the page spits out. They often show dynamic elements that were added through script and/or plugins.
Quick source viewer - Chrome Web Store
Altering generated html in a Java webapp is best done by a Servlet Filter. You can roll your own using whatever prettifying software you have to hand, or install one created by someone else, such as the JTidyFilter.
An advantage of this approach is that you can use the filter during development and remove it for production.
I know Chrome's developer tool have a "pretty print" feature for formatting javascript. I don't know about HTML, but you could look. I'm on a mobile device, so I can't.

Browser PDF File Previwer(Google Style)

I am working on an application which needs to preview privileged content in the browser. The preview should work in a way that its NOT possible to download the content. Only reading within the browser is allowed. I have looked at google docs preview but it needs the url of the docs to accessible online. I need to work with content in the intranet
The previewer should not mandate the installation of a pluggin as this would limit the access.
Any hints
Have a look at ImageMagick. If your're using, say, php, you could do something like
exec("convert -resize 500×800 {$file}.pdf {$file}.jpg");
and then display the resulting jpg-file on a webpage.
man convert should give you some further options.

Categories