Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rokon12
Created June 10, 2021 05:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rokon12/85705a4abe04cd572774bebdf76aebb7 to your computer and use it in GitHub Desktop.
Save rokon12/85705a4abe04cd572774bebdf76aebb7 to your computer and use it in GitHub Desktop.
package com.bazlur;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class ExportPdf {
private static final String OUTPUT_FOLDER = "exports/";
private static final String IMAGE_FOLDER = "images/";
private static final String IMAGE_EXTENSION = ".jpg";
public static void main(String[] args) throws IOException, DocumentException {
var imageSourcePath = Path.of(IMAGE_FOLDER);
var document = new Document(PageSize.A4, 20.0f, 20.0f, 20.0f, 150.0f);
PdfWriter.getInstance(document, new FileOutputStream(OUTPUT_FOLDER + "/export.pdf"));
document.open();
Files.walk(imageSourcePath)
.filter(path -> path.toString().endsWith(IMAGE_EXTENSION))
.forEach(path -> addImageToDocument(document, path));
document.close();
}
private static void addImageToDocument(Document document, Path path) {
try {
var image = Image.getInstance(path.toUri().toURL());
document.setPageSize(image);
document.newPage();
image.setAbsolutePosition(0, 0);
document.add(image);
} catch (IOException | DocumentException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment