Print PDF's directly, without using an external program

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

Print PDF's directly, without using an external program

Postby pbakker » Wed Nov 01, 2006 6:06 pm

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
pbakker
 
Posts: 2822
Joined: Wed Oct 01, 2003 8:12 pm
Location: Amsterdam, the Netherlands

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

Postby lhale » Thu May 10, 2012 6:11 am

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
lhale
 
Posts: 108
Joined: Mon Mar 17, 2008 3:49 pm

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

Postby sbutler » Sat May 12, 2012 3:54 am

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.
Scott Butler
iTech Professionals, Inc.
SAN Partner

Servoy Consulting & Development
Servoy University- Training Videos
Servoy Components- Plugins, Beans, and Web Components
Servoy Guy- Tips & Resources
ServoyForge- Open Source Components
User avatar
sbutler
Servoy Expert
 
Posts: 759
Joined: Sun Jan 08, 2006 7:15 am
Location: Cincinnati, OH

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

Postby lwjwillemsen » Thu Jun 28, 2012 7:07 pm

@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,
Lambert Willemsen
Vision Development BV
lwjwillemsen
 
Posts: 680
Joined: Sat Mar 14, 2009 5:39 pm
Location: The Netherlands

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

Postby Harjo » Thu Jun 28, 2012 9:11 pm

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
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

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

Postby omar » Fri Jun 29, 2012 10:58 am

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
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

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

Postby Harjo » Fri Jun 29, 2012 11:35 am

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.
Harjo Kompagnie
ServoyCamp
Servoy Certified Developer
Servoy Valued Professional
SAN Developer
Harjo
 
Posts: 4321
Joined: Fri Apr 25, 2003 11:42 pm
Location: DEN HAM OV, The Netherlands

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

Postby ptalbot » Fri Jun 29, 2012 3:55 pm

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).
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC


Return to How To

Who is online

Users browsing this forum: No registered users and 3 guests