All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.openhtmltopdf.latexsupport.DOMConverter Maven / Gradle / Ivy

Go to download

Open HTML to PDF is a CSS 2.1 renderer written in Java. This artifact supports rendering Latex markup (including math) on to PDFs using SnuggleTex.

There is a newer version: 1.0.10
Show newest version
package com.openhtmltopdf.latexsupport;

import java.io.IOException;

import org.w3c.dom.Element;

import uk.ac.ed.ph.snuggletex.SnuggleEngine;
import uk.ac.ed.ph.snuggletex.SnuggleInput;
import uk.ac.ed.ph.snuggletex.SnuggleSession;
import uk.ac.ed.ph.snuggletex.utilities.StylesheetManager;

class DOMConverter {
	private StylesheetManager stylesheetManager = new StylesheetManager();

	void convert(Element latexElement) throws IOException {
		String rawInputLaTeX = latexElement.getTextContent();
		String inputLaTeX = rawInputLaTeX.replaceAll("(\r\n|\r|\n)", "\n");

		SnuggleEngine engine = createSnuggleEngine();
		SnuggleSession session = engine.createSession();
		SnuggleInput input = new SnuggleInput(inputLaTeX, "LaTeX Element");

		try {
			session.parseInput(input);
		} catch (Exception e) {
			throw new IOException("Error while parsing: " + rawInputLaTeX + ": " + e.getMessage(), e);
		}

		while (latexElement.getChildNodes().getLength() != 0)
			latexElement.removeChild(latexElement.getFirstChild());

		try {
			session.buildDOMSubtree(latexElement);
		} catch (Exception e) {
			throw new IOException("Error while building DOM for: " + rawInputLaTeX + ": " + e.getMessage(), e);
		}
	}

	private StylesheetManager getStylesheetManager() {
		return stylesheetManager;
	}

	private SnuggleEngine createSnuggleEngine() {
		return new SnuggleEngine(getStylesheetManager());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy