Example usage for org.apache.pdfbox.rendering PDFRenderer PDFRenderer

List of usage examples for org.apache.pdfbox.rendering PDFRenderer PDFRenderer

Introduction

In this page you can find the example usage for org.apache.pdfbox.rendering PDFRenderer PDFRenderer.

Prototype

public PDFRenderer(PDDocument document) 

Source Link

Document

Creates a new PDFRenderer.

Usage

From source file:net.yacy.cora.util.Html2Image.java

License:Open Source License

/**
 * convert a pdf (first page) to an image. proper values are i.e. width = 1024, height = 1024, density = 300, quality = 75
 * using internal pdf library or external command line tool on linux or mac
 * @param pdf input pdf file/*from w w w  . j  a va  2 s. c  o  m*/
 * @param image output jpg file
 * @param width
 * @param height
 * @param density (dpi)
 * @param quality
 * @return
 */
public static boolean pdf2image(File pdf, File image, int width, int height, int density, int quality) {
    final File convert = convertMac1.exists() ? convertMac1
            : convertMac2.exists() ? convertMac2 : convertDebian;

    // convert pdf to jpg using internal pdfbox capability
    if (OS.isWindows || !convert.exists()) {
        try {
            PDDocument pdoc = PDDocument.load(pdf);
            BufferedImage bi = new PDFRenderer(pdoc).renderImageWithDPI(0, density, ImageType.RGB);

            return ImageIO.write(bi, "jpg", image);

        } catch (IOException ex) {
        }
    }

    // convert on mac or linux using external command line utility
    try {
        // i.e. convert -density 300 -trim yacy.pdf[0] -trim -resize 1024x -crop x1024+0+0 -quality 75% yacy-convert-300.jpg
        // note: both -trim are necessary, otherwise it is trimmed only on one side. The [0] selects the first page of the pdf
        String command = convert.getAbsolutePath() + " -density " + density + " -trim " + pdf.getAbsolutePath()
                + "[0] -trim -resize " + width + "x -crop x" + height + "+0+0 -quality " + quality + "% "
                + image.getAbsolutePath();
        List<String> message = OS.execSynchronous(command);
        if (image.exists())
            return true;
        ConcurrentLog.warn("Html2Image", "failed to create image with command: " + command);
        for (String m : message)
            ConcurrentLog.warn("Html2Image", ">> " + m);

        // another try for mac: use Image Events using AppleScript in osacript commands...
        // the following command overwrites a pdf with an png, so we must make a copy first
        if (!OS.isMacArchitecture)
            return false;
        File pngFile = new File(pdf.getAbsolutePath() + ".tmp.pdf");
        org.apache.commons.io.FileUtils.copyFile(pdf, pngFile);
        String[] commandx = { "osascript", "-e", "set ImgFile to \"" + pngFile.getAbsolutePath() + "\"", "-e",
                "tell application \"Image Events\"", "-e", "set Img to open file ImgFile", "-e",
                "save Img as PNG", "-e", "end tell" };
        //ConcurrentLog.warn("Html2Image", "failed to create image with command: " + commandx);
        message = OS.execSynchronous(commandx);
        for (String m : message)
            ConcurrentLog.warn("Html2Image", ">> " + m);
        // now we must read and convert this file to a jpg with the target size 1024x1024
        try {
            File newPngFile = new File(pngFile.getAbsolutePath() + ".png");
            pngFile.renameTo(newPngFile);
            Image img = ImageParser.parse(pngFile.getAbsolutePath(), FileUtils.read(newPngFile));
            final Image scaled = img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
            final MediaTracker mediaTracker = new MediaTracker(new Container());
            mediaTracker.addImage(scaled, 0);
            try {
                mediaTracker.waitForID(0);
            } catch (final InterruptedException e) {
            }
            // finally write the image
            final BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            bi.createGraphics().drawImage(scaled, 0, 0, width, height, null);
            ImageIO.write(bi, "jpg", image);
            newPngFile.delete();
            return image.exists();
        } catch (IOException e) {
            ConcurrentLog.logException(e);
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:org.apache.syncope.client.console.wicket.markup.html.form.preview.BinaryPDFPreviewer.java

License:Apache License

@Override
public Component preview(final byte[] uploadedBytes) {
    firstPage = null;//  w w  w  .ja v a2s.c om

    PDDocument document = null;
    try {
        document = PDDocument.load(new ByteArrayInputStream(uploadedBytes));
        if (document.isEncrypted()) {
            LOG.info("Document is encrypted, no preview is possible");
        } else {
            firstPage = new PDFRenderer(document).renderImage(0, RESOLUTION, IMAGE_TYPE);
        }
    } catch (IOException e) {
        LOG.error("While generating thumbnail from first page", e);
    } finally {
        IOUtils.closeQuietly(document);
    }

    Fragment fragment;
    if (firstPage == null) {
        fragment = new Fragment("preview", "noPreviewFragment", this);
    } else {
        fragment = new Fragment("preview", "previewFragment", this);
        fragment.add(new NonCachingImage("previewImage", new ThumbnailImageResource(firstPage)));
    }

    WebMarkupContainer previewContainer = new WebMarkupContainer("previewContainer");
    previewContainer.setOutputMarkupId(true);
    previewContainer.add(fragment);
    return this.addOrReplace(previewContainer);
}

From source file:org.apache.tika.parser.pdf.AbstractPDF2XHTML.java

License:Apache License

void doOCROnCurrentPage() throws IOException, TikaException, SAXException {
    if (config.getOcrStrategy().equals(NO_OCR)) {
        return;/*from  w  w  w  .j a  v  a2 s  .c  o  m*/
    }
    TesseractOCRConfig tesseractConfig = context.get(TesseractOCRConfig.class, DEFAULT_TESSERACT_CONFIG);

    TesseractOCRParser tesseractOCRParser = new TesseractOCRParser();
    if (!tesseractOCRParser.hasTesseract(tesseractConfig)) {
        throw new TikaException("Tesseract is not available. "
                + "Please set the OCR_STRATEGY to NO_OCR or configure Tesseract correctly");
    }

    PDFRenderer renderer = new PDFRenderer(pdDocument);
    TemporaryResources tmp = new TemporaryResources();
    try {
        BufferedImage image = renderer.renderImage(pageIndex, 2.0f, config.getOcrImageType());
        Path tmpFile = tmp.createTempFile();
        try (OutputStream os = Files.newOutputStream(tmpFile)) {
            //TODO: get output format from TesseractConfig
            /*ImageIOUtil.writeImage(image, config.getOcrImageFormatName(),
                os, config.getOcrDPI());*/
        }
        try (InputStream is = TikaInputStream.get(tmpFile)) {
            tesseractOCRParser.parseInline(is, xhtml, tesseractConfig);
        }
    } catch (IOException e) {
        handleCatchableIOE(e);
    } catch (SAXException e) {
        throw new IOExceptionWithCause("error writing OCR content from PDF", e);
    } finally {
        tmp.dispose();
    }
}

From source file:org.dspace.app.mediafilter.PDFBoxThumbnail.java

License:BSD License

/**
 * @param source/*from   w ww.  j a v  a2s. c  o  m*/
 *            source input stream
 * 
 * @return InputStream the resulting input stream
 */
@Override
public InputStream getDestinationStream(Item currentItem, InputStream source, boolean verbose)
        throws Exception {
    PDDocument doc = PDDocument.load(source);
    PDFRenderer renderer = new PDFRenderer(doc);
    BufferedImage buf = renderer.renderImage(0);
    //        ImageIO.write(buf, "PNG", new File("custom-render.png"));
    doc.close();

    JPEGFilter jpegFilter = new JPEGFilter();
    return jpegFilter.getThumb(currentItem, buf, verbose);
}

From source file:org.geomajas.plugin.printing.document.DefaultDocumentTest.java

License:Open Source License

@Test
public void testToImage() throws Exception {
    testRender();/*from w ww  .jav a  2 s. com*/
    PDDocument pdf = PDDocument.load(new File("target/test.pdf"), true);
    PDFRenderer renderer = new PDFRenderer(pdf);
    BufferedImage bufferedImage = renderer.renderImageWithDPI(0, 144);
    pdf.close();
    ImageIO.write(bufferedImage, "PNG", new File("target/test.png"));
}

From source file:org.geomajas.plugin.printing.document.SinglePageDocument.java

License:Open Source License

private void writeDocument(OutputStream outputStream, Format format, int dpi)
        throws IOException, DocumentException, PrintingException {
    if (format == Format.PDF) {
        baos.writeTo(outputStream);//from   w  w  w.  j av a  2s.  c o m
    } else {
        PDDocument pdf = PDDocument.load(new ByteArrayInputStream(baos.toByteArray()), true);
        PDFRenderer renderer = new PDFRenderer(pdf);
        BufferedImage bufferedImage = renderer.renderImageWithDPI(0, dpi);
        pdf.close();
        if (format == Format.PNG) {
            final String formatName = format.getExtension();
            for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
                ImageWriter writer1 = iw.next();
                ImageWriteParam writeParam = writer1.getDefaultWriteParam();
                ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier
                        .createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);
                IIOMetadata metadata = writer1.getDefaultImageMetadata(typeSpecifier, writeParam);
                if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
                    continue;
                }

                setDPI(metadata);
                // Write bufferedImage to outputStream
                final ImageOutputStream stream = ImageIO.createImageOutputStream(outputStream);
                try {
                    writer1.setOutput(stream);
                    writer1.write(metadata, new IIOImage(bufferedImage, null, metadata), writeParam);
                } finally {
                    stream.flush();
                    stream.close();
                }
                break;
            }
        } else {
            ImageIO.write(bufferedImage, format.getExtension(), outputStream);
        }

    }
}

