Class PrinterJob
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcancel()defaultPage(PageFormat page) A4 portrait with 1" margins.intstatic PrinterJobHides the JDK static; returns the virtual PDF printer job.The server-process user, matching the JDK.booleanstatic PrintService[]Hides the JDK static, which would enumerate the server's print services.pageDialog(PageFormat page) No page-setup UI — echoespageunchanged (the JDK contract's "user cancelled" shape).voidprint()booleanAlways true, with no dialog shown: a single always-present virtual printer has no options to ask about, and the download dialogprint()ends in is itself the cancelable moment (close it without downloading).voidsetCopies(int copies) voidsetJobName(String jobName) voidsetPageable(Pageable document) voidsetPrintable(Printable painter) voidsetPrintable(Printable painter, PageFormat format) validatePage(PageFormat page) The virtual printer accepts any page geometry — a clone is already valid.Methods inherited from class PrinterJob
defaultPage, getPageFormat, getPrintService, lookupStreamPrintServices, pageDialog, print, printDialog, setPrintService
-
Constructor Details
-
PrinterJob
public PrinterJob()
-
-
Method Details
-
getPrinterJob
Hides the JDK static; returns the virtual PDF printer job. -
lookupPrintServices
Hides the JDK static, which would enumerate the server's print services. The virtual printer is not aPrintService; code that needs one is in must-rewrite territory (see the migration checklist). -
setPrintable
- Specified by:
setPrintablein classPrinterJob
-
setPrintable
- Specified by:
setPrintablein classPrinterJob
-
setPageable
- Specified by:
setPageablein classPrinterJob- 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 dialogprint()ends in is itself the cancelable moment (close it without downloading). The attribute-taking overload inherits the JDK default, which delegates here.- Specified by:
printDialogin classPrinterJob
-
pageDialog
No page-setup UI — echoespageunchanged (the JDK contract's "user cancelled" shape). Pass the desired format tosetPrintable(Printable, PageFormat)instead.- Specified by:
pageDialogin classPrinterJob
-
defaultPage
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:
defaultPagein classPrinterJob
-
validatePage
The virtual printer accepts any page geometry — a clone is already valid.- Specified by:
validatePagein classPrinterJob
-
print
- Specified by:
printin classPrinterJob- Throws:
PrinterException
-
setCopies
public void setCopies(int copies) - Specified by:
setCopiesin classPrinterJob
-
getCopies
public int getCopies()- Specified by:
getCopiesin classPrinterJob
-
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:
getUserNamein classPrinterJob
-
setJobName
- Specified by:
setJobNamein classPrinterJob
-
getJobName
- Specified by:
getJobNamein classPrinterJob
-
cancel
public void cancel()- Specified by:
cancelin classPrinterJob
-
isCancelled
public boolean isCancelled()- Specified by:
isCancelledin classPrinterJob
-