Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

how can i print a web page by using core java program  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I need to print a web page by using my core java application only.

I actualy need to print driving directions between two places in my application.
Suppose this is the URL of web page whose printing is needed.

http://maps.yahoo.com/#mvt=m&lat=38.203765&lon=-122.003865&zoom=10&q1=94618&q2=95616

I'm able to hit the site and pass search criteria there from my swing application but i'm unable to find a way so that i can get the printout from my own program.
 
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

That's a tough one. You may be able to get an all-Java web browser to open and print the page through its API.
 
chirag bhalla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
Welcome to JavaRanch.

That's a tough one. You may be able to get an all-Java web browser to open and print the page through its API.



Thanks for your quick reply...

But are you quite sure that we would be unable to do the same by using any dominant web browser like IE6 or Mozilla Firefox or Opera.

Can you put some more light and give me a way to follow.

Your effort will be highly appreciated.
 
Ulf Dittmer
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not at all sure that you can't do the same with a native browser - you were asking for a Java solution.

You could do some research into what command line options the major browsers support; maybe there's one that tells the browser to load a page and print it. If there is, you could use the Runtime.exec method to run it.
[ April 19, 2008: Message edited by: Ulf Dittmer ]
 
chirag bhalla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
I'm not at all sure that you can't do the same with a native browser - you were asking for a Java solution.

You could do some research into what command line options the major browsers support; maybe there's one that tells the browser to load a page and print it. If there is, you could use the Runtime.exec method to run it.

[ April 19, 2008: Message edited by: Ulf Dittmer ]



Thanks for giving me a way towards which i can move..

But meanwhile, if someone got a better way regarding this problem then please post your comments.

Thanks in advance...
[ April 19, 2008: Message edited by: chirag bhalla ]
 
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not call this idea "better", but I'll toss it out anyway.

There are plenty of Java libraries that act as a HTTP client, i.e. a web browser. You can send the GET command to the URL, and get the response. You can walk the resulting HTML/mime types and do what you want.

I had to do this for a customer who used a proprietary image management system that would only return PDF files. So I write code to act like a web browser, got the PDF file, and then processed it.

Apache and others have fairly nice libraries to do all the hard work.

Printing, is, of course, very hard to do in a truly system, OS, hardware, and printer independent way.
 
Marshal
Posts: 24473
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by chirag bhalla:
I need to print a web page by using my core java application only.

I actualy need to print driving directions between two places in my application.

Could I suggest that your real requirement is the second sentence there, not the first? That's what it sounds like to me. In which case you don't need to print something that looks exactly like that page with all its irrelevant links and so on, you just need to extract the part of the page which contains the driving directions and print them?

If that's the case you don't need something that can mimic the full rendering power of a web browser, you just need something that can parse HTML and extract certain text from it. And that's a lot simpler.
 
chirag bhalla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
Could I suggest that your real requirement is the second sentence there, not the first? That's what it sounds like to me. In which case you don't need to print something that looks exactly like that page with all its irrelevant links and so on, you just need to extract the part of the page which contains the driving directions and print them?

If that's the case you don't need something that can mimic the full rendering power of a web browser, you just need something that can parse HTML and extract certain text from it. And that's a lot simpler.




Thanks Paul...

You got me right.

That is exactly what i am looking for. I just need to get the text which shows DRIVING DIRECTIONS in left side of the web page.

But when i try to parse HTML and extract certain text from the web page, i'm just unable to get the desired data from the whole web page.
What i get is name of links, some captions and some garbage kind of text.
I guess, this is because of the least compatibility between swing components and HTML.
I'm using file handling code to extract certain text from the web page.

As you said above in your post that it seems a lot easier to you, then could you please give me a solution or hint in which i can move further.

Thanks for your effort.
 
Paul Clapham
Marshal
Posts: 24473
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this particular case you might be better off using Yahoo's mapping APIs for accessing their site instead of trying to scrape their HTML. Unless you really want to interpret the Javascript that displays that page.
 
chirag bhalla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
In this particular case you might be better off using Yahoo's mapping APIs for accessing their site instead of trying to scrape their HTML. Unless you really want to interpret the Javascript that displays that page.




is there any other good approach to do the same.

I've to do all these by using core java(swing application) only. I can't use web technologies like j2ee etc.
 
Paul Clapham
Marshal
Posts: 24473
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't say why you were opposed to using Yahoo's APIs which they supply so you can do things like what you're asking.
 
chirag bhalla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
You didn't say why you were opposed to using Yahoo's APIs which they supply so you can do things like what you're asking.



I'm not opposed to any thing. I'm open to all the feasible solutions.

I'm just questioning that if it would be the most appropriate way of fixing my problem.

If it is then can you please give me some more details like how can i get the help of Yahoo Map APIs in my pure core java(.java) file.
What are the things which need to be taken care of?

Please put some more light.

Thanks
 
chirag bhalla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Pat Farrell:
I would not call this idea "better", but I'll toss it out anyway.

There are plenty of Java libraries that act as a HTTP client, i.e. a web browser. You can send the GET command to the URL, and get the response. You can walk the resulting HTML/mime types and do what you want.

I had to do this for a customer who used a proprietary image management system that would only return PDF files. So I write code to act like a web browser, got the PDF file, and then processed it.

Apache and others have fairly nice libraries to do all the hard work.

Printing, is, of course, very hard to do in a truly system, OS, hardware, and printer independent way.




Thanks Pat...

But i've do all these in my swing application.

I can't use APACHE or anything like that. All i've to do will be a totaly (.java) file.
 
Ulf Dittmer
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I can't use APACHE or anything like that. All i've to do will be a totaly (.java) file.


Just to make sure there's no misunderstanding - what Pat means by "Apache" is not the web server. He's talking about the various Apache Commons Java libraries.

If it is then can you please give me some more details like how can i get the help of Yahoo Map APIs in my pure core java(.java) file.


The Yahoo Map APIs are documented here.

If this were my problem, I'd probably use a library like jWebUnit to access the web page, and then use its API to extract the relevant page content.
[ April 21, 2008: Message edited by: Ulf Dittmer ]
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by chirag bhalla:

All i've to do will be a totaly (.java) file.



My suggested is to use a Java library, such as the one in Java Commons, there are many. Or you can write your own from the RFC. HTTP is really a simple protocol.
 
chirag bhalla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Pat Farrell:


My suggested is to use a Java library, such as the one in Java Commons, there are many. Or you can write your own from the RFC. HTTP is really a simple protocol.



I've started thinking in a little bit different way.

If i use JDIC(JDesktop Integration Components) to display the relevant web page in my application then I hope we will be able to to get the print of that web page from there.

What you guys have to say?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You try this and say.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it and see if you like it.

Just be warned, that when you see it work, you only know that it works on that day on that OS with that JVM. And sadly, sometimes its only on that version of that OS, etc.
 
chirag bhalla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Pat Farrell:
Try it and see if you like it.

Just be warned, that when you see it work, you only know that it works on that day on that OS with that JVM. And sadly, sometimes its only on that version of that OS, etc.



Thanks for your concern.

But let me give it a try.

I'll let you guys know about its efficiency.
 
chirag bhalla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Pat Farrell:
Try it and see if you like it.

Just be warned, that when you see it work, you only know that it works on that day on that OS with that JVM. And sadly, sometimes its only on that version of that OS, etc.



Hi,

I've been through it and its working just fine on Windows...

But till now i'm unable to get it done on Linux...

Can someone please let me know about the infrastructural settings like path settings and placement of jdic.jar and .so files on LINUX..

And please also let me know that if it can run smoothly on LINUX 7.3

Your posts will be highly appreciated.

Thanks.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!