Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with Tags #178

Closed
fabioebner opened this issue Feb 23, 2018 · 1 comment
Closed

Problem with Tags #178

fabioebner opened this issue Feb 23, 2018 · 1 comment

Comments

@fabioebner
Copy link

In my case I have tags like "
" without "
" than I got the error:

ERROR: 'The element type "br" must be terminated by the matching end-tag "
".'

and " " so I got the error:

ERROR: 'The entity "nbsp" was referenced, but not declared.'

how can I fix this without change all HTML code? (it's from my user)

@rototor
Copy link
Contributor

rototor commented Feb 25, 2018

Sorry, you can't.... you must feed it into JTidy or using JSoup. The normal XML parser won't parse a br which is not terminated correctly (i.e. <br/>).

In my projects I use JTidy to cleanup the HTML and make it wellformed:

	public static String tidyHtml(String htmlString) {
		Tidy tidy = new Tidy();
		tidy.setDocType("omit");
		tidy.setXmlOut(true);
		tidy.setQuiet(true);
		tidy.setTidyMark(false);
		tidy.setWraplen(100_000);
		StringWriter swError = new StringWriter();
		tidy.setErrout(new PrintWriter(swError));
		StringWriter sw = new StringWriter();
		tidy.parse(new StringReader(htmlString), sw);
		String tidyHtml = sw.getBuffer().toString();
		tidyHtml = tidyHtml.replaceAll("(\r\n|\n|\r)+\\s*<", "<");
		return tidyHtml;
	}

using this JTidy dependency:

		<dependency>
			<groupId>net.sf.jtidy</groupId>
			<artifactId>jtidy</artifactId>
			<version>r938</version>
		</dependency>

The resulting HTML is then feed to OpenHTMLToPdf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants