> When you "print" a complex document you need to invoke a copy of an 
> application that understands the "print" shell command. 
> 
> You know - file extension associations.
> 
> This would generally mean that you would need to associate PDF documents
> with Acrobat Reader so they can be printed.  And you would set the default
> launch action to be PRINT.

Java has a print API. I wrote some code that works well printing images,
text, html, etc from the server and also prints pdf's - but only to
printers that are PostScript Level 3.0 compliant. The Java Print API (as
of 1.4.2) is still a little flakey for pdf printing to anything less then
post script 3. The INPUT_STREAM.PDF DocFlavour is still a little flawed.

I wrote a simple cfx tag to do this, print.java below. You maybe able to
convert into a cfc or something. The tag itself provides a little control
over the print job; no. of copies, orientation, media, chromaticity, etc.

If anyone wants the compiled class file send me an email off list.

/*
 * File:           print.java
 * Date:           12 June 2003  18:57
 *
 * @author  Rod Higgins
 * @version 1.3
 */

import com.allaire.cfx.*;
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class print implements CustomTag {
    public void processRequest(Request request, Response response) throws
Exception {
        if(request.attributeExists("file")) {
            String filename = request.getAttribute("file");
            PrintRequestAttributeSet aset = new
HashPrintRequestAttributeSet();
            // copies
            if (request.attributeExists("copies")) {
                aset.add(new
Copies(Integer.parseInt(request.getAttribute("copies"))));
            }
            // Media size
            if(request.attributeExists("media")) {
                if (request.getAttribute("media").equalsIgnoreCase( "A4" )) {
                        aset.add(MediaName.ISO_A4_WHITE);
                }
                if(request.getAttribute("media").equalsIgnoreCase( "Transparent")) {
                        aset.add(MediaName.ISO_A4_TRANSPARENT);
                }
                if(request.getAttribute("media").equalsIgnoreCase( "Letter")) {
                        aset.add(MediaName.NA_LETTER_WHITE);
                }
            }
            
            // chromaticity
            if (request.attributeExists("chromaticity")) {
                if(request.getAttribute("chromaticity").equalsIgnoreCase(
"monochrome")) {
                    aset.add(Chromaticity.MONOCHROME);
                }
                if(request.getAttribute("chromaticity").equalsIgnoreCase( "color")) {
                    aset.add(Chromaticity.COLOR);
                }
            } 
            // fidelity
            if(request.attributeExists("fidelity")) {
                if (request.getAttribute("fidelity").equalsIgnoreCase("false")) {
                        aset.add(Fidelity.FIDELITY_FALSE);
                }
                if(request.getAttribute("fidelity").equalsIgnoreCase( "true")) {
                        aset.add(Fidelity.FIDELITY_TRUE);
                }
            }
            // sides
            if (request.attributeExists("sides")) {
                if(request.getAttribute("sides").equalsIgnoreCase("DUPLEX") ||
request.getAttribute("sides").equalsIgnoreCase("duplex") ) {
                        aset.add(Sides.DUPLEX);
                }
                if(request.getAttribute("sides").equalsIgnoreCase("one_sided") ||
request.getAttribute("sides").equalsIgnoreCase("ONE_SIDED")) {
                        aset.add(Sides.ONE_SIDED);
                }
            }
            // orientation
            if(request.attributeExists("orientation")) {
                if (request.getAttribute("orientation").equalsIgnoreCase( "landscape"))
{
                        aset.add(OrientationRequested.LANDSCAPE);
                }
                if(request.getAttribute("orientation").equalsIgnoreCase( "Portrait")) {
                        aset.add(OrientationRequested.PORTRAIT);
                } 
            }               
            // job name
            if(request.attributeExists("jobname")) {
                aset.add(new
JobName(request.getAttribute("jobname"),null));
            }   
            // find extension on file name to determine DocFlavour
            String filetype="";
            String file="";
            StringTokenizer stringtokenizer = new
StringTokenizer(filename, ".");
            int pathLength = stringtokenizer.countTokens();
            String path[] = new String[pathLength];
            int i = 0;
            while(stringtokenizer.hasMoreTokens()) {
                path[i] = stringtokenizer.nextToken();
                i++;
            }
            filetype=path[pathLength-1];
            response.writeDebug("Filetype is " + filetype + "<br>");
            // default DocFlavour
            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            // check to see if it is anything else
            
            if(filetype.equalsIgnoreCase( "text") ||
filetype.equalsIgnoreCase( "txt")) {
                flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII;
            }
            if(filetype.equalsIgnoreCase( "html")) {
                flavor = DocFlavor.INPUT_STREAM.TEXT_HTML_US_ASCII ;
            }
            if(filetype.equalsIgnoreCase(  "GIF")) {
                flavor = DocFlavor.INPUT_STREAM.GIF;
            }
            if(filetype.equalsIgnoreCase( "JPEG")) {
                flavor = DocFlavor.INPUT_STREAM.JPEG;
            }
            if(filetype.equalsIgnoreCase( "PCL")) {
                flavor = DocFlavor.INPUT_STREAM.PCL;
            }
            if(filetype.equalsIgnoreCase( "PDF")) {
                flavor = DocFlavor.INPUT_STREAM.PDF;
            }
            if(filetype.equalsIgnoreCase("PS")) {
                flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
            }
            PrintService[] services =
PrintServiceLookup.lookupPrintServices(flavor, aset);
            if (services.length > 0) {
                response.writeDebug("The printer being used is <b>" +
services[0].getName() + "</b><br>");
                DocFlavor[] docs = services[0].getSupportedDocFlavors();
                if (docs.length > 0) {
                response.writeDebug("The printer supports the following
DocFlavour/s<br>");
                    for (int z=0;z<docs.length;z++){
                        response.writeDebug("DocFlavour <b>" +
docs[z].getMediaType() + ", " + docs[z].getMimeType() + ", " +
docs[z].getMediaSubtype() + "</b><br>");
                    }
                }
                DocPrintJob job = services[0].createPrintJob();
                FileInputStream fis = new FileInputStream(filename);
                DocAttributeSet das = new HashDocAttributeSet();
                Doc doc = new SimpleDoc(fis, flavor, das);
                job.print(doc, aset);
            } else {
                response.writeDebug("No printers available");
            }    
        }
    }   
}

hth
Rod


---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]

MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia
http://www.mxdu.com/ + 24-25 February, 2004

Reply via email to