/** * Creates a document converter. * * @param exe_path The full path to the <code>ebook-convert</code> executable. (Uses {@link * #findEbookConvert()} if an empty string or <code>null</code> .) * @param output_profile The argument for the <code> * <a href="http://manual.calibre-ebook.com/cli/ebook-convert.html#cmdoption-ebook-convert--output-profile">--output-profile</a> * </code> argument (e.g. “<code>nook</code>”). Use <code>null</code> or an empty * string to not include. * @param author The author for the article (see <code> * <a href="http://manual.calibre-ebook.com/cli/ebook-convert.html#cmdoption-ebook-convert--authors">--authors</a> * </code> option.) Use <code>null</code> or an empty string to not include. * @param other_options Extra command line options to be passed to <code>ebook-convert</code> when * converting a document. See the <a href= * "http://manual.calibre-ebook.com/cli/ebook-convert.html" >Calibre User Manual</a> for * documentation on the <code>calibredb add</code> command and options. */ public EbookConverter( final String exe_path, final String output_profile, final String author, final List<String> other_options) { if (Str.isNotEmpty(exe_path)) this.exePath = exe_path; else this.exePath = findEbookConvert(); final ImmutableList.Builder<String> bldr = ImmutableList.builder(); if (Str.isNotEmpty(output_profile)) bldr.add("--output-profile=" + output_profile); if (Str.isNotEmpty(author)) bldr.add("--authors=" + author); if (other_options != null) bldr.addAll(other_options); this.options = bldr.build(); }
/** * Converts a document (usually HTML) to another format (e.g. epub or mobi) using <code> * ebook-convert</code>. * * @param title An optional title (use an empty string or <code>null</code> for unspecified or to * use whatever is in the <code>file</code> (if anything).) See <code><a href= * "http://manual.calibre-ebook.com/cli/ebook-convert.html#cmdoption-ebook-convert--title" * >--title</a></code> in the Calibre documentation. * @param in_file The file to be converted. * @param out_file The target file. * @param summary An optional summary of the contents of the document, in HTML (use an empty * string or <code>null</code> for unspecified or to use whatever is in the <code>file</code> * (if anything).) See <code><a href= * "http://manual.calibre-ebook.com/cli/ebook-convert.html#cmdoption-ebook-convert--comments" * >--comments</a></code> in the Calibre documentation. * @throws IOException */ public void convert( final String title, final File in_file, final File out_file, final String summary) throws IOException { final List<String> cmd = Colls.newArrayList(10); cmd.add(exePath); cmd.add(in_file.toString()); cmd.add(out_file.toString()); if (Str.isNotEmpty(title)) cmd.add("--title=" + title); if (Str.isNotEmpty(summary)) cmd.add("--comments=" + summary); cmd.addAll(options); log.debug("Running: {}", Str.join(", ", cmd)); final Process proc = new ProcessBuilder(cmd).redirectErrorStream(true).start(); final ProcessOutputSlurper slurper = new ProcessOutputSlurper(proc); final int ret; try { ret = proc.waitFor(); } catch (InterruptedException ex) { throw new UnhandledException(ex); } final List<String> output = slurper.getLines(); if (ret != 0) log.trace("ebook-convert returned {}", ret); log.trace("ebook-convert output:"); for (String line : output) log.trace(line); if (ret == 0) { final Pattern pat = Pattern.compile("\\S+ output written to .+$"); for (String line : output) if (pat.matcher(line).matches()) return; } throw new IOException("Error converting document. See log file for details."); }