Class PrinterJob

java.lang.Object
java.awt.print.PrinterJob
vaadinx.awt.print.PrinterJob

public class PrinterJob extends PrinterJob
The virtual PDF printer — import-swap replacement for java.awt.print.PrinterJob (D_printing). One printer, always present: every page the app's Printable draws is rendered server-side into a PDF (via PdfPrinterGraphicsFactory), and the finished document is offered to the user in a Vaadin download dialog; the user prints the PDF with their local viewer, on their own printer. No OS print subsystem is ever touched — the server's printers are the wrong machine's printers.

This is the only java.awt.print type the migrator swaps: the JDK no longer honors the historical java.awt.printerjob system property (getPrinterJob() hard-wires a platform job since JDK 24-ish), so the injection seam is the import-rewrite, same as for the component classes. Printable, PageFormat, Paper, Book, Pageable and the printer exceptions reference nothing we port and stay JDK. Extending the JDK class keeps assignments to java.awt.print.PrinterJob-typed variables compiling, and inherits the concrete defaults (printDialog(attributes) delegates to printDialog(), print(attributes) to print(), getPrintService() returns null).

UI-context contract: the job captures UI.getCurrent() at creation, so the classic Swing pattern of calling print() on a background thread still finds the right browser session — provided the job itself was created in a UI context (event listener, SwingWorker, Timer callback). When neither the captured nor the calling-thread UI exists, print() throws IllegalStateException: a print with no session to deliver to is a programming error, not a soft incompleteness (the R_callswing_envelope stance).

Faithful divergences (documented in the module README): printDialog() returns true without UI — nothing to configure on a single always-present printer, and the terminal download dialog is the cancelable moment. pageDialog(PageFormat) echoes its argument. defaultPage(PageFormat) is A4 portrait with 1" margins where the JDK is locale-dependent. Copies are ignored (WARN when > 1) — a PDF download is inherently one copy. lookupPrintServices() is empty — the virtual printer is not a javax.print.PrintService, and the inherited JDK static would have enumerated the server's printers.

  • Constructor Details

    • PrinterJob

      public PrinterJob()
  • Method Details

    • getPrinterJob

      public static PrinterJob getPrinterJob()
      Hides the JDK static; returns the virtual PDF printer job.
    • lookupPrintServices

      public static PrintService[] lookupPrintServices()
      Hides the JDK static, which would enumerate the server's print services. The virtual printer is not a PrintService; code that needs one is in must-rewrite territory (see the migration checklist).
    • setPrintable

      public void setPrintable(Printable painter)
      Specified by:
      setPrintable in class PrinterJob
    • setPrintable

      public void setPrintable(Printable painter, PageFormat format)
      Specified by:
      setPrintable in class PrinterJob
    • setPageable

      public void setPageable(Pageable document) throws NullPointerException
      Specified by:
      setPageable in class PrinterJob
      Throws:
      NullPointerException
    • printDialog

      public boolean printDialog()
      Always true, with no dialog shown: a single always-present virtual printer has no options to ask about, and the download dialog print() ends in is itself the cancelable moment (close it without downloading). The attribute-taking overload inherits the JDK default, which delegates here.
      Specified by:
      printDialog in class PrinterJob
    • pageDialog

      public PageFormat pageDialog(PageFormat page)
      No page-setup UI — echoes page unchanged (the JDK contract's "user cancelled" shape). Pass the desired format to setPrintable(Printable, PageFormat) instead.
      Specified by:
      pageDialog in class PrinterJob
    • defaultPage

      public PageFormat defaultPage(PageFormat page)
      A4 portrait with 1" margins. Divergence from the JDK's locale-dependent default (Letter in US locales): the server's locale says nothing about the user's paper anyway, and A4-vs-Letter differences are absorbed by the PDF viewer's fit-to-page at physical print time.
      Specified by:
      defaultPage in class PrinterJob
    • validatePage

      public PageFormat validatePage(PageFormat page)
      The virtual printer accepts any page geometry — a clone is already valid.
      Specified by:
      validatePage in class PrinterJob
    • print

      public void print() throws PrinterException
      Specified by:
      print in class PrinterJob
      Throws:
      PrinterException
    • setCopies

      public void setCopies(int copies)
      Specified by:
      setCopies in class PrinterJob
    • getCopies

      public int getCopies()
      Specified by:
      getCopies in class PrinterJob
    • getUserName

      public String getUserName()
      The server-process user, matching the JDK. Nearly meaningless in a web deployment (every session sees the same value) but harmless — apps use it as a cosmetic job-attribute default.
      Specified by:
      getUserName in class PrinterJob
    • setJobName

      public void setJobName(String jobName)
      Specified by:
      setJobName in class PrinterJob
    • getJobName

      public String getJobName()
      Specified by:
      getJobName in class PrinterJob
    • cancel

      public void cancel()
      Specified by:
      cancel in class PrinterJob
    • isCancelled

      public boolean isCancelled()
      Specified by:
      isCancelled in class PrinterJob