Direct printing

Hi all,

Is there a way to trigger printing without going the system print dialog?
Context: using thermal printer in a Point of Sale setup.

Thanks

Hello @samybill ,

You can write your own method that selects the printer and starts printing using PrintService (Java Platform SE 8 ), for example:

XML:

       <hbox id="buttonsPanel" classNames="buttons-panel">
            <button id="reportBtn" action="usersDataGrid.emailAction"/>
            <button id="createBtn" action="usersDataGrid.create"/>
            <button id="editBtn" action="usersDataGrid.edit"/>
            <button id="removeBtn" action="usersDataGrid.remove"/>
            <button id="showRoleAssignmentsBtn" action="usersDataGrid.showRoleAssignments"/>
            <button id="printUsersBtn" text="msg://printReport" icon="FILE_TEXT"/>

            <dropdownButton id="additionalBtn" text="msg://additionalMenu" icon="COG">
                <items>
                    <actionItem id="changePasswordItem" ref="usersDataGrid.changePassword"/>
                    <actionItem id="resetPasswordItem" ref="usersDataGrid.resetPassword"/>
                </items>
            </dropdownButton>
            <simplePagination id="pagination" dataLoader="usersDl"/>
        </hbox>

Controller:

private static final Logger log = LoggerFactory.getLogger(UserListView.class);

    @Autowired
    private ReportRunner reportRunner;
    @Autowired
    private Notifications notifications;

    @Subscribe("printUsersBtn")
    public void onPrintUsersBtnClick(final ClickEvent<JmixButton> event) {
        ReportOutputDocument document = reportRunner.byReportCode("reportCode")
                .run();

        PrintService service = PrintServiceLookup.lookupDefaultPrintService();

        try (PDDocument pddocument = PDDocument.load(document.getContent())) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPageable(new PDFPageable(pddocument));
            job.setPrintService(service);
            job.print();
        } catch (Exception e) {
            log.error("Failed to print document", e);
            notifications.create("Failed to print document")
                    .withType(Notifications.Type.ERROR)
                    .show();
        }
    }

Regards,
Nikita

2 Likes