Simple, typesafe PDF generation in Scala with Rapture LaTeX
| Welcome to Scala version 2.10.5 (OpenJDK 64-Bit Server VM, Java 1.6.0_27). | |
| Type in expressions to have them evaluated. | |
| Type :help for more information. | |
| // We're using returnResult mode, so that methods return `Result`s instead of throwing exceptions | |
| scala> import rapture.core._, modes.returnResult._ | |
| import rapture.core._ | |
| import modes.returnResult._ | |
| // Our LaTeX backend of choice is xelatex | |
| scala> import rapture.latex._, latexBackends.xelatex._ | |
| import rapture.latex._ | |
| import latexBackends.xelatex._ | |
| // This creates a basic LaTeX source | |
| scala> latex""" | |
| | \begin{document} | |
| | Hello World | |
| | \end{document} | |
| | """ | |
| res0: rapture.latex.Latex = | |
| Latex(\begin{document} | |
| Hello World | |
| \end{document}) | |
| // From this, we can attempt to generate a PDF. This fails, due to the errors listed. | |
| // Note that the errors are converted to `LatexException`s, and multiple errors are accumulated. | |
| scala> res0.generate() | |
| res1: rapture.core.Result[rapture.latex.PdfFile,rapture.latex.LatexException] = | |
| Errata( | |
| rapture.latex.LatexException: | |
| latex error at line 1: The font size command \normalsize is not defined: [_] | |
| latex error at line 3: The font size command \normalsize is not defined: [_] | |
| ) | |
| // Let's try correcting the problem. | |
| scala> latex""" | |
| | \documentclass{article} | |
| | \begin{document} | |
| | Hello World | |
| | \end{document} | |
| | """ | |
| res2: rapture.latex.Latex = | |
| Latex(\documentclass{article} | |
| \begin{document} | |
| Hello World | |
| \end{document}) | |
| // We now get a successful `Answer` response. | |
| scala> res2.generate() | |
| res3: rapture.core.Result[rapture.latex.PdfFile,rapture.latex.LatexException] = Answer(PdfFile(file:///tmp/tmp4432642532428880295.pdf)) | |
| // Import some more Rapture modules so we can save the file somewhere useful | |
| scala> import rapture.io._, rapture.fs._, rapture.uri._ | |
| import rapture.io._ | |
| import rapture.fs._ | |
| import rapture.uri._ | |
| // Unwrap the value from the `Answer` | |
| scala> res3.get | |
| res4: rapture.latex.PdfFile = PdfFile(file:///tmp/tmp4432642532428880295.pdf) | |
| // And move it to my home directory | |
| scala> res4.file.moveTo(uri"file:///home/jpretty/hello.pdf") | |
| res6: rapture.core.Result[rapture.io.Movable.Summary,Exception] = Answer(moved file) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment