当前位置: 首页 > news >正文

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 "";}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • fastadmin二次开发 修改默认的前端弹出样式
  • JVM 常见配置参数
  • 汇聚荣科技有限公司怎么样?
  • 人工智能应用层岗位—AI项目经理/AI产品经理
  • 【MySQL】MySQL的安装和基本概念
  • 亚马逊云科技专家分享 | OPENAIGC开发者大赛能量加油站6月5日场预约开启~
  • 文化设计“All in AI”,第二十届文博会中芬设计园分会场盛大开幕
  • 顺序表实现通讯录项目
  • HackTheBox-Machines--Cronos
  • vue使用EventBus进行跨组件通信
  • Android刮刮卡自定义控件
  • Oracle按照主键排序分页sql
  • 2.Redis之Redis的背景知识
  • 可选链与空值合并运算符的妙用
  • C++进阶之路:何为运算符重载、赋值运算符重载与前后置++重载(类与对象_中篇)
  • 【译】JS基础算法脚本:字符串结尾
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • 2017前端实习生面试总结
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • eclipse(luna)创建web工程
  • IDEA常用插件整理
  • vagrant 添加本地 box 安装 laravel homestead
  • 缓存与缓冲
  • 简单基于spring的redis配置(单机和集群模式)
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 首页查询功能的一次实现过程
  • 问:在指定的JSON数据中(最外层是数组)根据指定条件拿到匹配到的结果
  • 物联网链路协议
  • 硬币翻转问题,区间操作
  • 新海诚画集[秒速5センチメートル:樱花抄·春]
  • ​​​【收录 Hello 算法】10.4 哈希优化策略
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • #{}和${}的区别是什么 -- java面试
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (+4)2.2UML建模图
  • (1综述)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
  • (2024最新)CentOS 7上在线安装MySQL 5.7|喂饭级教程
  • (C语言)fread与fwrite详解
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (分享)自己整理的一些简单awk实用语句
  • (附源码)ssm高校志愿者服务系统 毕业设计 011648
  • (算法设计与分析)第一章算法概述-习题
  • (自适应手机端)行业协会机构网站模板
  • .aanva
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .Net Core 中间件验签
  • .Net Core缓存组件(MemoryCache)源码解析
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .Net7 环境安装配置
  • .NET上SQLite的连接
  • .net下简单快捷的数值高低位切换
  • :O)修改linux硬件时间
  • @JsonFormat与@DateTimeFormat注解的使用
  • [2016.7 Day.4] T1 游戏 [正解:二分图 偏解:奇葩贪心+模拟?(不知如何称呼不过居然比std还快)]