

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"));
    int imagedensity = Integer.valueOf(System.getenv("IMAGEDENSITY"));

    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");

                            System.out.println(from + " " + name);

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

                            File file = new File(from);

                            convertTo(file, name, to_images_dir, imageoutformat, imagedensity);
                            convertTo(file, name, to_pdf_dir, "pdf", pdfdensity);

                        }

                    }

                }

        );

    }


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

        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 = 1; i <= document.getNumberOfPages(); i++) {
                // Returns the given page as an RGB image with 300 DPI.
                BufferedImage image = renderer.renderImageWithDPI(i, density, ImageType.RGB);


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


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

                image.flush();

            }

            document.close();


            log.info("PDF conversion well done for: " + name);
        } 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;
    }


}
@@ -1860,16 +1860,23 @@
 nvertpdf
+toimage
 %22, value
@@ -2233,16 +2233,23 @@
 nvertpdf
+toimage
 %22, 30, p
@@ -3136,32 +3136,34 @@
                 
+//
 convertTo(file, 

Modify redis queue names