From source file:org.haplo.component.pdfbox.PDF.java

License:Mozilla Public License

/**
 * Render the PDF as an image/* ww w . ja  v  a  2s . c  o  m*/
 */
public void render(String outFilename, String outFormat, int page, int outWidth, int outHeight)
        throws IOException {
    BufferedImage img = null;
    try {
        PDPage pdfPage = this.pdf.getPage(page - 1);
        PDRectangle cropBox = pdfPage.getCropBox();
        int pageWidth = (int) cropBox.getWidth();
        int pageHeight = (int) cropBox.getHeight();
        if (pageHeight <= 0) {
            pageHeight = 1;
        }

        int resolution = (96 * outHeight) / pageHeight;
        if (resolution < 4) {
            resolution = 4;
        }
        if (resolution > 1000) {
            resolution = 1000;
        }
        if (outHeight < 100 || outWidth < 100) {
            resolution *= 2;
        }

        PDFRenderer pdfRenderer = new PDFRenderer(this.pdf);

        img = pdfRenderer.renderImageWithDPI(page - 1, resolution,
                outFormat.equals("png") ? ImageType.ARGB : ImageType.RGB);
    } catch (Exception e) {
        Logger.getLogger("org.haplo.app").error("Error rendering PDF: " + e.toString());
        throw new RuntimeException("Couldn't render PDF page", e);
    }

    // Did it convert? (most likely cause of null return is requested a page which didn't exist)
    if (img == null) {
        throw new RuntimeException("Failed to render PDF - did the requested page exist?");
    }

    // Scale the image to the right size
    BufferedImage original = null;
    if (img.getWidth() != outWidth || img.getHeight() != outHeight) {
        original = img;
        Image scaled = img.getScaledInstance(outWidth, outHeight, Image.SCALE_SMOOTH);
        img = new BufferedImage(outWidth, outHeight, original.getType());
        Graphics2D graphics = img.createGraphics();
        graphics.setBackground(Color.WHITE);
        graphics.clearRect(0, 0, outWidth, outHeight);
        graphics.drawImage(scaled, 0, 0, null);
        graphics.dispose();
        scaled.flush();
    }

    // Write the image to a file
    ImageIO.write(img, outFormat, new File(outFilename));

    // Free resources
    img.flush();
    if (original != null) {
        original.flush();
    }
}

From source file:org.mycore.iview2.frontend.MCRPDFTools.java

License:Open Source License

static BufferedImage getThumbnail(Path pdfFile, int thumbnailSize, boolean centered) throws IOException {
    InputStream fileIS = Files.newInputStream(pdfFile);
    PDDocument pdf = PDDocument.load(fileIS);
    try {/*from  w  w  w .ja  va  2 s . c  o  m*/
        PDFRenderer pdfRenderer = new PDFRenderer(pdf);
        BufferedImage level1Image = pdfRenderer.renderImage(0);
        int imageType = BufferedImage.TYPE_INT_ARGB;

        if (!centered) {
            return level1Image;
        }
        final double width = level1Image.getWidth();
        final double height = level1Image.getHeight();
        LOGGER.info("new PDFBox: " + width + "x" + height);
        LOGGER.info("temporary image dimensions: " + width + "x" + height);
        final int newWidth = width < height ? (int) Math.ceil(thumbnailSize * width / height) : thumbnailSize;
        final int newHeight = width < height ? thumbnailSize : (int) Math.ceil(thumbnailSize * height / width);
        //if centered make thumbnailSize x thumbnailSize image
        final BufferedImage bicubic = new BufferedImage(centered ? thumbnailSize : newWidth,
                centered ? thumbnailSize : newHeight, imageType);
        LOGGER.info("target image dimensions: " + bicubic.getWidth() + "x" + bicubic.getHeight());
        final Graphics2D bg = bicubic.createGraphics();
        bg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        int x = centered ? (thumbnailSize - newWidth) / 2 : 0;
        int y = centered ? (thumbnailSize - newHeight) / 2 : 0;
        if (x != 0 && y != 0) {
            LOGGER.warn("Writing at position " + x + "," + y);
        }
        bg.drawImage(level1Image, x, y, x + newWidth, y + newHeight, 0, 0, (int) Math.ceil(width),
                (int) Math.ceil(height), null);
        bg.dispose();
        return bicubic;
    } finally {
        pdf.close();
    }
}

From source file:org.qifu.util.PdfConvertUtils.java

License:Apache License

public static List<File> toImageFiles(File pdfFile, int resolution) throws Exception {
    PDDocument document = PDDocument.load(pdfFile);
    PDFRenderer pdfRenderer = new PDFRenderer(document);
    /*//from   www  .j a  va 2s  . co m
    List<PDPage> pages = new LinkedList<PDPage>();
    for (int i=0; i < document.getDocumentCatalog().getPages().getCount(); i++) {
       pages.add( document.getDocumentCatalog().getPages().get(i) );
    }
    */
    File tmpDir = new File(Constants.getWorkTmpDir() + "/" + PdfConvertUtils.class.getSimpleName() + "/"
            + System.currentTimeMillis() + "/");
    FileUtils.forceMkdir(tmpDir);
    List<File> files = new LinkedList<File>();
    //int len = String.valueOf(pages.size()+1).length();
    int len = String.valueOf(document.getDocumentCatalog().getPages().getCount() + 1).length();
    //for (int i=0; i<pages.size(); i++) {
    for (int i = 0; i < document.getDocumentCatalog().getPages().getCount(); i++) {
        String name = StringUtils.leftPad(String.valueOf(i + 1), len, "0");
        BufferedImage bufImage = pdfRenderer.renderImageWithDPI(i, resolution, ImageType.RGB);
        File imageFile = new File(tmpDir.getPath() + "/" + name + ".png");
        FileOutputStream fos = new FileOutputStream(imageFile);
        ImageIOUtil.writeImage(bufImage, "png", fos, resolution);
        fos.flush();
        fos.close();
        files.add(imageFile);
    }
    document.close();
    tmpDir = null;
    return files;
}

From source file:org.quelea.data.pdf.PDFPresentation.java

License:Open Source License

/**
 * Make the slides that go in this PDF, this is what takes time and should
 * only be done once.//  ww w .j a v  a  2 s .  c  om
 *
 * @return all the slides.
 */
private PdfSlide[] makeSlides() throws IOException {
    File pdf = new File(file);
    PDDocument document = PDDocument.load(pdf.getAbsoluteFile());
    Path f = Files.createTempDirectory(null);
    f.toFile().deleteOnExit();
    ArrayList<PdfSlide> ret = new ArrayList<>();
    PDFRenderer pdfRenderer = new PDFRenderer(document);
    int totalPages = document.getNumberOfPages();
    for (int i = 0; i < totalPages; i++) {
        ret.add(new PdfSlide(i + 1, pdfRenderer));
    }
    document.close();
    return ret.toArray(new PdfSlide[ret.size()]);
}