import javafx.application.Application;
import javafx.print.Printer;
import javafx.print.PrinterJob;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.loadContent("<html><body><h1>Hello, World!</h1></body></html>");
Scene scene = new Scene(webView, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
Printer printer = Printer.getDefaultPrinter();
PrinterJob job = PrinterJob.createPrinterJob(printer);
if (job != null && job.showPrintDialog(primaryStage.getOwner())) {
webView.getEngine().print(job);
job.endJob();
}
}
public static void main(String[] args) {
launch(args);
}
}
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import javax.print.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
// Step 1: Convert HTML to PDF
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(baos));
HtmlConverter.convertToPdf("<h1>Hello, World!</h1>", pdfDocument);
pdfDocument.close();
// Step 2: Send PDF to printer
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, attributes);
if (printServices.length > 0) {
PrintService printService = printServices[0];
DocPrintJob job = printService.createPrintJob();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
Doc doc = new SimpleDoc(bais, flavor, null);
job.print(doc, null);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}