Illustrated ebooks with epub: using the svg image element

All things being equal, the epub format is preferable to pdf for reading on devices like the iPad, Nook, Sony, Kobo, and other dedicated e-readers.

But for some types of books (such as manga, comics, or graphic novels), epub doesn’t seem to be able to handle large images that should fill the screen.

Apple has gone so far as to create its own fixed layout format for such ebooks.

It is possible, though, to stick with epub and get perfect results for illustrated ebooks, using the forgotten (or perhaps simply overlooked) svg image element.

Here’s a peek under the hood of how we do it at eBookBurn.com, as part of our new illustrated ebook publishing feature.

This is an example of the markup to use in your epub’s xhtml files for each image:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 592 900" preserveAspectRatio="xMidYMid meet">
<image width="592" height="900" xlink:href="images/page01.jpeg" />
</svg>

What this markup does is take a JPEG image sized 592 pixels wide by 900 pixels tall, and frame it in the center of a 592×900 svg element.

It turns out that 592×900 is the right size and aspect ratio for “standard-sized” six inch e-ink screens found on the regular Nook, and Sony Reader.

So why use svg at all?

Wouldn’t it be simpler to define it with the plain img tag (as this epub template does), like this?

<img src="images/page01.jpeg" width="592" height="900" alt="Page 1"/>

Unfortunately, many devices, such as the iPad and the Nook Color, have screens larger than six inches.

So on those devices, using the plain img tag in your epub’s xhtml files leaves an embarrassing whitespace gap, from where the image stops to where the actual screen ends.

The svg element shown earlier, though, is different: it stretches the image to fill the entire screen, while preserving the aspect ratio.

It’s also important to note that 592 width and 900 height specified within the svg and image elements should not be thought of as pixel sizes, since no units are defined, but as a width-to-height ratio.

So any image with the same aspect ratio as 592×900 will work well, regardless of its actual size. Scaling up to larger screens, though, also means the dpi count should be reasonably high, at least 72 dpi (and more for images whose base size is smaller than 592×900 pixels).

For most devices, that’s enough, but some e-readers insist on adding margins and other padding to each page, so it’s helpful to define this in the xhtml file’s head block:

<style type="text/css">
@page { margin: 0.000000pt; padding: 0.000000pt; }
</style>

And these css classes in the stylesheet:

.svg_outer {
display: block;
margin-bottom: 0;
margin-left: 0;
margin-right: 0;
margin-top: 0;
padding-bottom: 0;
padding-left: 0;
padding-right: 0;
padding-top: 0;
text-align: left;
}

.svg_inner {
display: block;
text-align: center;
}

So the final xhtml for each page image looks like this:

<div class="svg_outer">
<div class="svg_inner">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 592 900" preserveAspectRatio="xMidYMid meet">
<image width="592" height="900" xlink:href="images/page01.jpeg" />
</svg>
</div>
</div>

Just repeat that pattern for every image in book, for every chapter that contains full-page illustrations.


Update, May 1, 2012: Moriah Jovan has been kind enough to provide sample epub files created using this technique:


Tags: , , , , , , , , , , , ,

Share on Facebook Tweet This Share on LinkedIn Share on Google+ Share on reddit Share on Pinterest

11 Responses to “Illustrated ebooks with epub: using the svg image element”

  1. Illustrated ebooks with epub: using the svg image element, by Denis Papathanasiou | Ebooks on Crack Says:

    [...] Via Denis Papathanasiou’s blog [...]

  2. Some recent information culled from various sites « Larry Bonura's Blog Says:

    [...] Denis Papathanasiou writes in his blog about using the SVG image element in illustrated eBooks with EPUB. He shows how he marks up EPUB’s XHTML files at eBookBurn.com. [...]

  3. saskir Says:

    So if I understand correectly you need to have a picture with teh right size that it works correctly. But what if you don’t have one (charts, cover, etc). Should I then leave the attribute with the aspect ratio and change the width and height?

  4. Denis Says:

    The critical thing is aspect ratio, not size. If you have a 6×9 image, the method described here will still work.

    If your images are in a different aspect ratio, and scaling to a different width or height to get them into the correct aspect ratio won’t work, the next best thing to do is create a blank image sized 592×900 and paste your image inside it.

    That is, of course, if you want the image to fill the page; if it’s to be surrounded by text, then you should use the regular img tag and avoid this method.

  5. Moriah Jovan Says:

    THANK YOU THANK YOU THANK YOU!!!

  6. Ryan Taube Says:

    I was wondering if you have any examples of an illustrated ebook in EPUB format using the SVG, that you could share so we can dissect?

  7. Bob Barry Says:

    This sounds good, but I found a compatibility chart at
    https://developer.mozilla.org/en/SVG/Element/image
    which seems to indicate that the image element is only
    available on Android versions 3.0 and above. Have you
    encountered any compatibility problems with SVG image?

    I would also (like Ryan) like to see example epub ebook
    using this technique.

    Thanks

  8. Denis Says:

    You are confusing HTML elements allowed by web browsers (which is what that mozilla link documents) and XHTML elements allowed in the epub standard.

    This method produces valid epub files that look good in iPad and Nook (and Kindle, when converted to mobi format).

    If you’d like to see samples, try it for yourself at eBookBurn.com

  9. Moriah Jovan Says:

    @Ryan Taube

    I do. I’ve done plenty of them with this. Denis, if you would like me to share, I will. Please contact me.

  10. Ilaria Says:

    Hi,
    I know this conversation might be dead already, but I need some help: I’m using the svg method here described, but I in this comic I’m making, I have double spread pages and they look funny when I apply the svg code to them. What should I change in the svg code to make them look ok? They appear to be cut on the right side of the image.

    thanks

  11. David Says:

    @Ilaria With comics or books with images going across a spread you should use fixed format epub3 … there is no way to cut it in a sensible way – it will be wrongly formatted or cut into to seperate pages … ruining the spread with svg …

    /David

Leave a Reply