🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #545

Re: Simplest way to print reports in java1.4?

#545
From: Magnus
Date: Sun, 03 Jul 2005 15:38
80 lines
3735 bytes
Paul Nichols (TeamB) wrote:
> Magnus wrote:
> 
> 
>>Hi,
>>
>>I have been looking for some time on how to achieve a simple report.
>>At this moment I don't care about headers, footers, different fonts etc.
>>
> 
> What I use to print reports, is good ole HTML-XML. Best way to have full
> control over your format. Use a browser to send the report to, or a
> PrintStream.
> 
Thanks Paul!

That sounds good. I have tried to use the below example to stream a file 
to a printer. My plan was to create a txt file and send it to the 
printer but HTML would be even better... However, I always get:
"Print job failed PrintEvent on sun.print.Win32PrintJob@162e295"
no matter what file I try and to what printer I select.

Do you have any sample code to send a HTML file to a printer with 
PrintStream or PrintWriter? Is it possible to get the PageDialog and 
PrintDialog as part of it? Any example of how to handle pagebreak,LETTER 
and A4 size?

As always, I'm truly greatful for any help?

--- My file stream test ---

First I list the printers on the PC that can handle attributes like 
Letter and A4 size and put them in a ComboBox .

printers = PrintFile.listPrinters(attributes);
printerCB.removeAllItems();
for (int i=0; i<printers.length; i++) {
    printerCB.addItem(printers[i].getName());
}

The "filename"=absolute path to a txt-file.

// txt files only but I guess that TEXT_HTML_HOST would work
DocFlavor fileType = DocFlavor.INPUT_STREAM.TEXT_PLAIN_HOST;

InputStream readFile = new FileInputStream(filename);
Doc doc = new SimpleDoc(readFile, fileType, null);

DocPrintJob job = printer.createPrintJob();

job.addPrintJobListener(new PrintJobAdapter() { // Monitor the print job 
with a listener

      public void printJobCompleted(PrintJobEvent e) {
            System.out.println("Print job complete");
            System.exit(0);
      }
     public void printDataTransferCompleted(PrintJobEvent e) {
            System.out.println("Document transfered to printer");
     }
     public void printJobRequiresAttention(PrintJobEvent e) {
            System.out.println("Print job requires attention");

     }
     public void printJobFailed(PrintJobEvent e) {
            JOptionPane.showMessageDialog(null,
                        "Print job failed "+e.toString(),
                        "Failure",
                  JOptionPane.ERROR_MESSAGE);
            System.exit(1);
    }
  });

try {
      job.print(doc, attributes);
     }
     catch(PrintException e) {
         System.out.println(e);
         System.exit(1);
     }
Technical Details
Message-ID: <42c80653$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!pd7cy2so!shaw.ca!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <42c56d8b@newsgroups.borland.com> <42c7703b@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,a8485db9e079ecd9
X-Google-Attributes: gidf74b3,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Sun, 03 Jul 2005 11:38:12 -0400
From: Magnus <magnus_bjorkman@iwon.com>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.2) Gecko/20040804 Netscape/7.2 (ax)
X-Accept-Language: en-us, en, sv
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language
Subject: Re: Simplest way to print reports in java1.4?
References: <42c56d8b@newsgroups.borland.com> <42c7703b@newsgroups.borland.com>
In-Reply-To: <42c7703b@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 69.69.78.87
Message-ID: <42c80653$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1120405075 69.69.78.87 (3 Jul 2005 08:37:55 -0700)
Lines: 80
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!pd7cy2so!shaw.ca!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail