private void handleError(Exception e) {
throw new RuntimeException("Unable to export to PDF", e);
}
public void doExport(final Profile profile, final int width, final int height) {
final PauseTransition pt = new PauseTransition();
pt.setDuration(Duration.millis(profile.getPause()));
final AtomicInteger cpt = new AtomicInteger();
final Document document = new Document(new Rectangle(width, height), 0, 0, 0, 0);
try {
PdfWriter.getInstance(document, new FileOutputStream(exportFile));
} catch (DocumentException | FileNotFoundException e) {
handleError(e);
return;
}
document.open();
profile.setDocument(document);
pt.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
WritableImage image = browser.snapshot(null, null);
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null);
double scaler = ((document.getPageSize().getWidth() - document.leftMargin()
- document.rightMargin()) / image.getWidth()) * 100;
try {
com.itextpdf.text.Image image2 =
com.itextpdf.text.Image.getInstance(bufferedImage, null);
image2.scalePercent((float) scaler);
document.add(image2);
document.newPage();
int current = cpt.incrementAndGet();
int nbSlides = profile.getSlideCount();
System.out.println("Exported slide " + current + (nbSlides > 0 ? "/" + nbSlides : ""));
if (!profile.isLastSlide(current)) {
profile.nextSlide();
pt.setDuration(Duration.millis(profile.getPause()));
pt.play();
} else {
profile.finish();
document.close();
System.out.println("Export complete.");
Platform.exit();
}
} catch (IOException | DocumentException e) {
handleError(e);
}
}
});
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<Worker.State>() {
public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
if (newState == Worker.State.SUCCEEDED) {
profile.setup();
profile.ready(new Runnable() { public void run() { pt.play(); } });
}
}
});
}