Printing a pdf file stored on server to client side printer - java

I am generating PDFs file dynamically in my application using Apache PDFBox library.
I have jsp page which is having Print button.When user click on that print button i want to generate PDF file and at the same time show pdf file on browser and apply window.print() method.
How can i achieve this in my jsp page?

Create a pdf link on your page and the link should be mapped to the actual location the PDF exists on your server.
The browser actually handles what to do with the pdf (based on your browser settings) .... whether to download it or open it via plugin. The bottomline is you cannot control it via server side code.
In either of the case you cannot apply window.print() because that is only applicable to browser window and not pdf plugin functionality or if it gets downloaded then he has to manually open it.

There is an alternate solution to this. That is show the pdf in a div in your html and print that div.
For how to show pdf in a html div you can look Display Adobe pdf inside a div
For printing a div or any other html element there are jquery plugins available. I have used print.js that will print a html div, it will also maintain your css.
So when user clicks the print button first show the pdf in a div and then call the print function to print that div.

Related

Show PDF cover page as thumbnail on browser

My requirement is that i have to display list of PDF documents on browser as rectangular boxes. In the rectangular box i need to show PDF document cover page as thumbnail. When user clicks on any rectangular box, calls goes to server side and document will download on client.
We have couple of JQuery plugins that works as PDF viewer but what i need is PDF first page as thumbnail.
My questions is can we achieve this on client side?
If not then i will have to possibly work this out on server side by converting PDF page to image using JPedal/Ghost4J tools and then sending images to client side.

Display PDF in browser

I am developing a Java Web Application (jsp/servlet) using tomcat. I need to display pdf file from local machine. can you suggest what is best way to display it?
I used iframe to display pdf file.
<iframe src="resume.pdf" width="100%" style="height:60em">
[Your browser does <em>not</em> support <code>iframe</code>,
or has been configured not to display inline frames.
You can access the document
via a link though.]
</iframe>
I think you can try a Library called XPDF , I think you can convert from PDF to HTML page , or the second option is just let the user open a link to the page (www.yourwebsite.com/pdffolder/somepdf.pdf)
If you need display a pdf file using tomcat, you can access directly to the file using the specific url where the file is located in your navigator, depending on the path where you put the file, so you can access using 127.0.0.1/files/test.pdf for example. If you need generate a pdf, the best tool I think is iText, this is an easy example how to use id: Introducing PDF and iText

Export charts to html - missing images when open html in application. How to set absolute path to image

I am trying to export a jrxml containing a chart to html format.
From there I read the html file and send an html mail with the chart.
When am exporting the report to html, the src for the image is as follows:
src="report2.html_files/img_0_0_0"
and when the mail is sent, the chart is not displayed.
But when i change the src tag to: src="C:/report2.html_files/img_0_0_0", the image is being displayed. Is there a way to write the absolute path of the files in the html file?
You don't want the image to be an absolute path, otherwise when you send it via email it won't show on the client machine unless the image is in the exact location (which it won't be).
You need to make sure that when you send the html file the image is also sent and in the correct format. See the post below for more details on this:
embedding image in html email
I don't think iReport will be able to help you with this so you may have to modify the contents of the HTML file before attaching it to the email.

Create a "print-only" PDF with itext

This question is related to another one I've posted recently: Check printing with Java/JSP
We're looking for alternatives to how we currently print checks in a Java web application via an applet. It seems the consensus is to use PDF for printing and that itext offers the ability to do so with Java.
However, it's important in our particular case that the checks are "print-only" - the user should not have any ability in the application to save the check (I know a savvy user could do a PrintScreen but we want to cover our rears and make no native functionality in the app to save checks).
I haven't been successful in browsing the web to find out if it's possible to create a PDF with itext in this manner. I have seen posts on restricting permissions in a PDF but what I'm really looking for is a way to disable the ability to save a PDF locally using itext.
Does this functionality exist? If so, could you point me to documentation/code samples on it?
I'm presuming that you're serving this PDF and wishing to print it from within a web application / web site where no out of the ordinary client side plug-ins are installed.
If printing the PDF using conventional means (e.g. Adobe Reader), the PDF MUST be downloaded to the browser's cache to be opened and printed. There is no way around that.
Now you can probably prevent the average Joe from saving the PDF locally via the following technique, but any savvy user will be able to inspect your HTML's source and download the PDF directly.
Output your PDF in iText such that when the PDF is opened, a print action automatically occurs
Put an invisible IFRAME on your HTML page which loads this PDF, but is not visible in the browser to your user
When the user loads your HTML page, the PDF will be loaded in the IFRAME and sent to the users printer (presuming that Adobe Reader is installed in the browser). Yes, the PDF will end up in the browser cache, but the user would have to be savvy enough to both recognize this and then hunt it down in their browser's cache.
If this is not acceptable, you're going to have to look at converting the PDF to another file type (e.g. pages are rendered to images displayed in the browser or perhaps a Flash / Java object that sends each page in the document to the printer directly)
The printWriter class gives some static variables for certain options: PrintWriter
And here is another SO post that might help: iText disable printing/Copying/Saving

Show PDF file inside web browser with iText

I developed a report (without and independent of any db datasource) only with iText. I know that with JasperReports this is easy. But in my case I just storage the report in pdf file or I used acrobat for java beans to show the report to user. Now I need to change my implementation code to show this report in web browser instead to save in the file.
How I can do this?
You need 2 thinks, first the browser plugin installed on the browser and second to set the content disposition header as inline in the respose, otherwise the browser will try to download it.
response.setHeader("Content-Disposition","inline; filename=\"file.pdf\"");
I assume that you've already set the Content Type to application/pdf and know how to convert an iText document to a bytearray and transmit it to the user.
A word of caution: if people outside your organization will use this app, the might not have the pdf plugin installed, in which case, the browse will download the pdf instead of displaying the pdf.
if the browser has the appropriate add-on installed, you can just drag and drop a pdf-file into the browser and it will show it.
Or if you have a site on which you want to present the pdf, you just put a link to it:
link to your pdf
Or did you mean you want to generate html-output for your report instead of pdf?

Categories

Resources