The Printing API that allows programmatically printing the currently loaded web page using the PDF printer is not available in JxBrowser 7. It's already on our roadmap. We do our best to introduce this functionality in one of the next versions.
I recommend that you follow us on https://twitter.com/JxBrowserTeam to be notified when this functionality is released.
UPD: In JxBrowser 7.13 the Printing API has been extended with functionality that allows programmatically configure print settings and print a web page without displaying the Print Preview dialog. The API allows you to save the currently loaded web page as a PDF document. Read more at https://jxbrowser-support.teamdev.com/docs/guides/printing.html#configuring-settings
The following example demonstrates how to save a web page as PDF:
import static com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN;
import static com.teamdev.jxbrowser.print.Orientation.PORTRAIT;
import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.browser.callback.PrintCallback;
import com.teamdev.jxbrowser.browser.callback.PrintHtmlCallback;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.frame.Frame;
import com.teamdev.jxbrowser.print.PdfPrinter;
import com.teamdev.jxbrowser.print.PdfPrinter.HtmlSettings;
import com.teamdev.jxbrowser.print.PrintJob;
import com.teamdev.jxbrowser.print.event.PrintCompleted;
import java.nio.file.Paths;
/**
* This example demonstrates how to configure print settings programmatically and print the
* currently loaded web page using the built-in PDF printer. In general, it shows how to save the
* currently loaded web page as a PDF document.
*/
public final class PrintToPdf {
public static void main(String[] args) {
Engine engine = Engine.newInstance(OFF_SCREEN);
Browser browser = engine.newBrowser();
browser.set(PrintCallback.class, (params, tell) -> tell.print());
browser.set(PrintHtmlCallback.class, (params, tell) -> {
PdfPrinter<PdfPrinter.HtmlSettings> pdfPrinter =
params.printers().pdfPrinter();
PrintJob<HtmlSettings> printJob = pdfPrinter.printJob();
printJob.settings()
.pdfFilePath(Paths.get("google.pdf").toAbsolutePath())
.enablePrintingBackgrounds()
.orientation(PORTRAIT)
.apply();
printJob.on(PrintCompleted.class, event -> {
if (event.isSuccess()) {
System.out.println("Printing is completed successfully.");
} else {
System.out.println("Printing has failed.");
}
});
tell.proceed(pdfPrinter);
});
browser.navigation().loadUrlAndWait("https://google.com");
browser.mainFrame().ifPresent(Frame::print);
}
}