Print PDF as byte array in JAVA

convert byte array to pdf in java example
convert pdf to byte array java
pdf byte array example
convert xssfworkbook to byte array in java
convert byte array to excel file java
convert byte array to multipartfile in java
convert audio file to byte array android
convert pdf to byte array in android

I need to print a pdf file for my printer. With this code I have converted my pdf to bytearray, but I am stuck and not know how to send it to the printer. Someone can help me?

    File file = new File("java.pdf");

    FileInputStream fis = new FileInputStream(file);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];

    try {
        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum); //no doubt here is 0
            //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
            System.out.println("read " + readNum + " bytes,");
        }
    } catch (IOException ex) {
        System.out.println("ERROR!");
    }
    byte[] bytes = bos.toByteArray();

Thank you in advance.

Another approach is to send the pdf file using intent and here is an example

Sample code :

Intent prnIntent = new Intent(Intent.ACTION_SEND);
prnIntent.putExtra(Intent.EXTRA_STREAM, uri);

prnIntent.setType("application/pdf");


startActivity(Intent.createChooser(prnIntent , "Send pdf using:"));

With this approach there is no need to use buffers but you send pdf file directly to printer!

PDF to byte array and vice versa, Java 7 introduced Files.readAllBytes() , which can read a PDF into a byte[] like so​: import java. Java 8Object Oriented ProgrammingProgramming. You can read data from a PDF file using the read() method of the FileInputStream class this method requires a byte array as a parameter.

Steps to follow:

  1. Find default printer service in your environment.
  2. Define the document flavor, for PDF, to use for print.
  3. Prepare a document to print from byte array.
  4. Execute the print job.

Example code snippet:

byte[] bytes = bos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream( bytes );

// First identify the default print service on the system  
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();  

// prepare the flvaor you are intended to print  
DocFlavor docFlavor = DocFlavor.BYTE_ARRAY.PDF;  

// prepare the print job
DocPrintJob printJob = printService.createPrintJob();  

// prepare the document, with default attributes, ready to print  
Doc docToPrint = new SimpleDoc( bais, docFlavor, null );  

// now send the doc to print job, with no attributes to print
printJob.print( docToPrint, null );

Java: convert a file to a byte array, then convert byte array to a file., To convert a file to byte array, ByteArrayOutputStream class is used. This class File file = new File("java.pdf"); FileInputStream fis = new FileInputStream(file);  I need to print a pdf file for my printer. With this code I have converted my pdf to bytearray, but I am stuck and not know how to send it to the printer. Someone can help me? File file = new

Using a ByteArrayInputStream throws java.lang.IllegalArgumentException, but a regular byte array worked for me. Modifying the example of Ravinder Reddy:

    // Get default print service
    PrintService printService = PrintServiceLookup.lookupDefaultPrintService();

    // Create a print job
    DocPrintJob printJob = printService.createPrintJob();

    // Optional fancy listener
    printJob.addPrintJobListener(new PrintJobAdapter() {
        @Override
        public void printJobCompleted(PrintJobEvent pje) {
            System.out.println("PDF printing completed");
            super.printJobCompleted(pje);
        }

        @Override
        public void printJobFailed(PrintJobEvent pje) {
            System.out.println("PDF printing failed");
            super.printJobFailed(pje);
        }
    });

    // Check the PDF file and get a byte array
    File pdfFile = new File("path/to/pdf");
    if (pdfFile.exists() && pdfFile.isFile()) {
        byte[] PDFByteArray = Files.readAllBytes(FileSystems.getDefault().getPath(pdfFile.getAbsolutePath()));

        // Create a doc and print it
        Doc doc = new SimpleDoc(PDFByteArray, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
        printJob.print(doc, null);
    }

How to convert a PDF to byte array in Java?, You can read data from a PDF file using the read() method of the FileInputStream class this method requires a byte array as a parameter. If you want to print the bytes as chars you can use the String constructor. byte[] bytes = new byte[] { -1, -128, 1, 127 }; System.out.println(new String(bytes, 0));

Auto or Silent Print a PDF byte Array using default Printer by , Auto or Silent Print a PDF byte Array using default Printer by disabling Print it's just using Adobe Acrobat as a program) and Java to print a PDF to the default  Assuming your byte array is called buf: System.out.println(Arrays.toString(buf)); Edit: It sounds like what you really want to do is write your bytes to stdout, not print them.

android Print PDF as byte array in JAVA?, toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream( bytes ); // First identify PDF; // prepare the print job DocPrintJob printJob = printService. Sum triangle from an array in C programming; Importance of XOR operator in Java? How to convert an OutputStream to a Writer in Java? How to convert a String to an InputStream object in Java? How to find the unicode category for a given character in Java? How to print a formatted text using printf() method in Java?

Print a PDF loaded in memory (byte array) without saving it as a file , There are different constructors in the PDFPrint class to load documents, one of them takes a Java InputStream to read the PDF content from. If  How to print array in Java. Java array is a data structure where we can store the elements of the same data type. The elements of an array are stored in a contiguous memory location.

Comments
  • See my answer here. The job can be done with sockets
  • Thank you for your answer, but there are other possibility like not be sockets?
  • Yes, but I have converted my pdf to bytearray, in your example, where I send my bytearray to printer?
  • With this example you send a pdf file. So, you have to do your work first as bytearray then convert to pdf file and then you send it to printer with this solution.
  • How will an Android approach help in a standard java application? How do Intents fit in here?
  • You are right Christian it's an android solution only but I thought that this question was for android. So, the tag android I think is missing from the question.
  • It throws a "data is not of declared type" exception
  • @whizzzkey: ??? which data??? where is it???? post your query with proper details on where you are struck with...
  • "which data???" - just simple pdf doc, "where is it???? " - on line Doc docToPrint = new SimpleDoc( bais, docFlavor, null );. I do everything as you posted, and I have a similar code like TS for getting a bytearray from my pdf file.
  • As I suggested, you better post your code, exception stacktrace with your query as a new question. Just through comments it is hard to identify the problem and give a solution.