# Use ConTeXt to turn EPUB into PDF

Because ConTeXt is good at manipulating XML, it would seem likely that it is also good at handling EPUB, since this is essentially a zipped collection of HTML and CSS files. I am wondering if there exists a good ConTeXt-based system for converting EPUBs to PDFs? ConTeXt Garden contains some information about EPUB, but this is restricted to ConTeXt-EPUB conversion.

Such a script would have the potential of producing better results than most of the other (IMO) mediocre EPUB to PDF conversion scripts currently available.

• it is quite easy to open ePUB file and parse HTML files using Lua, the hard thing is CSS support, as it can change the appearance a lot. it is better to use headless browser such as SlimerJS to convert DOM to LaTeX. Yes, no such tool exists at the moment :( – michal.h21 Jul 10 '15 at 21:14
• Can ConTeXt handle CSS? – Gaussler Jul 10 '15 at 21:15
• maybe yes, but I don't know about CSS parser for Lua and then you would have to reimplement DOM in Lua. This seems like huge task (basically create a new web browser in pure Lua) – michal.h21 Jul 10 '15 at 21:20
• I'm not so sure it does not have native support. I haven't tested the EPUB output yet, but I would assume it also outputs CSS. In other words, ConTeXt "knows" something about CSS. – Gaussler Jul 11 '15 at 6:28
• @michal.h21 It would not need support for JavaScript, which (I think) is a major obstacle when making a web browser. – Gaussler Jul 11 '15 at 6:29

ConTeXt is not up to the task of typesetting EPUB. However, there exist third-party solutions to convert an EPUB file to ConTeXt, e.g. pandoc. It is a command line tool written in Haskell which can translate between multiple markup file formats. To convert from EPUB to ConTeXt use

pandoc -f epub -t context -o output.tex input.epub


(add the -s flag if you want to obtain a default preamble as well).

If you are keen to give it a go, there are test file in the pandoc repository which you could run first: https://github.com/jgm/pandoc/tree/master/test/epub

Here is an excerpt of the first few lines (without the preamble). Doesn't look too bad.

\startalignment[middle]
{\tfd EPUBTEST 0101 - Styling Tests}
\smallskip
{\tfa Ori Idan\crlf Matt Garrish\crlf Marisa DeMeglio\crlf Toshiaki Koike\crlf Vincent Gros\crlf Markus Gylling}
\bigskip
\stopalignment

\section{EPUB 3 Styling Test Document: 0101}

\subsection{Status of this Document}

This publication is currently considered {[}UNDER DEVELOPMENT{]} by the
IDPF.

This publication is part of version X.X of the EPUB 3.0 Compliance Test
Suite released on TBD.

Before using this publication to evaluate reading systems, testers are
strongly encouraged to verify that they have the latest release by
checking the current release version and date of the test suite at
\useURL[url1][http://idpf.org/][][TBD]\from[url1]

This publication is one of several that currently comprise the EPUB 3
conformance test suite for reflowable content. The complete test suite
includes all of the following publications:

\startitemize[n,packed]
\item
.
\stopitemize

This document focuses on human-evaluated binary (pass/fail) tests in a
reflowable context. Tests for fixed-layout content and other individual
tests that require a dedicated epub file are available in additional
sibling documents; refer to the
\useURL[url2][https://github.com/mgylling/epub-testsuite/wiki/Overview][][test
suite wiki]\from[url2]
(\type{https://github.com/mgylling/epub-testsuite/wiki/Overview}) for

\subsection{Conventions}

The following conventions are used throughout the document:

\startdescription{1. Locating a test}

Tests for {\em required} Reading System functionality are preceded by
the label: {[}REQUIRED{]}

Tests for {\em optional} Reading System functionality are preceded by
the label: {[}OPTIONAL{]}
\stopdescription

\startdescription{2. Performing the test}
Each test includes a description of its purpose followed by the actual
{\bf test statement, which can always be evaluated to true or false}.
These statements typically have the form: "If {[}some condition{]},
the test passes".
\stopdescription

\startdescription{3. Scoring in the results form}
@@@TODO provide info on where to get the results form
\stopdescription


This is the compiled output of the formatting.epub example: