

package de.unigoettingen.sub.converter;


import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.redis.RedisClient;
import io.vertx.redis.RedisOptions;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


/**
 * These are the examples used in the documentation.
 *
 * @author <a href="mailto:pmlopes@gmail.com">Paulo Lopes</a>
 */
public class PdfFromImagesConverterVerticle extends AbstractVerticle {

    final static Logger log = Logger.getLogger(PdfFromImagesConverterVerticle.class);

    int MAX_ATTEMPTS = Integer.valueOf(System.getenv("MAX_ATTEMPTS"));

    String product = System.getenv("SHORT_PRODUCT");

    String imageoutpath = System.getenv("OUT") + System.getenv("IMAGE_OUT_SUB_PATH");
    String pdfoutpath = System.getenv("OUT") + System.getenv("PDF_OUT_SUB_PATH");

    String imageoutformat = System.getenv("IMAGE_OUT_FORMAT");

    int pdfdensity = Integer.valueOf(System.getenv("PDFDENSITY"));

    String redis_host = System.getenv("REDIS_HOST");
    int redis_port = Integer.valueOf(System.getenv("REDIS_EXTERNAL_PORT"));
    int redis_db = Integer.valueOf(System.getenv("REDIS_DB"));


    @Override
    public void start() {

        System.out.println(Thread.currentThread().getName() + " started...");

        final RedisClient redis = RedisClient.create(vertx,
                new RedisOptions().setHost(redis_host).setPort(redis_port).setSelect(redis_db));


        redis.llen("convertpdf", value -> {
                    if (value.succeeded()) {

                        long i = value.result();

                        for (int j = 0; j < i; j++) {
                            foo(redis);
                        }

                    }
                }
        );


    }


    public void foo(RedisClient redis) {


        redis.brpop("convertpdf", 30, path -> {
                    if (path.succeeded()) {

                        JsonArray json = path.result();

                        if (json != null) {

                            JsonObject object = new JsonObject(json.getString(1));

                            String from = object.getString("'from'");
                            String name = object.getString("name");
                            String format = object.getString("'format'");

                            String to_images_dir = imageoutpath + "/" + product + "/" + name + "/";
                            String to_pdf_dir = pdfoutpath + "/" + product + "/" + name + "/";

                            convertTo(new File(from), to_images_dir, imageoutformat);
                            convertTo(new File(from), to_pdf_dir, "pdf");

                        }

                    }

                }

        );

    }


    public void convertTo(File from, String to_dir, String format) {

        if (!fileExists(from)) {
            throw new RuntimeException("File not found ! (" + from.getAbsolutePath() + ")");
        }


        String fileName = "";

        try {

            FileUtils.forceMkdir(new File(to_dir));

            // load PDF document
            PDDocument document = PDDocument.load(from, "");


            // create PDF renderer
            PDFRenderer renderer = new PDFRenderer(document);

            // go through each page of PDF, and generate TIF for each PDF page.
            for (int i = 0; i < document.getNumberOfPages(); i++) {
                // Returns the given page as an RGB image with 300 DPI.
                BufferedImage image = renderer.renderImageWithDPI(i, pdfdensity, ImageType.RGB);


                // Assign the file name of TIF
                //String fileName = pdfFileName + "_" + String.format("%06d", i + 1);
                fileName = String.format("%06d", i + 1);


                // Writes a buffered image to a file using the given image format.
                boolean done = ImageIOUtil.writeImage(image, to_dir + "/" + fileName + "." + format, pdfdensity);

                image.flush();

            }

            document.close();


            log.info("PDF to TIF conversion well done for: " + fileName);
        } catch (IOException e) {
            log.error("IOException with destination file: " + to_dir + "\n");
            e.printStackTrace();
            return;
        } catch (Exception e) {
            log.error("Exception with destination file: " + to_dir + "\n");
            e.printStackTrace();
            return;
        }
    }


    /**
     *
     */
    private Boolean fileExists(File file) {
        Boolean exists = Boolean.FALSE;
        exists = (file.exists() && (!file.isDirectory()));
        return exists;
    }


}
@@ -1280,24 +1280,95 @@
 FDENSITY%22));
+%0A    int imagedensity = Integer.valueOf(System.getenv(%22IMAGEDENSITY%22));
 %0A%0A    String
@@ -2538,14 +2538,12 @@
 ng(%22
-'
 from
-'
 %22);%0A
@@ -2630,32 +2630,34 @@
                 
+//
 String format = 
@@ -2678,17 +2678,83 @@
 ng(%22
-'
 format
-'
 %22
+);%0A%0A                            System.out.println(from + %22 %22 + name
 );%0A%0A
@@ -2973,34 +2973,36 @@
             
-convertTo(
+File file = 
 new File(fro
@@ -2999,24 +2999,75 @@
 w File(from)
+;%0A%0A                            convertTo(file, name
 , to_images_
@@ -3085,16 +3085,30 @@
 utformat
+, imagedensity
 );%0A     
@@ -3140,30 +3140,26 @@
 nvertTo(
-new File(from)
+file, name
 , to_pdf
@@ -3169,16 +3169,28 @@
 r, %22pdf%22
+, pdfdensity
 );%0A%0A    
@@ -3310,16 +3310,29 @@
 le from,
+ String name,
  String 
@@ -3352,16 +3352,29 @@
 g format
+, int density
 ) %7B%0A%0A   
@@ -3910,14 +3910,15 @@
 i = 
-0
+1
 ; i %3C
+=
  doc
@@ -4091,19 +4091,16 @@
 hDPI(i, 
-pdf
 density,
@@ -4301,20 +4301,16 @@
 %2506d%22, i
- + 1
 );%0A%0A%0A   
@@ -4490,19 +4490,16 @@
 format, 
-pdf
 density)
@@ -4610,15 +4610,8 @@
 PDF 
-to TIF 
 conv
@@ -4636,21 +4636,17 @@
 or: %22 + 
-fileN
+n
 ame);%0A  

Convert to pages (PDFs and also images)