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

org.xhtmlrenderer.pdf.ITextUserAgent Maven / Gradle / Ivy

Go to download

Flying Saucer is a CSS 2.1 renderer written in Java. This artifact supports PDF output with iText 5.x.

There is a newer version: 9.7.2
Show newest version
/*
 * {{{ header & license
 * Copyright (c) 2004, 2005 Torbj�rn Gannholm
 * Copyright (c) 2006 Wisconsin Court System
 *
 * 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.
 * }}}
 */
package org.xhtmlrenderer.pdf;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfReader;
import org.xhtmlrenderer.extend.FSImage;
import org.xhtmlrenderer.resource.ImageResource;
import org.xhtmlrenderer.swing.NaiveUserAgent;
import org.xhtmlrenderer.util.Configuration;
import org.xhtmlrenderer.util.ContentTypeDetectingInputStreamWrapper;
import org.xhtmlrenderer.util.IOUtil;
import org.xhtmlrenderer.util.ImageUtil;
import org.xhtmlrenderer.util.XRLog;

import javax.annotation.ParametersAreNonnullByDefault;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

@ParametersAreNonnullByDefault
public class ITextUserAgent extends NaiveUserAgent {
    private static final int IMAGE_CACHE_CAPACITY = 32;

    private final ITextOutputDevice _outputDevice;
    private final int dotsPerPixel;

    public ITextUserAgent(ITextOutputDevice outputDevice, int dotsPerPixel) {
        super(Configuration.valueAsInt("xr.image.cache-capacity", IMAGE_CACHE_CAPACITY));
        this.dotsPerPixel = dotsPerPixel;
        _outputDevice = outputDevice;
    }

    int getDotsPerPixel() {
        return dotsPerPixel;
    }

    private byte[] readStream(InputStream is) throws IOException {
        return IOUtil.readBytes(is);
    }

    @Override
    public ImageResource getImageResource(String uriStr) {
        ImageResource resource;
        if (!ImageUtil.isEmbeddedBase64Image(uriStr)) {
            uriStr = resolveURI(uriStr);
        }
        resource = _imageCache.get(uriStr);

        if (resource == null) {
            if (ImageUtil.isEmbeddedBase64Image(uriStr)) {
                resource = loadEmbeddedBase64ImageResource(uriStr);
                _imageCache.put(uriStr, resource);
            } else {
                InputStream is = resolveAndOpenStream(uriStr);
                if (is != null) {
                    try {
                        ContentTypeDetectingInputStreamWrapper cis=new ContentTypeDetectingInputStreamWrapper(is);
                        is=cis;
                        if (cis.isPdf()) {
                            URI uri = new URI(uriStr);
                            PdfReader reader = _outputDevice.getReader(uri);
                            PDFAsImage image = new PDFAsImage(uri);
                            Rectangle rect = reader.getPageSizeWithRotation(PDFAsImage.pageNumberFromURI(uri));
                            image.setInitialWidth(rect.getWidth() * _outputDevice.getDotsPerPoint());
                            image.setInitialHeight(rect.getHeight() * _outputDevice.getDotsPerPoint());
                            resource = new ImageResource(uriStr, image);
                        } else {
                            Image image = Image.getInstance(readStream(is));
                            scaleToOutputResolution(image);
                            resource = new ImageResource(uriStr, new ITextFSImage(image));
                        }
                        _imageCache.put(uriStr, resource);
                    } catch (Exception e) {
                        XRLog.exception("Can't read image file; unexpected problem for URI '" + uriStr + "'", e);
                    } finally {
                        IOUtil.close(is);
                    }
                }
            }
        }
        if (resource != null) {
            FSImage image = resource.getImage();
            if (image instanceof ITextFSImage) {
                image = (FSImage) ((ITextFSImage) resource.getImage()).clone();
            }
            resource = new ImageResource(resource.getImageUri(), image);
        } else {
            resource = new ImageResource(uriStr, null);
        }
        return resource;
    }

    private ImageResource loadEmbeddedBase64ImageResource(final String uri) {
        try {
            byte[] buffer = ImageUtil.getEmbeddedBase64Image(uri);
            Image image = Image.getInstance(buffer);
            scaleToOutputResolution(image);
            return new ImageResource(null, new ITextFSImage(image));
        } catch (BadElementException | IOException e) {
            XRLog.exception("Can't read XHTML embedded image.", e);
        }
        return new ImageResource(null, null);
    }

    private void scaleToOutputResolution(Image image) {
        float factor = dotsPerPixel;
        if (factor != 1.0f) {
            image.scaleAbsolute(image.getPlainWidth() * factor, image.getPlainHeight() * factor);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy