All Downloads are FREE. Search and download functionalities are using the official Maven repository.

erer.flying-saucer-examples.9.9.4.source-code.SimplePrintable Maven / Gradle / Ivy

There is a newer version: 9.11.2
Show newest version
/*
 * {{{ header & license
 * Copyright (c) 2008 Patrick Wright
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * }}}
 */

import org.xhtmlrenderer.simple.XHTMLPanel;
import org.xhtmlrenderer.simple.XHTMLPrintable;

import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;


/**
 * Example of how to print an XHTMLPanel; the heart of the code is in the
 * {@link #printPanel(org.xhtmlrenderer.simple.XHTMLPanel)} method.
 *
 * @author Patrick Wright
 */
public class SimplePrintable {
    public static void main(String[] args) throws MalformedURLException, URISyntaxException, PrinterException {
        if (args.length == 0) {
            System.err.println("Need file path");
            System.exit(-1);
        }

        if (args[0].startsWith("http:")) {
            new SimplePrintable().printURL(args[0]);
        } else {
            final String path = args[0];
            final URL url = new URL(path);
            final File file = new File(url.toURI());
            if (!file.exists()) {
                System.err.println("File not found: " + args[0]);
                System.exit(-1);
            }
            new SimplePrintable().printURL(url.toExternalForm());
        }
    }

    private void printPanel(XHTMLPanel panel) throws PrinterException {
        final PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(new XHTMLPrintable(panel));

        if (printJob.printDialog()) {
            printJob.print();
        }
    }

    private void printURL(String url) throws PrinterException {
        XHTMLPanel panel = new XHTMLPanel();
        panel.getSharedContext().setPrint(true);
        panel.getSharedContext().setInteractive(false);

        panel.setDocument(url);
        printPanel(panel);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy