Easiest way to render HTML from String in Java - java

I need to render HTML page from a Java String. The String contains a full HTML page which may include CSS and Javascript. I know that some CSS/script reference may missing because I only has the html, so it's not a problem.
I have tried using JEditorPane, but it only works for very simple HTML. If it can't render, it will display nothing.
Is there any library I can use?

You can use the Desktop class. It will open the default browser of your platform.
Read the section from the Swing tutorial on How to Integrate With the Desktop Class for more information and working examples.

Related

Whether using HTML codes in the Java will only work on internet system ?

I am developing desktop application. In that I use some HTML code for line break (wrapping text) and change tab size in JTabbedPane.
If I use the application in a system without internet, will the HTML code will work or not?
Using HTML tags in your application doesn't make it dependent on a working Internet connection. However make sure your application is not using some HTML DTD or schema spec for strict HTML validation.
I guess you could have found it yourself by trying.

editing html/webpages in java

I want to create a java application that allows users to import a web page and be able to edit it within the program.
Importing a web page will render it and the components of the page (images, text, etc) will be editable or draggable, allowing the user to re-layout the components.
For example, the user can load a web page that has an image header, but decides to have it at the bottom of the page. They can simply click and drag the image down and the html will be reformatted appropriately. It is basically a WYSIWYG html editor...
Will this be a difficult task? I am stuck at how to parse the web page into draggable components and being able to export the html after all the edits.
From what I can see, I'd need wrapper classes for html components and a way to keep track of all the changing positions and objects, but I could be wrong. Are there any helpful tools for this task?
I think creating something from sctratch will be difficult. But you can use Eclipse Web Tools Platform (either just by installing it or by developing your own plugin/distribution based on that). It has a fairly good HTML Editor/Web Page Editor which has WYSIWYG features.
EDIT: Also this question has some tips: Java WYSIWYG HTML editor

how to convert a HTML web page into a PDF file using Java

i've been searching on the internet on how to convert a HTML page into a PDF file using Java. i found a lot of pointers, and in short, they don't work or are too difficult to implement. i also downloaded a commercial product, pdf4ml; the API is something i'd be happy to work with, except that when i crawled a simple page on wikipedia, i get a out of memory error (setting Xmx to 1024 M). in some approaches, they suggest converting HTML -> XHTML -> FO -> PDF. however, i am getting a lot of exceptions for the XHTML-to-FO XLS file; and reading the documentations, it's not something that i have enough time to understand right now.
here are my questions/concerns.
1. is there another cohesive API out there that will easily convert HTML to PDF (commercial or not)?
2. is there a way i can simply capture a HTML page and store it as a single file. this approach would be similar to using internet explorer's way of saving a web page as a web archive (single file, MHT format)?
any help is appreciated. (btw, i know this question has been asked repeatedly, but in addition to the original spirit of the question, i'm opened to other ways). thanks.
Try wkhtmltopdf, which is using WebKit. Another option (I'm using that currently) is using OpenOffice (remote controlled via macros).
you may use iText open source Java lib for that, and read this
or use YaHPConverter open source Java lib.
or do this whith help of icepdf popular open source lib
or use pd4ml, but it not free, only trial.
or use this, and this is man for it.
My 2 cents using opensource tools:
You can use either Capture screenshots with Selenium or WebDriver to save html page's screenshot in an image file from your Java code. And once you have image file you can convert it to pdf again from your Java code.
EDIT:
It seems you can do all that in 1 step using itext Html to Pdf
I am not sure but you could Try
1) cobra html rendering engine http://lobobrowser.org/cobra.jsp
2) htmleditorkit -- part of jdk
3) JWebPane
Use the rendering kit to parse and render html. The rendered out put is a swing component. Swing component can be used by itext to generate pdf file out put
You can try out Pdfcrowd. It is an easy to use commercial online API with many options and with support for Java.
It can create PDF either from web pages or raw HTML code.

How can I get html content from a browser that can do the html correction and js scripting?

I need a solution for getting HTML content from a browser. As rendering in a browser, js will be ran, and if not, js won't be ran. So any html libraries like lxml, beautifulsoup and others are all not gonna work.
I've searched a project named pywebkitgtk, but it's purpose is to create a browser with a front end.
Is there any way to put a url into a "fake browser" and render it and run its all javascript and save it into a html file? I don't need any front-end, just back-end is ok.
I need to use Python or java to do that.
selenium-rc lets you drive an actual browser for your purpose, under control of any of several languages at your choice, which include both Python and Java. Check it out!
For a detailed example of use with Python, see here.

Viewing HTML inside Applet without using JEditorPane

I have a small (500kb) swing applet that displays very simple/limited set of small HTML page(s) inside it with JEditorPane, however this does not seem to work 100% fluently, some customers get a blank page displayed without any java exceptions. The page works OK from my machine. I need a more reliable way to show HTML page to all our users.
Any ideas if there is a small + free class to use instead of JEditorPane
OR is there an easy fix to make it more reliable (non blank)
private JEditorPane m_editorPane = new JTextPane();
m_editorPane.setEditable( false);
m_editorPane.setBackground(new Color(239 ,255, 215));
m_editorPane.setBounds(30,42,520,478 );
m_editorPane.setDoubleBuffered(true);
m_editorPane.setBorder(null);
m_editorPane.registerEditorKitForContentType("text/html", "com.xxxxx.SynchronousHTMLEditorKit");
m_editorPane.setPage(ResourceLoader.getURLforDataFile(param.trim()));
Although I haven't used it before, Lobo is an open source web browser for Java with support for HTML 4, Javascript and CSS 2.
Compared to the JEditorPane which only has support of HTML 3.2, it seems like Lobo may be a better bet for loading modern web pages.
AFAIK, JEditorPane is a very primitive HTML component: it is confused by CSS and knows nothing about JS.
I doubt you will find a "small + free" class doing better, HTML parsing and displaying isn't a simple business, even less today.
Perhaps it is better to let the big names in the business to take care of this task, ie. using Internet Explorer or Mozilla components (depending on what is available, etc.): JDIC: Embedding a Web browser in Java.
I've recently POC-ed several java HTML rendering solutions. We decided on JEditorPane because we really need to minimize the size of our jar and it's built into Swing. However, the best library I came across was Flying Saucer. It doesn't have any js support but it's rendering quality and api is top notch, and it's "free" (LGLP), 100% Java, and only about 1mb (still too big for us, but small compared to other options). However, it only renders strict XHTML (all attribute values have to be quoted, all tags properly formed), but that may be OK depending on your needs (and HtmlCleaner or some other such utility may help towards that end).

Categories