How to Create RTL Books for Kindle

Written by AbiusX on . Posted in English, Hacks

Many of the books that I wanted to read on my Kindle(s), were in Arabic or Persian, and were also available freely online. These books were not of .mobi format, but were available as online books, in many html pages. I created a script that snatches these pages, stitches them together and forms a huge HTML file (one for each book).

The problem was that these books would not display/sync properly on my kindles. I have a Paperwhite, and a Fire, and wanted them both to fully support the generated book in RTL, and also support the same book (not two different versions of it), so that it synchs properly.

Unfortunately the solution that I came up with does not show the book under "Books" tab in Kindle Fire. Basically, to achieve that, we need two different versions of the book on the Paperwhite (.mobi) and the Fire (.azw3), and that would mean that they won't sync properly.

The most important part of the solution was to use the proper HTML template for the book. The following is the HTML template that needs to be used for the book. I was using "rtl" attribute towards the HTML tag instead of BODY tag:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>

<style type="text/css">
p {text-align: right; margin-right: .5em;}
</style>
</head>

<body dir="rtl">
</body>
</html>

You can also download this template here: Kindle RTL Template.

Put the entire Kindle book between the BODY tags in the above HTML template, save with UTF-8 encoding. The next step would be to convert this to .mobi format. My personal choice for that is the "kinglegen" app. It is a terminal app that you run, with the following syntax:

$ kindlegen mybook.html -verbose -o mybook.mobi

It will inform you of any issues or warnings, and generate a basic mobi file. You can also use Calibre, if you don't like CLI (terminal) applications, to convert the HTML into mobi, or use Amazon's send to device tools.

The next step is sending the book to devices. Using send to kindle, send the book to all your Fire devices, and to the Kindle library, but not to Paperwhite devices. Paperwhite devices will automatically see the book sent to the library, and can download it from there. This allows your Paperwhite copy to be synchronized with the Fire copy. (Both have the sync menu as well).

Finally, if syncing is not important for you and you want the book to appear under Books in Kindle Fire, convert it to AZW3 format with Calibre, and use Calibre to send it to device by connecting it via USB cable. You can have both mobi and azw3 formats that way.

Note: If syncing is not available on your fire when you download the book, delete it, close Kindle and Docs apps, rerun Docs, go to Send-To-Kindle tab, and click the book there. Once it downloads, it should sync properly.

Download a sample book that I generate for testing purposes: Realism - Motahari Kindle book

Welcome

I'm Abbas Naderi Afooshteh, also known as Abius and AbiusX. A software engineer and a renowned security expert, I'm currently OWASP chapter leader of Iran, owner of many OWASP projects, a member of ISSECO and CIO of Etebaran Informatics. For more details of what I do and what I can do, check my resume.