This Bash shell script uses the pdf2htmlEX tool to convert a PDF file to an ePub file.
The result is a fixed layout ePub version 3: the layout is perfectly retained and all the fonts are embedded.
The pdf2htmlEX tool converts a PDF file into HTML5 (with CSS, JS, fonts, and bitmap and/or vector images).
Once you have an ePub file, only if you want, you can edit it with one of the available tools (Sigil, Calibre, Kotobee, etc.) to add interactive content, add reflowable text, etc.
If you want to publish an eBook on one of the available eBook stores (Google Play Books, Apple Books, Rakuten Kobo, etc.), you have to provide an ePub file and not a PDF file.
To convert myfile.pdf to myfile.epub, open the terminal and run the following command in the directory where the PDF file is located:
pdf2epubEX myfile.pdf
- Add the package repository (repository.dodeeric.be/apt):
sudo echo "deb [trusted=yes] https://repository.dodeeric.be/apt/ /" > /tmp/dodeeric.list
sudo mv /tmp/dodeeric.list /etc/apt/sources.list.d/
sudo apt update
- Install the package (pdf2epubex):
sudo apt install pdf2epubex
All the dependencies will be automatically installed.
To convert myfile.pdf to myfile.epub, open the terminal and run the following command in the directory where the PDF file is located:
docker run -ti --rm -v `pwd`:/temp dodeeric/pdf2epubex pdf2epubEX myfile.pdf
You can also replace `pwd`
by the absolute path of the directory where the PDF file is located (e.g.: /home/dodeeric/Documents/myfile.pdf):
docker run -ti --rm -v /home/dodeeric/Documents:/temp dodeeric/pdf2epubex pdf2epubEX myfile.pdf
You need to install Docker which is available for all computer operating systems: Linux, Windows and MacOS. See here.
To convert C:\Users\Eric\Documents\myfile.pdf to C:\Users\Eric\Documents\myfile.epub, open a terminal (PowerShell, Command Prompt or Windows terminal) and run the following command:
docker run -ti --rm -v C:\Users\Eric\Documents:/temp dodeeric/pdf2epubex pdf2epubEX myfile.pdf
or
docker run -ti --rm -v /c/Users/Eric/Documents:/temp dodeeric/pdf2epubex pdf2epubEX myfile.pdf
You need to install Docker which is available for all computer operating systems: Linux, Windows and MacOS. See here.
Remark: You will first have to install WSL1 (Windows Subsystem Linux), then update to WSL2.
You can also use pdf2htmlEX with this same Docker image:
To convert myfile.pdf to myfile.html, open the terminal and run the following command in the directory where the PDF file is located:
docker run -ti --rm -v `pwd`:/temp dodeeric/pdf2epubex pdf2htmlEX myfile.pdf
Remark: use the dodeeric/pdf2epubex:original Docker image to use the original version of pdf2htmlEX (coolwanglu).
Once you launch pdf2epubEX, some information will be displayed like the book/PDF width and height (in inches and cm), then some questions will be asked like:
- Format of the images in the epub (png, jpg or svg) [default: jpg]
- Resolution of the images in the epub in dpi (e.g.: 150 or 300) [default: 150]
- Title, author, publisher, year, language, ISBN number, subject
If you want, you can hit ENTER
to all the questions.
Image formats:
- if you chose png or jpg (bitmap formats), the vector images of the PDF will be converted in bitmap images (rasterized).
- if you chose svg (vector and bitmap format), the vector images of the PDF will remain in vector format, but: a) you cannot chose the resolution of the bitmap images (it is the one from the PDF); b) the bitmap images will be included in the svg files (Base64 coded); c) this format is not always correctly rendered by eBook readers; d) the generated epub file is not always passing the epub check.
A vector image can be as simple as a line, a rectangle, a table frame, a colored background, etc.
For eBooks with a lot of bitmap images, it is better to chose JPG (compression with loss) to not have a file too big. For eBooks with mainly vector images, it is better to chose PNG (lossless compression).
The ePub cover image will be made from the first page of the PDF file (png format).
In the examples below, the HTML version is one big file including everything (all the pages with HTML5, CSS, JS, fonts and images; fonts and images are coded in Base64, which can make the file quite big). pdf2htmlEX can also put all that content in different files (.html, .css, .js, .woff, .png, .jpg, .svg); that's in fact what basicaly the pdf2epubEX script does before wripping all the files in one ePub container file (.epub). Sometime, ePub is referred as "website in a box".
Legends:
- Number in parentheses: the size of the file in MB.
- Hashtag in parentheses: the ePub file does not pass the epub check validation using version epub 3.2 rules (commands not allowed in some svg files). This does not mean the ePub will not be displayed properly in most ePub readers.
- ePub written in bold: the recommended ePub version.
CEB 2015 - Solides et figures
(24 pages, only vector images in the PDF)
150 DPI | 300 DPI | ||
---|---|---|---|
PDF (0.3) | |||
SVG | ePub (1.0)(#) | ||
JPG | ePub (0.6) | ePub (1.1) | |
PNG | ePub (0.7) | ePub (1.5) | |
SVG | HTML (2.2) | ||
JPG | HTML (1.8) | HTML (5.7) | |
PNG | HTML (1.1) | HTML (2.5) |
Install your own OpenStack Cloud
(49 pages, bitmap and vector images in the PDF)
150 DPI | 300 DPI | ||
---|---|---|---|
PDF (1.0) | |||
SVG | ePub (1.4) | ||
JPG | ePub (1.5) | ePub (2.0) | |
PNG | ePub (1.6) | ePub (3.2) | |
SVG | HTML (2.9) | ||
JPG | HTML (5.3) | HTML (14.0) | |
PNG | HTML (3.0) | HTML (6.4) |
La dynastie belge en images
(248 pages, lot of bitmap images in the PDF)
150 DPI | 300 DPI | |
---|---|---|
PDF (56) | PDF (396) | |
SVG | ePub (78)(#) | ePub (504)(#) |
JPG | ePub (48) | ePub (150) |
PNG | ePub (209) | ePub (628) |
SVG | HTML (142) | HTML (895) |
JPG | HTML (69) | HTML (217) |
PNG | HTML (296) | HTML (869) |
Vector image quality in different formats (zoom of 500 %):
- SVG (vector format):
Vector |
---|
- PNG (bitmap format, lossless compression):
150 DPI | 300 DPI |
---|---|
- JPG (bitmap format, compression with loss):
150 DPI | 300 DPI |
---|---|
The script is based on the method described in my book published in 2014: Fixed Layout ePub: A Practical Guide to Publish eBooks from PDF Files. It is available on Amazon and on Googgle Play Books.
To read a fixed layout ePub, the best device is a tablet (Android or iOS/iPad). A smartphone is not adapted most of the time because of the too small screen size.
A lot of ePub reader apps exist (to read reflowable text ePub and fixed layout ePub) available on different platforms (Android, iOS, Windows, MacOS, or Linux): Google Play Books, BookShelf, PocketBook, Adobe Digital Editions, Apple Books (only on iOS; formely known as Apple iBooks), etc.
Amazon Kindle does not support the standard ePub format (they have their own format which is based on the ePub format).
To use Google Play Books, you have to go to Settings, then set Enable uploading. The uploaded eBooks (PDF or ePub) will be available on all devices using the same Google account. You can also upload eBooks from the Google Play Books web interface (see the Upload files button on the top right corner). Please note that: a) the ePub file has to pass a pre-check to be able to be hosted in the Google cloud; b) if you upload a PDF, all pages (text + images) will be converted into images (the text and vector images are rasterized, and no hidden text layer will be added: it means no text search or copy/paste is possible). Please also note that there is a Web browser reader available.
More about fixed layout (FXL) ePub version 3 specifications (IDPF / W3C): Fixed Layouts (EPUB Content Documents 3.2) and Fixed-Layout Properties (EPUB Packages 3.2).
This script is converting a PDF to a fixed layout ePub. pdf2htmlEX is THE tool to maintain the original layout. Hence, it is not the best tool to extract the text and the images from a PDF.
Anyway, the script will ask if you want a reflowable text ePub or a fixed layout ePub if you install the Calibre software (apt install calibre
) or if you use the image dodeeric/pdf2epubex:calibre (image much bigger than dodeeric/pdf2epubex).
The script will use the ebook-convert command from Calibre to convert the PDF to a reflowable text ePub.
Caution: converting automatically a PDF file to a reflowable text ePub file cannot be perfect. We suggest to edit the ePub file manually with Calibre or Sigil.
Repositories for pdf2htmlEX: the original one (coolwanglu) and the new one (with updated .deb packages).
This script is based on the Bash scripts written by Robert Clayton (RNCTX) and available in his Git repository.