Page 1 of 1

Print PDF's directly, without using an external program

PostPosted: Wed Nov 01, 2006 6:06 pm
by pbakker
In respons to questyions raised in topic "http://forum.servoy.com/viewtopic.php?t=2914", hereby a brief introduction to printing PDF documents directly.

When is this usefull?
- When you want to print PDF's without a user that needs to push buttons (batchprocessors)
- When you want to print on machines that do not have software installed to print PDF's (for example Acrobat)

How does it work?
The PDFEncoder bean that can be installed as a bean for Servoy allready gives you the ability to view PDF's that are either on disk, in memory or in the database. The same bean can be used to print directly. For this, some java code is required. Servoy has the ability to use java code directly in it's methods. The PDFEncoder is used to send a print command directly through Java's printing services, which can allready handle the printing of PDF.

Requirements
- The latest version of the PDFEncoder bean. This bean can be downloaded from the download section of the the manufacturers website: http://www.jpedal.org/. You need to download jpedal and place the downloaded jar-file in the beans directory of Servoy. Don't forget to remove the old JPedal.jar library!!!

The method:
Code: Select all
try
{
   var docflavor = Packages.javax.print.DocFlavor;
   var service = docflavor.SERVICE_FORMATTED
   var pageble = service.PAGEABLE;
   
   var defaultService = Packages.javax.print.PrintServiceLookup.lookupDefaultPrintService();
   if ( defaultService != null )
   {
      var job = defaultService.createPrintJob();

      //var decodePDF = elements.XXX; //uncomment if you have the PDFEncoder bean on your form


      var decodePDF = new Packages.org.jpedal.PdfDecoder(true); //comment out if you have the PDFEncoder bean on your form
      decodePDF.openPdfArray( XXX /*blobfield name*/ ); //comment out if you have the PDFEncoder bean on your form


      var pageCount = decodePDF.getPageCount();
      decodePDF.setPagePrintRange(1,pageCount);
      
      var doc = new Packages.javax.print.SimpleDoc(decodePDF, pageble, null);
      job.print( doc, null );
   }
}
catch ( e )
{
   application.output(e.toString());
}


Limitations
- This is not a feature rich piece of code. All possible layout options are not embedded. If you need to do things with formatting, check out the forum on the earlier mentioned website and extend the code to your liking.
- The method is hardcoded to use the default printer. Use the internet to find sources to rewrate that part of the logic to make it more flexible
- Since this solutions accesses the Java layer, when used in the WebClient, the processing will be done Serverside, so printing will also take place on the Server.

Paul

Re: Print PDF's directly, without using an external program

PostPosted: Thu May 10, 2012 6:11 am
by lhale
I am having trouble getting a pdf to print directly with the latest jpedal-lgpl. I am using the code below and it is throwing an exception "JavaException: java.lang.IllegalArgumentException: data is not of declared type". I think it has something to do with the var pageable when inserted into "var doc = new Packages.javax.print.SimpleDoc(pdf,pageable,null);"

Code: Select all
      try
    {
      var pdf = elements.bean;
      pdf.openPdfFile('C:\\file.pdf');
                pdf.setPagePrintRange(1,1);
   

      var docflavor = Packages.javax.print.DocFlavor;
          var service = docflavor.SERVICE_FORMATTED;
               var pageable = service.PAGEABLE;
             
          var defaultService = Packages.javax.print.PrintServiceLookup.lookupDefaultPrintService();
          if(defaultService != null)
          {
            var job = defaultService.createPrintJob();
   
            var doc = new Packages.javax.print.SimpleDoc(pdf,pageable,null);
            
            pdf.setPrintAutoRotateAndCenter(true)
              
            job.print(doc,null);
       }
    }
    catch ( e )
    {
       application.output(e.toString());
    }



I need a clean way to send pdf's directly to the printer without opening any external programs. Any an all help is appreciated.

servoy 5.2.7 windows

Re: Print PDF's directly, without using an external program

PostPosted: Sat May 12, 2012 3:54 am
by sbutler
You can do an OS level call with application.execute..
On Mac, you would call the "lpr" command.
On Windows, you can do a call to the acrobat reader to print it without opening it.

Re: Print PDF's directly, without using an external program

PostPosted: Thu Jun 28, 2012 7:07 pm
by lwjwillemsen
@lhale :

I am having trouble getting a pdf to print directly with the latest jpedal-lgpl.


On the jpdedal.org site I see that printing functionality is not present in the LGPL-version of the bean...

Regards,

Re: Print PDF's directly, without using an external program

PostPosted: Thu Jun 28, 2012 9:11 pm
by Harjo
I did'nt test it, but you could try to print pdf directly with this:

viewtopic.php?f=22&t=18291&p=98438&hilit=open+files#p98438

Re: Print PDF's directly, without using an external program

PostPosted: Fri Jun 29, 2012 10:58 am
by omar
I recently found this command in a post by Martin:

Code: Select all
//send the PDF to the printer
application.executeProgram( '/usr/bin/lpr', "-T", jobName, "-P", printerName, "-#" , printQuantity, "-o", "media=" + printerTray, jobName );


See: http://forum.servoy.com/viewtopic.php?t=94740

But like he says it depends on the OS so the PDFEncoder bean sounds like a promising alternative

Re: Print PDF's directly, without using an external program

PostPosted: Fri Jun 29, 2012 11:35 am
by Harjo
Indeed, this does not work on WIndows.
There are also some alternatives to print directly to Acrobat, but than you are depending on local installed software.

Re: Print PDF's directly, without using an external program

PostPosted: Fri Jun 29, 2012 3:55 pm
by ptalbot
It would be possible to create a plugin out of the PDFRenderer lib to print PDF files without the need for any external PDF software (it's the one I'm using for printing in VelocityReport).