Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[HowTo] Recursively convert e-books to PDF

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2044
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 151 times
Been thanked: 212 times

[HowTo] Recursively convert e-books to PDF

#1 Post by Hallvor »

What is this?
Below is a script that will convert e-books of various formats to PDF for the sake of compatibility. (I have tested this, and it works very well.)

Features:

Supported input formats: EPUB, MOBI, AZW, AZW3, IBA and OPF.
Simplicity. Just run the script and type the input directory; the script will do the rest.
It won't mess anything up: The script will not alter or delete any of your current files or directories. In case you want to convert a collection of EPUB-files to PDF, the PDF files will merely be added to the same folder as the EPUB-files.
Easy to deal with large collections: Since everything works automatically, it is ideal for large collections with many files and folders.

Prerequisites:

You need to install Calibre to make this work.

Let's install Calibre (as root):

Code: Select all

# apt install calibre
This will likely pull in quite a few packages.


Create a file called .ebookconverter.sh

Code: Select all

$ nano ~/.ebookconverter.sh

Copy and paste the following content into the file

Code: Select all

#!/bin/bash

# Recursive e-book to PDF converter. Supported input formats: EPUB, MOBI, AZW, AZW3, IBA, and OPF

# Check if Calibre is installed
command -v ebook-convert >/dev/null 2>&1 || {
    echo >&2 "Calibre is not installed. Please install Calibre (e.g., # apt install calibre) and try again."
    exit 1
}

# Prompt for input directory
read -p "Enter input directory: " input_directory

# Check if input directory exists
if [ ! -d "$input_directory" ]; then
    echo "Input directory not found: $input_directory"
    exit 1
fi

# Check if output file exists
check_output_file_exists() {
    output_file=$1

    if [ -f "$output_file" ]; then
        echo "Output file already exists: $output_file"
        return 0
    else
        return 1
    fi
}

# Check file formats and convert
convert_to_pdf() {
    ebook_path=$1

    # Check if e-book
    extension="${ebook_path##*.}"
    if [[ "$extension" =~ ^(epub|mobi|azw|azw3|iba|opf)$ ]]; then
        output_file="${ebook_path%.*}.pdf"

        # Check if output file exists
        if check_output_file_exists "$output_file"; then
            echo "Skipping conversion for existing file: $ebook_path"
            return
        fi

        echo "Converting: $ebook_path"

        # Find and convert e-book to PDF
        ebook-convert "$ebook_path" "$output_file" >/dev/null

        if [ $? -eq 0 ]; then
            echo "Successfully converted: $ebook_path"
        else
            echo "Failed to convert: $ebook_path"
        fi
    else
        echo "Skipping PDF or non-ebook file: $ebook_path"
    fi
}

