org.xhtmlrenderer.simple.PDFRenderer Maven / Gradle / Ivy
The newest version!
package org.xhtmlrenderer.simple;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
*
* PDFRenderer supports headless rendering of XHTML documents, outputting
* to PDF format. There are two static utility methods, one for rendering
* a {@link java.net.URL}, {@link #renderToPDF(String, String)} and one
* for rendering a {@link File}, {@link #renderToPDF(File, String)}
* You can use this utility from the command line by passing in
* the URL or file location as first parameter, and PDF path as second
* parameter:
*
* java -cp %classpath% org.xhtmlrenderer.simple.PDFRenderer
*
*
* @author Pete Brant
* @author Patrick Wright
*/
public class PDFRenderer {
/**
* Renders the XML file at the given URL as a PDF file
* at the target location.
*
* @param url url for the XML file to render
* @param pdf path to the PDF file to create
* @throws IOException if the URL or PDF location is
* invalid
* @throws DocumentException if an error occurred
* while building the Document.
*/
public static void renderToPDF(String url, String pdf)
throws IOException, DocumentException {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
doRenderToPDF(renderer, pdf);
}
/**
* Renders the XML file as a PDF file at the target location.
*
* @param file XML file to render
* @param pdf path to the PDF file to create
* @throws IOException if the file or PDF location is
* invalid
* @throws DocumentException if an error occurred
* while building the Document.
*/
public static void renderToPDF(File file, String pdf)
throws IOException, DocumentException {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(file);
doRenderToPDF(renderer, pdf);
}
/**
* Internal use, runs the render process
*/
private static void doRenderToPDF(ITextRenderer renderer, String pdf)
throws IOException, DocumentException {
OutputStream os = null;
try {
os = new FileOutputStream(pdf);
renderer.layout();
renderer.createPDF(os);
os.close();
os = null;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
// ignore
}
}
}
}
/**
* Renders a file or URL to a PDF. Command line use: first
* argument is URL or file path, second
* argument is path to PDF file to generate.
*
* @param args see desc
* @throws IOException if source could not be read, or if
* PDF path is invalid
* @throws DocumentException if an error occurs while building
* the document
*/
public static void main(String[] args) throws IOException, DocumentException {
if (args.length != 2) {
usage("Incorrect argument list.");
}
String url = args[0];
if (url.indexOf("://") == -1) {
// maybe it's a file
File f = new File(url);
if (f.exists()) {
PDFRenderer.renderToPDF(f, args[1]);
} else {
usage("File to render is not found: " + url);
}
} else {
PDFRenderer.renderToPDF(url, args[1]);
}
}
/** prints out usage information, with optional error message */
private static void usage(String err) {
if (err != null && err.length() > 0) {
System.err.println("==>" + err);
}
System.err.println("Usage: ... [url] [pdf]");
System.exit(1);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy