我想使用 Java 将 EPUB 文档转换为 PDF。 我发现了很多关于将 PDF 转换为 EPUB 的问题,但没有其他问题。
有没有我可以通过 Java 调用的 Java 库或命令行工具来发挥神奇的作用?
提前致谢!
-格什
最佳答案
如果您不害怕使用在线服务,那么EPUB to PDF Rest API可以使用。
实际的 JAVA 代码看起来像
public class Main {
public static void main(String[] args) throws IOException {
TreeMap params = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
params.put("File", "C:\path\to\test-epub.epub");
params.put("Secret", "<secret>");
CloseableHttpResponse response = ConvertApi.convert("epub", "pdf", params);
System.out.println(EntityUtils.toString(response.getEntity(), "UTF-8"));
}
}
class ConvertApi {
public static CloseableHttpResponse convert(String srcFormat, String dstFormat, TreeMap params) throws IOException {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
params.forEach((k, v) -> {
if(Files.exists(Paths.get(v))) {
builder.addBinaryBody(k, new File(v));
} else {
builder.addTextBody(k, v, ContentType.TEXT_PLAIN);
}
});
String authParam = params.get("secret") == null ? String.format("Token=%s", params.get("token")) : String.format("Secret=%s", params.get("secret"));
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(String.format("https://v2.convertapi.com/%s/to/%s?%s", srcFormat, dstFormat, authParam));
httpPost.setEntity(builder.build());
return httpClient.execute(httpPost);
}
}
关于java - 使用 Java 将 EPUB 转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26734907/