-
Notifications
You must be signed in to change notification settings - Fork 359
Plugins: Latex
danfickle edited this page Jun 29, 2019
·
1 revision
- Uses fork of SnuggleTex library to convert basic Latex to XHTML, MathML and CSS. The SnuggleTex library is not undergoing active development.
- Sizing: Can be sized using CSS like any other element.
- Fonts: See note about fonts for MathML support, otherwise just uses fonts as defined for the elements it generates.
- Security: Trusted content only as it generates arbitrary XML and MathML.
- MathML support must be registered separately in the builder (see below).
- Supported Latex
- You will see warnings in log about unsupported CSS properties that SnuggleTex produces. These mostly don't change the appearance of rendered content.
- PR Link. Thanks @rototor.
- Testing: MathML produces different results on different JDKs so is not suitable for automatic visual regression testing. Latex without MathML seems to test OK.
- Below is a very simple sample that does not require math STIX fonts.
<dependency>
<!-- Latex support plugin - also pulls in MathML support plugin. -->
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-latex-support</artifactId>
<version>${openhtml.version}</version>
</dependency>
public static void main(String... args) throws Exception {
try (OutputStream os = new FileOutputStream("/Users/me/Documents/pdf-issues/output/latex.pdf")) {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withUri("file:///Users/me/Documents/pdf-issues/latex.html");
builder.useFastMode();
builder.toStream(os);
builder.addDOMMutator(LaTeXDOMMutator.INSTANCE); // Converts Latex to XHTML and MathML
builder.useMathMLDrawer(new MathMLDrawer()); // Renders MathML, not needed if you don't use math.
builder.run();
}
}
<html>
<head>
<style>
@font-face {
src: url(fonts/Simple-Font.ttf);
font-family: 'MyFont';
font-weight: normal;
}
@page {
size: 10cm 30cm;
margin: 0;
}
body {
margin: 0;
font-family: 'MyFont';
font-size: 15px;
}
latex {
display: block;
width: 100%;
margin: 2px;
padding: 5px;
page-break-inside: avoid;
}
math {
font-family: 'MyFont'; /* Probably want to use STIX pack instead. */
}
</style>
</head>
<body>
<latex><![CDATA[
Simple list:
\begin{itemize}
\item First item
\item Second item
\end{itemize}
]]>
</latex>
<latex><![CDATA[
Math:
$ \frac{x+1}{3y} $
$ x^{2^{y-z}} $
]]>
</latex>
</body>
</html>