cancel
Showing results for 
Search instead for 
Did you mean: 

How to print a document with web services

stlecho
Champ in-the-making
Champ in-the-making
I would be pleased if someone could provide documentation on how to print a document by using a web service ?
3 REPLIES 3

jcustovic
Champ in-the-making
Champ in-the-making
You can user alfresco web service to add, update, delete content but not to print.
You can download content you want to print and then use Java Print Service API or other API for printing.

stlecho
Champ in-the-making
Champ in-the-making
Would it be possible to get some code examples of this ?

jcustovic
Champ in-the-making
Champ in-the-making
You can print jpeg file from alfresco using this code. Reference to the file is defined by alfresco nodeUuid.

public static void main(String[] args) throws ContentFault, RemoteException {
      Store workspace = new Store(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
      Reference referencePath = new Reference(workspace, "1f177f86-f8d3-40a2-922e-598171e3bf1c", null);
      
      WebServiceFactory.setEndpointAddress("http://localhost:8080/alfresco/api");
      AuthenticationUtils.startSession("admin", "admin");
      try {
         Content[] read = WebServiceFactory.getContentService().read(new Predicate(new Reference[] { referencePath }, workspace, null), Constants.PROP_CONTENT);
         Content content = read[0];
         print(ContentUtils.getContentAsInputStream(content));
      } finally {
         AuthenticationUtils.endSession();
      }
   }
   
   private static void print(InputStream in) {
      DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;

      PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
      aset.add(MediaSizeName.ISO_A4);
      aset.add(new Copies(1));

      PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);

      if (pservices.length > 0) {
         DocPrintJob printJob = pservices[0].createPrintJob();
         Doc doc = new SimpleDoc(in, flavor, null);
         try {
            printJob.print(doc, aset);
         } catch (PrintException e) {
            System.err.println(e);
         }
      } else {
         System.err.println("No suitable printers");
      }
   }