DEV Community

Cover image for Converting E-Books to any format with node-ebook-converter
João Victor Cardoso Kdouk
João Victor Cardoso Kdouk

Posted on

Converting E-Books to any format with node-ebook-converter

A long time ago, I was stuck in a project that required me to convert huge piles of EPUBs to PDFs using Node.js...

Well, things turned out to be way more complicated than I initially thought. I could not find any reliable library to do what I wanted, and all available packages were just simple wrappers of the Calibre library (this is not bad, it is just that the functionalities I needed, such as Threading, weren't built-in, giving me some headaches while trying to build one).

Throughout my numerous tries, I ran out of memory many times, had problems defining paths, found no support to promises and, as the package I was using was quite old, many flags and parameters were completely broken,..

To solve the problems, I decided to create a minimal, full-featured E-Book converter from the ground, with Queuing, Pooling, and Promises built natively in the package. Above everything, it has only one dependency (Calibre), adding close to zero complexity to your existing code!


Looks good so far... But will it really work? Let's give it a try! First, we need to install the Calibre Library (I know it is an external dependency, but it is the most complete and reliable conversion library out there! And better of all, it is lightweight). To install it, you can access Calibre Website. There, you will find CLI and GUI versions for each OS. If you are trying to run this on a server, you can follow this tutorial.

After installing Calibre, you can test it by opening terminal and typing:

ebook-convert --version
Enter fullscreen mode Exit fullscreen mode

This will either output a version stamp in your terminal or an error telling you that the command couldn't be found. In case you received an error, you can follow this Thread

If you received the version stamp, then you can add the library to your existing project and start tinkering with it! To do that, open your terminal in the root directory of your project and install the library using NPM:

npm i node-ebook-convert
Enter fullscreen mode Exit fullscreen mode

This will add the package to your project. Below, there is a simple example of how to use the library:

const ebookConverter =  require('node-ebook-converter');

/* Adds the conversion to the Execution Queue */
ebookConverter.convert({
  input: "./input/bear.pdf",
  output: "./output/bear.epub"
}).then(response => console.log(response))
  .catch(error => console.error(error));
Enter fullscreen mode Exit fullscreen mode

This will add the conversion to the idle queue and, if the current execution queue length is less than the pool size, than the conversion will be passed to the execution queue and the process of conversion will begin. To change the pool size, allowing you to convert more files at once, you can use the following method:

ebookConverter.setPoolSize(4); // This will allow 4 documents to process simultaneously
Enter fullscreen mode Exit fullscreen mode

Now, you can start converting your E-Books the way you want!

To access the documentation, go to the GitHub Official Page or if you want to know more about the package, you can access the NPM Page.

If you want to contribute, you can open an issue on GitHub, submit a Pull Request fixing something, or even Buy Me a Coffee if you enjoy this tutorial, or want this library to keep on growing!

See you in the next post!

Top comments (0)