Search code examples
javapdfprintingstruts2

Java code print PDF file from client machine


I am developing Billing module in my application in Java (Struts2 framework). After successful submission of Bill form, my code generates PDF of bill. What I want to implement is, on the successful bill form submission and on successful PDF generation, the generated PDF should print from printer. I wrote the code for printing PDF, but the problem here is, by that code, it is only possible to print the PDF file from the same machine where application is deployed and printer is connected. But that is not going to be the architecture after completion of application. There is going to be client-server architecture, where my application will be deployed on one server and many client will be using that application from their respective machines.

So the problem here is, every other machine is connected to different printers and by my current code it is only possible to print the PDF from where the application is deployed i.e, in this case, server.

Now my question here is:

  1. Is it possible to achieve PDF printing, in such scenario, using Java, I mean, printing PDF from printer which is connected to the respective client machine?

  2. If yes, how would I do that?

Here is my code snippet:

import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Sides;

public class PDFPrintDemo {

    public static void main(String[] args) {
        try {

            System.out.println("Printing PDF demo using JAVA.");

            DocFlavor docFlavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;

        PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();

        attributeSet.add(Sides.ONE_SIDED);
        attributeSet.add(new Copies(2));

        PrintService[] printServices = PrintServiceLookup
                .lookupPrintServices(docFlavor, attributeSet);

        if (printServices.length == 0) {
            System.out.println("No printer found...");
        }

        PrintService myService = null;

        for (PrintService service : printServices) {
            System.out.println("Connected printer name is :: "
                    + service.getName());
            if (service.getName().equals(
                    "Hewlett-Packard-HP-LaserJet-Pro-MFP-M126nw")) {

                myService = service;

                break;

            }
        }

        FileInputStream inputStream = new FileInputStream(
                "/home/roshan/Downloads/rudhiraBillDemo.pdf");

        Doc pdfDoc = new SimpleDoc(inputStream,
                DocFlavor.INPUT_STREAM.AUTOSENSE, null);

        DocPrintJob printJob = myService.createPrintJob();

        printJob.print(pdfDoc, attributeSet);

        inputStream.close();

        System.out.println("PDF printed successfully..");

    } catch (Exception exception) {
        exception.printStackTrace();
    }
}

}

Solution

  • I guess you your final project is a web site which clients want to download and print a PDF file.

    If my guess is true then your answer is no! Clients can download but you can not force it to be printed!

    You can write an struts action which returns a PDF file (same as you done above), but (because of security issues) there is no way that you can develop any html+javascript code which send the generated PDF to client printer. As you know there are other limitations in html+javascript , for example you can not access client camera or microphone.

    If you defiantly want this, you should use Java Applet, Flash Player or ActiveX(Windows only). These technologies can access any device on client machine.