Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

1. Introduction

Converting DjVu documents to the PDF format can be a common task when handling digital documents in Linux, especially eBooks.

In this tutorial, we’ll use command-line tools like DjVuLibre, and ImageMagick to perform the conversion. These tools offer a simple and efficient way to convert DjVu files to PDF.

Before we dive into the steps, let’s quickly review DjVu and PDF formats.

2. Understanding DjVu and PDF File Formats

The DjVu format optimizes the compression of scanned documents, such as books, magazines, and image-heavy texts. It reduces the file size while maintaining quality, especially for high-contrast images and text. In essence, this makes DjVu ideal for storing large collections of scanned materials where storage efficiency is important.

On the other hand, PDF is one of the most widely used formats for sharing, viewing, and printing documents. Furthermore, PDFs preserve the layout and appearance across devices and operating systems, usually making them the preferred choice for everyday use.

PDF also supports features like annotations, digital signatures, and text search, which aren’t commonly available in DjVu.

3. Using DjVuLibre

DjVuLibre is a collection of tools and libraries for working with DjVu files. It’s widely used for viewing, creating, and converting DjVu documents.

3.1. Installing DjVuLibre

To use DjVuLibre, we first install it:

$ sudo apt install djvulibre-bin

In this case, we use the native APT package manager.

After installation, DjVuLibre should be ready for use.

3.2. Converting a Single DjVu File to PDF

To convert a single DjVu file to a PDF, we use the ddjvu tool included in DjVuLibre:

$ ddjvu -format=pdf input.djvu output.pdf

After executing the command, the conversion process doesn’t display any progress or output on the terminal.

However, we can verify the conversion by using the ls command to check whether the output file exists:

$ ls
input.djvu output.pdf

In this case, we can see the original DjVu file and the newly converted PDF file in the directory.

By default, DjVuLibre saves the converted PDF in the same directory as the input DjVu file. If we’d like to save the output in a different directory, we can specify the desired output path:

$ ddjvu -format=pdf input.djvu /path/to/output/folder/output.pdf

Saving PDF files in a separate directory is an effective way to keep them organized.

3.3. Converting Multiple DjVu Files to PDF

We can perform a batch conversion of multiple DjVu files using a shell loop:

$ for file in *.djvu; do ddjvu -format=pdf "$file" "${file%.djvu}.pdf"; done

In this command, for file in *.djvu; scans all .djvu files in the current directory. Meanwhile, ddjvu -format=pdf “$file” “${file%.djvu}.pdf”; converts each .djvu file to a PDF, saving the output with the same name but a .pdf extension.

The loop continues until all DjVu files are converted.

3.4. Adjusting Conversion Settings

We can use DjVuLibre to customize the conversion process. For example, we can adjust the PDF quality using the -quality option, which accepts values between 25 and 150, representing the dots per inch (DPI)  metric of the output. Higher values produce better image quality, while lower values produce smaller file sizes.

To generate a high-quality PDF, we can set -quality value to 150:

$ ddjvu -format=pdf -quality=150 input.djvu output.pdf

We can also include specific pages in the conversion by using the -page option:

$ ddjvu -format=pdf -page=1-5 input.djvu output.pdf

This command converts only pages 1 through 5 of the DjVu file into a new PDF.

We can also provide quality settings while converting multiple files to apply the same settings to all files:

$ for file in *.djvu; do ddjvu -format=pdf -quality=100 "$file" "${file%.djvu}.pdf"; done

By adjusting the -quality value, we can balance the output PDF’s file size and image clarity according to our needs.

3.5. Automating Conversion

If we frequently need to convert multiple DjVu files, automating the conversion process using a shell script can save time and effort:

$ cat djvu_file_conversion.sh
 #!/bin/bash
for file in *.djvu; do
  ddjvu -format=pdf -quality=100 "$file" "${file%.djvu}.pdf"
done
echo "Conversion complete"

After processing all files, the script displays the message: Conversion complete.

This is an effective approach for handling large numbers of files quickly without manually entering commands each time.

4. Using ImageMagick

ImageMagick is a versatile, open-source software suitable for imaging manipulation. It supports various file formats and offers robust tools for converting and editing images and documents, including DjVu files.

4.1. Installing ImageMagick

To get started with ImageMagick, we first install it:

$ sudo apt install imagemagick

Again, we use APT.

After installation, ImageMagick is ready for conversion tasks.

4.2. Converting a Single DjVu File to PDF

We can use the convert utility, a core component of ImageMagick to convert a single DjVu file to PDF:

$ convert input.djvu output.pdf

To execute this command properly, we ensure that ImageMagick’s security policy allows reading and writing PDFs.

If the command fails, we update the policy.xml file to grant the required permission:

<policy domain="coder" rights="read|write" pattern="PDF" />

After making the changes, we save the file and retry the command to complete the conversion.

4.3. Converting Multiple DjVu Files to PDF

To handle multiple DjVu files at once, ImageMagick enables batch processing through a loop:

$ for file in *.djvu; do convert "$file" "${file%.djvu}.pdf"; done

Similar to the earlier example with ddjvu, this script scans all DjVu files in the current directory and passes them to the convert command to create PDFs with the same names until all files are converted.

4.4. Adjusting Conversion Settings

ImageMagick provides various options to fine-tune the output during conversion.

For instance, we can modify the resolution of the output using the -density option:

$ convert -density 120 input.djvu output.pdf

This sets the resolution to 120 dots per inch (DPI).

Similarly, to extract certain pages, we specify the page range:

$ convert input.djvu[0-3] output.pdf

Here, pages 1 through 4 (index starts at 0) are converted to PDF.

To control the size of PDF files, we can use a combination of -density and -quality options:

$ convert -density 120 -quality 90 input.djvu output.pdf

The -quality option controls the compression quality. Its value ranges from 1 to 100, where a higher value produces better quality with less compression, resulting in a larger file size.

4.5. Automating Conversion Tasks

Similarly, we can automate the process using a shell script:

$ cat djvu_to_pdf.sh
#!/bin/bash
for file in *.djvu; do
  convert "$file" "${file%.djvu}.pdf"
done
echo "All conversions are complete"

As usual, after converting all DjVu files to PDF, the script displays a final message indicating the process is complete.

5. Conclusion

In this article, we’ve covered two powerful tools (DjVuLibre and ImageMagick) for converting DjVu files to PDF using the terminal.

DjVuLibre excels in handling DjVu-specific tasks, offering quality and page range adjustments. While ImageMagick is known for its flexibility and advanced image manipulation options.

By understanding their features and use cases, we can select the tools that best suit our needs.