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

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

/*
 * Copyright (c) 2007 Patrick Wright
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the
 * 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.Graphics2DRenderer;
import org.xhtmlrenderer.swing.Java2DRenderer;
import org.xhtmlrenderer.util.FSImageWriter;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 */
public class ImageRender {
    /**
     * Renders a file or URL to an image file. Command line use: first
     * argument is URL or file path, second argument is path to image file to generate.
     *
     * @param args see desc
     * @throws java.io.IOException if source could not be read, or if image path is invalid
     */
    public static void main(String[] args) throws IOException {
        if (args.length != 1) {
            usage("Incorrect argument list.");
        }
        String url = args[0];
        if (!url.contains("://")) {
            // maybe it's a file
            File f = new File(url);
            if (f.exists()) {
                Java2DRenderer renderer = Java2DRenderer.fromFile(f, 1024, BufferedImage.TYPE_INT_RGB);
                BufferedImage image = renderer.getImage();

                FSImageWriter imageWriter = new FSImageWriter();
                String path = f.getAbsolutePath();
                path = path.substring(0, path.lastIndexOf('.'));
                imageWriter.write(image, path + ".png");

                // compare to old
                BufferedImage img = Graphics2DRenderer.renderToImageAutoSize(f.toURI().toURL().toExternalForm(), 1024, BufferedImage.TYPE_INT_ARGB);
                ImageIO.write(img, "png", new File(path + "-G2DR.png"));

            } else {
                usage("File to render is not found: " + url);
            }
        } else {
            // ignore for now
        }
    }

    /**
     * prints out usage information, with optional error message
     */
    private static void usage(String err) {
        if (err != null && !err.isEmpty()) {
            System.err.println("==>" + err);
        }
        System.err.println("Usage: ... [url]");
        System.exit(1);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy