<commit_before>package com.github.onsdigital.perkin.transform.jpg;

import com.github.onsdigital.perkin.json.Survey;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * Create one or more images representing the survey questions and answers.
 */
public class ImageBuilder {

    public ImageInfo createImages(final Survey survey, final long batchId) throws IOException {

        byte[] pdf = createPdf(survey, batchId);

        return createImages(pdf, survey, batchId);
    }

    //TODO: change to throw TransformException
    //TODO: make it a runtime exception?

    private byte[] createPdf(final Survey survey, final long batchId) throws IOException {
        //TODO: create pdf for the survey


        //TODO for now we have hardcoded a 2 page pdf
        Path path = null;
        try {
            path = Paths.get(ClassLoader.getSystemResource("/to-jpg/2page.pdf").toURI());
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IOException("problem loading pdf", e);
        }
        return Files.readAllBytes(path);
    }

    public ImageInfo createImages(final byte[] pdf, final Survey survey, final long batchId) throws IOException {

        ImageInfo.ImageInfoBuilder builder = ImageInfo.builder();

        ByteArrayInputStream is = new ByteArrayInputStream(pdf);
        PDDocument document = PDDocument.load(is);

        List<PDPage> pages = document.getDocumentCatalog().getAllPages();
        int i = 0;
        for (PDPage page : pages) {
            i++;

            //TODO: convert pdf page to image
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int dpiImageResolution = 300;
            BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, dpiImageResolution);
            ImageIO.write(image, "JPG", baos);

            builder.image(
                    Image.builder()
                            //TODO: generate proper image filename (info from Rachel)
                            .filename("page" + i + ".jpg")
                            .data(baos.toByteArray())
                            .build()
            );

            System.out.println("created image: " + "page" + i + ".jpg");
            //TODO: create/append to image index csv
        }

        return builder.build();
    }
}
<commit_msg>fix typo in pdf path<commit_after>package com.github.onsdigital.perkin.transform.jpg;

import com.github.onsdigital.perkin.json.Survey;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * Create one or more images representing the survey questions and answers.
 */
public class ImageBuilder {

    public ImageInfo createImages(final Survey survey, final long batchId) throws IOException {

        byte[] pdf = createPdf(survey, batchId);

        return createImages(pdf, survey, batchId);
    }

    //TODO: change to throw TransformException
    //TODO: make it a runtime exception?

    private byte[] createPdf(final Survey survey, final long batchId) throws IOException {
        //TODO: create pdf for the survey


        //TODO for now we have hardcoded a 2 page pdf
        Path path = null;
        try {
            path = Paths.get(ClassLoader.getSystemResource("to-jpg/2page.pdf").toURI());
        } catch (URISyntaxException e) {
            e.printStackTrace();
            throw new IOException("problem loading pdf", e);
        }
        return Files.readAllBytes(path);
    }

    public ImageInfo createImages(final byte[] pdf, final Survey survey, final long batchId) throws IOException {

        ImageInfo.ImageInfoBuilder builder = ImageInfo.builder();

        ByteArrayInputStream is = new ByteArrayInputStream(pdf);
        PDDocument document = PDDocument.load(is);

        List<PDPage> pages = document.getDocumentCatalog().getAllPages();
        int i = 0;
        for (PDPage page : pages) {
            i++;

            //TODO: convert pdf page to image
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int dpiImageResolution = 300;
            BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, dpiImageResolution);
            ImageIO.write(image, "JPG", baos);

            builder.image(
                    Image.builder()
                            //TODO: generate proper image filename (info from Rachel)
                            .filename("page" + i + ".jpg")
                            .data(baos.toByteArray())
                            .build()
            );

            System.out.println("created image: " + "page" + i + ".jpg");
            //TODO: create/append to image index csv
        }

        return builder.build();
    }
}

{'fraction_non_alphanumeric': 0.0980355819125278, 'fraction_numerical': 0.002223869532987398, 'mean_word_length': 2.3773466833541925, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5529754', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1425, 'n_words': 398}