recursive_convert() {
    path=$1

    for file in "$path"/*; do
        if [ -d "$file" ]; then
            recursive_convert "$file"
        elif [ -f "$file" ]; then
            convert_to_pdf "$file"
        fi
    done
}

recursive_convert "$input_directory"
Press Ctrl+x. Then confirm with y to save changes and exit.


Make the script executable

Code: Select all

$ chmod +x ~/.ebookconverter.sh

Run the script

Just open the terminal and use the following command to run the script:

Code: Select all

./.ebookconverter.sh
You will then be prompted to input a directory where your e-books are located, for instance ~/Documents. Press enter and grab a cup of coffee while the script automates the conversion process. (If the documents are already converted to PDF, they will be skipped. Any non-supported format will also be skipped.)
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

jimbo45
Posts: 7
Joined: 2022-09-11 22:16

Re: [HowTo] Recursively convert e-books to PDF

#2 Post by jimbo45 »

Possibly an easier one -- install calibre and then just import the books into the library and convert as many as you want in one go !!! (this assumes you have a GUI of course on your machine. If you don't have a GUI you can create a VM say (if I dare say it even Windows) and install calibre on that and then convert your e-books.

User avatar
fabien
Forum Helper
Forum Helper
Posts: 688
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 62 times
Been thanked: 161 times

Re: [HowTo] Recursively convert e-books to PDF

#3 Post by fabien »

I like recursive functions ^^
Hallvor wrote: 2023-06-18 21:05It won't mess anything up: The script will not alter or delete any of your current files or directories.
I would check that the new file won't overwrite a preexisting file of the same name (which will inevitably happen when the user relaunches the script on the same path because they have new e-books there, but we can imagine the worst case scenario where the preexisting pdf file is different from the newly created. I don't know the behaviour of ebook-convert though, maybe it doesn't overwrite files, but probably it does).
Optionally, before proceeding I would check that the directory is readable and writable.

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2044
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 151 times
Been thanked: 212 times

Re: [HowTo] Recursively convert e-books to PDF

#4 Post by Hallvor »

No, it just skips previously converted files.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
fabien
Forum Helper
Forum Helper
Posts: 688
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 62 times
Been thanked: 161 times

Re: [HowTo] Recursively convert e-books to PDF

#5 Post by fabien »

Hallvor wrote: 2023-06-20 06:31No, it just skips previously converted files.
You mean that it is ebook-convert behaviour? I (quickly) checked the manual and it is not stated so.

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2044
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 151 times
Been thanked: 212 times

Re: [HowTo] Recursively convert e-books to PDF

#6 Post by Hallvor »

Feel free to test it yourself.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
fabien
Forum Helper
Forum Helper
Posts: 688
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 62 times
Been thanked: 161 times

Re: [HowTo] Recursively convert e-books to PDF

#7 Post by fabien »

Hallvor wrote: 2023-06-20 11:22Feel free to test it yourself.
I didn't know I was in charge of testing, but I can assume that position :)

Code: Select all

$> ebookconverter 
Enter input directory: /tmp/myEbooks/
Converting: /tmp/myEbooks//book1.epub
Successfully converted: /tmp/myEbooks//book1.epub
$> ls -nl /tmp/myEbooks/book1.pdf
-rw-r--r-- 1 1000 1000 356579 Jun 20 15:27 /tmp/myEbooks/book1.pdf


$> gunzip -c /usr/share/doc/debian/FAQ/debian-faq.en.pdf.gz >/tmp/myEbooks/book1.pdf
$> ls -nl /tmp/myEbooks/book1.pdf; md5sum /tmp/myEbooks/book1.pdf
-rw-r--r-- 1 1000 1000 370337 Jun 20 15:28 /tmp/myEbooks/book1.pdf
f12f66e5f4ae23b0c2f2e7212dc379b9  /tmp/myEbooks/book1.pdf


$> ebookconverter 
Enter input directory: /tmp/myEbooks/
Converting: /tmp/myEbooks//book1.epub
Successfully converted: /tmp/myEbooks//book1.epub
Skipping non-ebook file: /tmp/myEbooks//book1.pdf
$> ls -nl /tmp/myEbooks/book1.pdf; md5sum /tmp/myEbooks/book1.pdf
-rw-r--r-- 1 1000 1000 356579 Jun 20 15:31 /tmp/myEbooks/book1.pdf
1428d4c2cf2228407d7108cdf83ac82a  /tmp/myEbooks/book1.pdf

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2044
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 151 times
Been thanked: 212 times

Re: [HowTo] Recursively convert e-books to PDF

#8 Post by Hallvor »

Much appreciated, fabien! :D

:oops: I must have deleted the epubs after first run: You were right.

This adds a check to see if the output file already exists. It ran fine on my system. I am altering the original post to prevent overwriting output files from previous runs.

Code: Select all

#!/bin/bash

# Recursive e-book to PDF converter. Supported input formats: EPUB, MOBI, AZW, AZW3, IBA, and OPF

# Check if Calibre is installed
command -v ebook-convert >/dev/null 2>&1 || {
    echo >&2 "Calibre is not installed. Please install Calibre (e.g., # apt install calibre) and try again."
    exit 1
}

# Prompt for input directory
read -p "Enter input directory: " input_directory

# Check if input directory exists
if [ ! -d "$input_directory" ]; then
    echo "Input directory not found: $input_directory"
    exit 1
fi

# Check if output file exists
check_output_file_exists() {
    output_file=$1

    if [ -f "$output_file" ]; then
        echo "Output file already exists: $output_file"
        return 0
    else
        return 1
    fi
}

# Check file formats and convert
convert_to_pdf() {
    ebook_path=$1

    # Check if e-book
    extension="${ebook_path##*.}"
    if [[ "$extension" =~ ^(epub|mobi|azw|azw3|iba|opf)$ ]]; then
        output_file="${ebook_path%.*}.pdf"

        # Check if output file exists
        if check_output_file_exists "$output_file"; then
            echo "Skipping conversion for existing file: $ebook_path"
            return
        fi

        echo "Converting: $ebook_path"

        # Find and convert e-book to PDF
        ebook-convert "$ebook_path" "$output_file" >/dev/null

        if [ $? -eq 0 ]; then
            echo "Successfully converted: $ebook_path"
        else
            echo "Failed to convert: $ebook_path"
        fi
    else
        echo "Skipping PDF or non-ebook file: $ebook_path"
    fi
}

recursive_convert() {
    path=$1

    for file in "$path"/*; do
        if [ -d "$file" ]; then
            recursive_convert "$file"
        elif [ -f "$file" ]; then
            convert_to_pdf "$file"
        fi
    done
}

recursive_convert "$input_directory"
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2044
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 151 times
Been thanked: 212 times

Re: [HowTo] Recursively convert e-books to PDF

#9 Post by Hallvor »

jimbo45 wrote: 2023-06-19 07:43 Possibly an easier one -- install calibre and then just import the books into the library and convert as many as you want in one go !!! (this assumes you have a GUI of course on your machine. If you don't have a GUI you can create a VM say (if I dare say it even Windows) and install calibre on that and then convert your e-books.
I get this error in Calibre when trying to add several folders:
You have selected more than one folder, and this window does not accept multiple folders. Select just one folder.
I think the script may be useful when dealing with huge libraries of e-books, or if you don't have a GUI. It is also not a problem to automate the task completely.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

User avatar
fabien
Forum Helper
Forum Helper
Posts: 688
Joined: 2019-12-03 12:51
Location: Anarres (Toulouse, France actually)
Has thanked: 62 times
Been thanked: 161 times

Re: [HowTo] Recursively convert e-books to PDF

#10 Post by fabien »

Hallvor wrote: 2023-06-20 14:44This adds a check to see if the output file already exists. It ran fine on my system.

Code: Select all

$> ebookconverter
Enter input directory: /tmp/myEbooks/
Converting: /tmp/myEbooks//book1.epub
Successfully converted: /tmp/myEbooks//book1.epub
$> ls -nl --time-style='+%T:%N' /tmp/myEbooks/book1.pdf; md5sum /tmp/myEbooks/book1.pdf
-rw-r--r-- 1 1000 1000 356578 22:13:11:698918508 /tmp/myEbooks/book1.pdf
9bbde56b6876ab89d8076becb93d4a81  /tmp/myEbooks/book1.pdf

$> ebookconverter
Enter input directory: /tmp/myEbooks/
Output file already exists: /tmp/myEbooks//book1.pdf
Skipping conversion for existing file: /tmp/myEbooks//book1.epub
Skipping PDF or non-ebook file: /tmp/myEbooks//book1.pdf
$> ls -nl --time-style='+%T:%N' /tmp/myEbooks/book1.pdf; md5sum /tmp/myEbooks/book1.pdf
-rw-r--r-- 1 1000 1000 356578 22:13:11:698918508 /tmp/myEbooks/book1.pdf
9bbde56b6876ab89d8076becb93d4a81  /tmp/myEbooks/book1.pdf
Test: Don't mess my files up: PASS
Signed: Chief Testing Officer :D

Thank You!

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2044
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 151 times
Been thanked: 212 times

Re: [HowTo] Recursively convert e-books to PDF

#11 Post by Hallvor »

Good job! Thanks! :D
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

Post Reply