Code for printing an html file.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Code for printing an html file.

Hybrid View

  1. #1
    Join Date
    Jul 2004
    Posts
    54

    Code for printing an html file.

    I have the following code for printing an html file from within a program.

    I pass a String with the html code in textString.

    I get this error message:
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: services must be non-null and non-empty
    at javax.print.ServiceUI.printDialog(Unknown Source)
    at PrintHtml.printHtml(PrintHtml.java:24)

    Please help as I don't understand what is incorrect.

    Thanks,
    RON C

    Code:
    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintException;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.ServiceUI;
    import javax.print.SimpleDoc;
    import javax.print.attribute.DocAttributeSet;
    import javax.print.attribute.HashDocAttributeSet;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    
    class PrintHtml
    {
    public void printHtml(String textString)
    {
    DocFlavor flavor = DocFlavor.STRING.TEXT_HTML;
    PrintService[] services = PrintServiceLookup.lookupPrintServices(
    flavor, null);
    PrintService defaultService = PrintServiceLookup
    .lookupDefaultPrintService();
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    PrintService service = ServiceUI.printDialog(null, 50, 50, services,
    defaultService, flavor, pras);
    if (service != null)
    {
    DocPrintJob job = service.createPrintJob();
    DocAttributeSet das = new HashDocAttributeSet();
    Doc doc = new SimpleDoc(textString, flavor, das);
    try
    {
    job.print(doc, pras);
    }
    catch (PrintException pe)
    {
    }
    }
    }
    }
    Last edited by Jamie; 04-07-2005 at 05:14 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Dublin, Ireland
    Posts
    63
    It looks to me like:

    Code:
    PrintServiceLookup.lookupPrintServices(flavor, null);
    is returning null, and that is being assigned into services. So I would test whether services is or is not null before attempting to use it.
    VBForums.com & DevX.com Super Moderator
    Microsoft MVP : Visual Developer - Visual Basic
    My Website

  3. #3
    Join Date
    Jul 2004
    Posts
    54
    You're correct.

    I found a tool DocumentRenderer that works if you use HTMLDocument methodology.

    Thanks,
    RON C

  4. #4
    Join Date
    Jun 2006
    Posts
    1
    Hi , i am trying the same code.

    DocFlavor psInFormat = DocFlavor.STRING.TEXT_HTML;
    Doc doc = new SimpleDoc(data, psInFormat, null);
    // data is a html formated string
    PrintService psZebra = null;
    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
    if (services.length >= 0) {
    psZebra = services[1];
    } else {
    return;
    }
    DocPrintJob job = psZebra.createPrintJob();
    job.print(doc, null);

    // i am getting invalid flavour exception.

    can you plz. help me out

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center