EPBU/MOBI转PDF

--痛苦

--不爱BB 直接上码。 

 

写了一个java方法,转epub 或者mobi 为 pdf的方法 (单个转换)

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class EbookConverter {

    public static void main(String[] args) {
        try {
            // 桌面路径
            String userHome = System.getProperty("user.home");
            String desktopPath = userHome + File.separator + "Desktop";

            // 输入文件名(包括扩展名)
            String inputFileName = "example.epub"; // 修改为实际文件名

            // 输入文件路径
            String inputFilePath = desktopPath + File.separator + inputFileName;

            // 输出文件路径(更改扩展名为 .pdf)
            String outputFileName = inputFileName.substring(0, inputFileName.lastIndexOf('.')) + ".pdf";
            String outputFilePath = desktopPath + File.separator + outputFileName;

            // 转换文件
            convertEbookToPdf(inputFilePath, outputFilePath);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void convertEbookToPdf(String inputFilePath, String outputFilePath) throws IOException, InterruptedException {
        // 判断文件格式
        String fileExtension = getFileExtension(inputFilePath).toLowerCase();

        if (!fileExtension.equals("epub") && !fileExtension.equals("mobi")) {
            throw new IllegalArgumentException("Unsupported file format. Only EPUB and MOBI are supported.");
        }

        // 使用 Calibre 的 ebook-convert 工具
        String command = String.format("ebook-convert \"%s\" \"%s\"", inputFilePath, outputFilePath);

        // 启动进程
        Process process = Runtime.getRuntime().exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        // 记录开始时间
        long startTime = System.currentTimeMillis();

        // 读取输出并显示进度
        String line;
        while ((line = reader.readLine()) != null || (line = errorReader.readLine()) != null) {
            // 这里我们假设输出包含进度信息,可以根据具体的输出格式进行调整
            if (line.contains("%")) {
                System.out.println(line); // 打印带有百分比的行
            }
        }

        int exitCode = process.waitFor();

        // 记录结束时间
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;

        if (exitCode != 0) {
            throw new IOException("Error during conversion process. Exit code: " + exitCode);
        }

        System.out.println("Conversion successful: " + outputFilePath);
        System.out.println("Time taken: " + (duration / 1000) + " seconds");
    }

    private static String getFileExtension(String fileName) {
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
            return fileName.substring(dotIndex + 1);
        }
        return "";
    }
}

 

代码说明

  1. 启动进程:使用Runtime.getRuntime().exec(command)启动ebook-convert命令,并创建BufferedReader对象读取进程的标准输出和错误输出。
  2. 记录时间:在转换开始时记录开始时间,转换完成后记录结束时间,并计算耗时。
  3. 读取输出:在while循环中读取进程输出,并假设输出包含进度信息(例如百分比)。实际情况下,ebook-convert的输出格式需要根据具体情况调整解析方式。
  4. 显示进度:将包含百分比信息的行打印到控制台,模拟显示进度。
  5. 显示耗时:转换完成后,打印耗时信息。

注意事项

  • 进度信息ebook-convert工具的标准输出可能不包含详细的进度信息,需根据实际输出格式进行解析和调整。
  • 错误处理:在实际应用中,应增加更多的错误处理逻辑,例如捕获和处理可能的异常情况。
  • 优化:如果需要更详细和准确的进度更新,可以考虑使用支持更丰富反馈信息的转换工具或库。

 写了一个java方法,转epub 或者mobi 为 pdf的方法 (批量转换)

 以下是一个Java程序,用于批量将指定目录下的所有EPUB或MOBI文件转换为PDF文件,并将转换后的PDF文件保存到同一目录中,保持相同的文件名但后缀为PDF。

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class BatchEbookConverter {

    public static void main(String[] args) {
        // 指定要处理的目录路径
        String directoryPath = "C:\\Users\\Administrator\\Downloads\\pdf"; // 修改为实际目录路径

        File directory = new File(directoryPath);

        System.out.println("目录路径"+directoryPath);
        if (!directory.isDirectory()) {
            System.err.println("The provided path is not a directory.");
            System.exit(1);
        }

        File[] files = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(".epub") || name.toLowerCase().endsWith(".mobi"));

        if (files == null || files.length == 0) {
            System.out.println("No EPUB or MOBI files found in the directory.");
            return;
        }

        for (File file : files) {
            String inputFilePath = file.getAbsolutePath();
            String outputFilePath = inputFilePath.substring(0, inputFilePath.lastIndexOf('.')) + ".pdf";

            try {
                convertEbookToPdf(inputFilePath, outputFilePath);
            } catch (IOException | InterruptedException e) {
                System.err.println("Failed to convert file: " + inputFilePath);
                e.printStackTrace();
            }
        }
    }

    public static void convertEbookToPdf(String inputFilePath, String outputFilePath) throws IOException, InterruptedException {
        // 判断文件格式
        String fileExtension = getFileExtension(inputFilePath).toLowerCase();

        if (!fileExtension.equals("epub") && !fileExtension.equals("mobi")) {
            throw new IllegalArgumentException("Unsupported file format. Only EPUB and MOBI are supported.");
        }

        // 使用 Calibre 的 ebook-convert 工具
        String command = String.format("ebook-convert \"%s\" \"%s\"", inputFilePath, outputFilePath);

        // 启动进程
        Process process = Runtime.getRuntime().exec(command);
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

        // 记录开始时间
        long startTime = System.currentTimeMillis();

        // 读取输出并显示进度
        String line;
        while ((line = reader.readLine()) != null || (line = errorReader.readLine()) != null) {
            // 这里我们假设输出包含进度信息,可以根据具体的输出格式进行调整
            if (line.contains("%")) {
                System.out.println(line); // 打印带有百分比的行
            }
        }

        int exitCode = process.waitFor();

        // 记录结束时间
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;

        if (exitCode != 0) {
            throw new IOException("Error during conversion process. Exit code: " + exitCode);
        }

        System.out.println("Conversion successful: " + outputFilePath);
        System.out.println("Time taken: " + (duration / 1000) + " seconds");
    }

    private static String getFileExtension(String fileName) {
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
            return fileName.substring(dotIndex + 1);
        }
        return "";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arenaschi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值