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

  • SEARCH
  • Home
  • 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 6177929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:21:45+00:00 2026-05-24T00:21:45+00:00

After compile and run it shows no pdf printer available, How to solve this?

  • 0

After compile and run it shows “no pdf printer available”, How to solve this?

I have created a file in c:\print.pdf (using PHP TCPDF). And i am
trying to read that file in byte array, so that i can silently print
it without showing any popup of print etc.

I cant make it working, can anyone please show guide how to read a
file in Byte array? To do the following:

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
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.SimpleDoc;

public class print 
{
    private static Object pdfBytes;

    // Byte array reader 
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);
        long length = file.length();
        if (length > Integer.MAX_VALUE) {}
        byte[] bytes = new byte[(int)length];

        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }
        is.close();
        return bytes;
    }    

    // Convert Byte array to Object 
    public static Object toObject(byte[] bytes) 
    {
        Object obj = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream (bis);
            obj = ois.readObject();
        } catch (IOException ex) {

        } catch (ClassNotFoundException ex) {

        }
        return obj;
    }

    private static File fl = new File("c:\\print.pdf");    
    public static void main(String argc[])
    {
        DocFlavor flavor = DocFlavor.BYTE_ARRAY.PDF;
        PrintService[] services = 
                PrintServiceLookup.lookupPrintServices(flavor,
                null);
        //Object pdfBytes = null;
        try {
            byte[] abc = getBytesFromFile(fl);
            pdfBytes =toObject(abc);
        } catch (IOException ex) {
            Logger.getLogger(print.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (services.length>0)
        {
            DocPrintJob printJob = services[0].createPrintJob();
            Doc document = new SimpleDoc(pdfBytes,flavor,null);
            try {
                printJob.print(document, null);
            } catch (PrintException ex) {
                Logger.getLogger(print.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            System.out.println("no pdf printer available");

        }
    }

}

I tried this and it solves my silent printing: https://gist.github.com/1094612

  • 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-05-24T00:21:45+00:00Added an answer on May 24, 2026 at 12:21 am

    Here’s an example on how to read a file into a byte[]:

    // Returns the contents of the file in a byte array.
    public static byte[] getBytesFromFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);
    
        // Get the size of the file
        long length = file.length();
    
        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }
    
        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];
    
        // Read in the bytes
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }
    
        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }
    
        // Close the input stream and return bytes
        is.close();
        return bytes;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this article http://lukast.mediablog.sk/log/?p=155 I decided to use mingw on linux to compile
I'm using Ubuntu 10.04 and Qt4.6, and I've created an executable binary file on
Never thought I'd get an error in this file when I compiled, but after
When I compile and run my code, I get a bus error right after
Are there methods to obfuscate during/after compile that are automated?
I try to add Message.framework to my project frameworks directory. After that I compile
The compiler complains about this, after I activated all kind of warnings: For MyApp_Prefix.pch
After reading the Head First Design Patterns book and using a number of other
After reading this question , I was reminded of when I was taught Java
I'm trying to post-process the documentation XML file as a post-build XSLT transformation (using

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.