添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Hi Team

I want to print a document in a PDF format from the Application programmatically, I would also appreciate if you could help with how to accomplish this task using WebBrowserTools as well. Has anyone done this to guide me?

I have another function I would like to have implemented that is to View a document, in a case where a user does not want to print but view the contents of the document and close it. Please guide me on this one as well.

Thank you for your help.

Hello @fluidsoftwaredevelopers ,

here my bean to print a PDF from application:

mport com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.FileLoader;
import com.haulmont.cuba.core.global.FileStorageException;
import com.haulmont.cuba.core.global.Messages;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
import javax.print.PrintServiceLookup;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Service(PrintService.NAME)
public class PrintServiceBean implements PrintService {
    @Inject
    private FileLoader fileLoader;
    @Inject
    private Messages messages;
    @Inject
    private Logger log;
    @Override
    public List<String> getPrinterNames() {
        final javax.print.PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        return Arrays.stream(printServices)
                .map(javax.print.PrintService::getName)
                .map(String::trim)
                .sorted()
                .collect(Collectors.toList());
    @Override
    public String getDefaultPrinterName() {
        final javax.print.PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        return printService != null ? printService.getName() : null;
    @Override
    public void print(FileDescriptor fd, String printerName, int copies) {
        log.info("Printing {} on printer \"{}\"", fd, printerName);
        Objects.requireNonNull(fd);
        Objects.requireNonNull(printerName);
        final javax.print.PrintService printService = findPrintService(printerName);
        if (printService == null) {
            throw new PrintException(messages.formatMessage(getClass(), "printerNotFound", printerName));
        try {
            final InputStream inputStream = fileLoader.openStream(fd);
            final PDDocument document = PDDocument.load(inputStream);
            final PrinterJob job = PrinterJob.getPrinterJob();
            job.setPageable(new PDFPageable(document));
            job.setPrintService(printService);
            job.setCopies(copies);
            job.print();
        } catch (FileStorageException | IOException | PrinterException e) {
            throw new PrintException(e);
    private javax.print.PrintService findPrintService(String printerName) {
        if (StringUtils.isBlank(printerName)) {
            return PrintServiceLookup.lookupDefaultPrintService();
        final javax.print.PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        return Arrays.stream(printServices)
                .filter(ps -> printerName.equals(ps.getName().trim()))
                .findAny()
                .orElse(null);

If the server is a Linux system you need to install CUPS and the command /usr/bin/lpr.

Then edit the file /etc/cups/cupsd.conf and add or comment out the following entries and
then restart CUPS:

Listen *:631
Listen 192.168.166.211:631
WebInterface Yes

Afterwards, the printer can be created via the address ‘http://localhost:631’, if it was not found automatically. In case of external access, port 631 can also be redirected via SSH.

Greetings
Andreas