3

Guys I am stuck in printing a pdf using java. The code that I have written is below:

`

public static void main(String[] args) throws PrinterException, PrintException, IOException{
        DocFlavor docflavor = new DocFlavor.INPUT_STREAM ("application/octet-stream");
    //  DocFlavor docflavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE;
/*      DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.;*/
        PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
        DocFlavor[] docF = printService.getSupportedDocFlavors();
        for(int i = 0; i<docF.length;i++){
            System.out.println(docF[i]);
        }
        FileInputStream fis = new FileInputStream("pathofpdffile");



        Doc pdfDoc = new SimpleDoc(fis, docflavor, null);


        DocPrintJob printJob = printService.createPrintJob();
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

        aset.add(new Copies(1));
        aset.add(Sides.ONE_SIDED);
        printJob.print(pdfDoc,aset);

        fis.close();

}`

The above code intiate the printing activity but the problem is that I get only encode character in print. I am not able to get my exact file.

Second if I change the DocFlavor to SERVICE_FORMATTED.PAGEABLE ,it throws an error

java.lang.IllegalArgumentException: data is not of declared type
at javax.print.SimpleDoc.<init>(Unknown Source)
at com.calculator.main.PrintingTest.main(PrintingTest.java:42)

Third IF I change the DocFlavor to INPUT_STREAM.PDF, it throws as error

`Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
at sun.print.Win32PrintJob.print(Unknown Source)
at com.calculator.main.PrintingTest.main(PrintingTest.java:49)`

All this I am trying on a network printer. Any help would gr8..

5
  • Please see stackoverflow.com/questions/2478890/… Commented Jan 21, 2014 at 10:13
  • Which framework do you use to parse and print the PDF? Commented Jan 21, 2014 at 10:41
  • @RaviH : I am not prefering to use any external jar for this. I too had a walk through the link posted by you. any further help !!! Commented Jan 21, 2014 at 12:02
  • @AaronDigulla : framework I didn't get u.. :( . It is plain java code. Commented Jan 21, 2014 at 12:07
  • Does it work when you print on a different printer? Commented Jan 21, 2014 at 15:36

2 Answers 2

13

Just change your code to use AUTO_SENSE as shown below.

InputStream inputStream = new FileInputStream("C://test.pdf");
Doc doc = new SimpleDoc(inputStream, DocFlavor.INPUT_STREAM.AUTOSENSE,null);
Sign up to request clarification or add additional context in comments.

Comments

1

My first guess would be that new DocFlavor.INPUT_STREAM ("application/octet-stream") isn't what you want.

You may want to try the code from this answer: https://stackoverflow.com/a/18962278/34088

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.