Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7798617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:03:30+00:00 2026-06-02T00:03:30+00:00

I have a problem with printing of several html documents using java. I need

  • 0

I have a problem with printing of several html documents using java. I need application which show ONE print dialog for all printable files (A file count could be big). First i tried to do this using standart java method:

if (Desktop.isDesktopSupported()){
        Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Desktop.Action.PRINT))
        {
            try {
                File html1 = new File("c://file1.html");
                File html2 = new File("c://file2.html");
                desktop.print(html1);
                desktop.print(html2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}

But i saw a one dialog for each printable file, it doesn’t suit me. Then i tried to use a Java Printing API, but it turned out that my printer doesn’t support DocFlavor of html files, my list of supported DocFlavor look like this:

image/gif; class="[B"
image/gif; class="java.io.InputStream"
image/gif; class="java.net.URL"
image/jpeg; class="[B"
image/jpeg; class="java.io.InputStream"
image/jpeg; class="java.net.URL"
image/png; class="[B"
image/png; class="java.io.InputStream"
image/png; class="java.net.URL"
application/x-java-jvm-local-objectref; class="java.awt.print.Pageable"
application/x-java-jvm-local-objectref; class="java.awt.print.Printable"
application/octet-stream; class="[B"
application/octet-stream; class="java.net.URL"
application/octet-stream; class="java.io.InputStream"

Then i tried to print html file as image (png, which I drew in Paint:)), my code:

PrintRequestAttributeSet pras =
                    new HashPrintRequestAttributeSet();
            DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
            PrintRequestAttributeSet aset =
                    new HashPrintRequestAttributeSet();
            aset.add(MediaSizeName.ISO_A4);
            aset.add(new Copies(1));
            aset.add(Sides.ONE_SIDED);
            aset.add(Finishings.STAPLE);

            PrintService printService[] =
                    PrintServiceLookup.lookupPrintServices(flavor, pras);
            PrintService defaultService =
                    PrintServiceLookup.lookupDefaultPrintService();
            PrintService service = ServiceUI.printDialog(null, 200, 200,
                    printService, defaultService, flavor, pras);
            if (service != null) {
                try {
                    FileInputStream fis = new FileInputStream("c://test//test.png");
                    DocAttributeSet das = new HashDocAttributeSet();
                    Doc doc1 = new SimpleDoc(fis, flavor, das);

                    FileInputStream fis2 = new FileInputStream("c://test//test2.png");
                    DocAttributeSet das2 = new HashDocAttributeSet();
                    Doc doc2 = new SimpleDoc(fis2, flavor, das2);

                    DocPrintJob job1 = service.createPrintJob();
                    DocPrintJob job2 = service.createPrintJob();

                    try {
                        job1.print(doc1, pras);
                        job2.print(doc2, pras);
                    } catch (PrintException e) {
                        e.printStackTrace();
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

It works perfectly, but convertation from html to image isn’t simple problem. I tried to use swing components, implements Printable interface and use Cobra library, but it demands the display of document on the form, it’s not nessesary for me, because i need a printing in “silent” mode, without opening a docs.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:03:31+00:00Added an answer on June 2, 2026 at 12:03 am

    In the end, I choose this way:

    1. Convert html files to pdf.

    2. Print pdf files using PDFBox in silent print mode.


    List<PDDocument> docs = new ArrayList<PDDocument>();
    try {
        docs.add(PDDocument.load("c://test/test.pdf"));
        docs.add(PDDocument.load("c://test/test2.pdf"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    try {
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.ISO_A4);
        aset.add(new Copies(1));
        aset.add(Sides.ONE_SIDED);
        aset.add(Finishings.STAPLE);
    
        PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
        PrintService service = ServiceUI.printDialog(null, 200, 200,
                printService, defaultService, flavor, pras);
    
        if (service != null && !docs.isEmpty()) {
            for (PDDocument doc : docs) {
                PrinterJob printJob = PrinterJob.getPrinterJob();
                printJob.setPrintService(service);
                doc.silentPrint(printJob);
            }
        }
    } catch (PrinterException e) {
        e.printStackTrace();
    } finally {
        for (PDDocument doc : docs) {
            if (doc != null) {
                try {
                    doc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to print using php_printer.dll. I have no problem printing a simple text
I have one file called test.ai and I need to print it several times,
I have a script, which by using several querystring variables provides an image. I
I have several modules (mainly C) that need to be redesigned (using C++). Currently,
HI, I have a problem printing the named route's URL in production application. In
I have an application that uses disabled JTextFields in several places which are intended
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with variable printing inside javascript. variable htmlString printing not working: document.write(htmlString)
I have a problem with printing a chart. I made a web page contains
Background The application I am working with has several COM DLLs. One of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.