-
-
Save rokon12/85705a4abe04cd572774bebdf76aebb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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