Main Page‎ > ‎

ebook and epub tricks

Some must use tools and best practices

  • sigil as an editor (no word is not the way...)
  • calibre as a conversion tool
  • check your epub syntax with epubcheck until there is no error reported
  • you need to convert your epub to epub via calibre before publication (it made my epub accepted by amazon) ebook-convert file.epub file-publish.epub --no-default-epub-cover
apt-get install epubcheck calibre sigil fbreader pandoc tex4ht hevea
Some edition recommendations:
  • insert split between chapters, edit split markers to cut the book in chapters then rename each chapter files
  • insert a css stylesheet in your epub and include it in all your sections/chapters of your book with
<link href="../Styles/ebook.css" rel="stylesheet" type="text/css" />
  • remove any other local style definition (thousands of lines to remove in the epub source code...) by editing the code.
You can use the following style sheet as a starting point and customize it:

cat ebook.css

/* Style Sheet for ePub Books */

/* Set margins at 2% (This gives a white border around the book) */

body {margin-left:2%;

/* Text indent will make a paragraph indent, like putting a tab at the beginning of each new paragraph
The margin settings get rid of the white space between paragraphs, again so it looks more like a book
The text-align line justifies the margins. If you don't want them justified, change it to left, or remove that line
You don't have to specify a font, but you can */

p {text-indent: .3in;
    text-align: left;
    font-family:"Times New Roman";}

/* Here we make our headings centered
We've also made the headings the same font as the body text */

/* h1 { text-align: center;
    font-family:"Times New Roman"; }
h2 { text-align: center;
    font-family:"Times New Roman"; }
h3 { text-align: center;
    font-family:"Times New Roman"; }
blockquote {text-indent: 0,25 pouce; margin-left: 0,25 pouce; margin-right: 0,25 pouce; font-family:"Times New Roman";}

edit epub manually with vi

epub format is just a zip you can unzip it and rezip it using the following command

zip -rX ../my.epub mimetype META-INF/ OEBPS/

Now here are some vi search and replace commands that are interesting to get rid of word html ugly conversion:

:%s/a name=/a id=/g
:%s/"Image /"Image_/g
:%s/align="[^"]*" //g
:%s/<br clear="all"/<br/g
:%s/ class="[^"]*" / /g
:%s/ class="[^"]*">/>/g
:%s/<span style="font-family:Calibri">//g

For chapter *** separations:

:%s/^  <p>\*<\/p>\n\n  <p>\* \*<\/p>/  <p><br \/><\/p>\r\r  <p style="text-align: center;">* * *<\/p>\r\r  <p><br \/><\/p>/g
:%s/^  <p>\*<\/p>\n\n  <p>\*\*<\/p>/  <p><br \/><\/p>\r\r  <p style="text-align: center;">* * *<\/p>\r\r  <p><br \/><\/p>/g

Also remove all
  <meta content="" name="Titre" />
  <meta content="" name="Mots clés" />
  <meta content="Microsoft Word 14 (filtered)" name="Generator" />

To clean ugly word save as html you can also make use of these:

:%s/<a id="_Toc.*"><\/a>//g
:%s/<a id="_Toc.*">//g
:%s/<p style="[^"]*">/<p>/g
:%s/<i style="[^"]*">/<p>/g
:%s/<b style="[^"]*">/<p>/g

equations in ebooks

Name of the game here is to include equations not as a bitmap and be able to scale them without loss of quality. The only easy way I found is to use svg format using this script:

cat eq2svg

if [ "$3" = '' ]; then
cat > formule.tex << EOF
\begin{document} \pagestyle{empty}
\[ $1 \]
latex formule.tex
#dvips -q -f -e 0 -E -D 10000 -x 10000 -o formule.dvi
dvips -E -x $scale formule.dvi
mv formule.eps
#pstoedit -xscale $scale -yscale $scale -f plot-svg -dt -ssp $2
# it seems pstoedit adds a background which is easily removed:
#sed -i "/^.*background.*$/d" $2
dvisvgm -E formule.eps
mv formule.svg $2
rm formule.aux  formule.dvi  formule.log  formule.tex formule.eps

Using the following command:
./eq2svg "\lim_{n\to +\infty}\frac{b-a}{n}\sum_{k=0}^{n-1}f\left(a+k\frac{b-a}{n}\right)=\int_a^bf(x)dx" riemann.svg 2000
generates following riemann svg file:

figures in ebooks

Here again we can use this script to convert a postscript figure (e.g. printed from a word document) to a svg format:
cat ps2svg

# shrink to bounding box generating an eps from ps
ps2eps $1
# convert into svg
#dvisvgm -E ${1}.eps
inkscape -l ${fn}.svg ${fn}.eps

kindle mobi format conversion

cat epub2mobi


ebook-convert ${fn}.epub ${fn}-calibre.epub
ebook-convert ${fn}-calibre.epub ${fn}.mobi

find . -type f -name "*.epub" -print0 | while IFS= read -d $'\0' -r file
  echo processing $file
  mobi=`echo $file | sed "s/\.epub$/.mobi/g"`
  ebook-convert  "${file}" "${mobi}"

cbz/cbr to epub conversion

For kindle comic converter:

git clone
sudo apt-get install python3 python3-dev python3-pip python3-pyqt5 libpng-dev libjpeg-dev p7zip-full unrar
sudo pip3 install pillow python-slugify psutil scandir
sudo python3 ./ install

Now kcc can be executed.

LaTeX to epub conversion

In order to convert latex files to epub use and

hevea input.tex -o output.html
imagen -pdf input
ebook-convert input_file output_file --page-breaks-before / --toc-threshold 0 --level1-toc //h:h2 --level2-toc //h:h3 --level3-toc //h:h4

Google book publish error 017

I got an "Error n° 017 please contact us" when trying to publish the latest epub version on Google play book. After some help from Google support, they said that it is because I was using SVG of a size greater than 3200 in height  which is not supported. In order to make it pass I had to edit manually the OEBPS/Text/cover.xhtml and force the image height to 3000.


Marc de Courville,
Nov 30, 2015, 2:06 AM