1

Currently I am using this code but its throwing PrintJobFlavorException. This is my code help me out fixing this one:

public class PJUtil {
    public static void main(String[] args) throws Exception {
        DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
        Writer output = null;
        String text = "printing in pdfPrinting in Java ";
        File file = new File("C:\\CMPSup_AL_.PDF");
        output = new BufferedWriter(new FileWriter(file));
        output.write(text);
        output.close();
        InputStream is = new BufferedInputStream(new FileInputStream(file));
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        DocPrintJob job = service.createPrintJob();
        Doc doc = new SimpleDoc(is, flavor, null);
        PrintJobWatcher pjDone = new PrintJobWatcher(job);
        job.print(doc, null);
        pjDone.waitForDone();
        is.close();
    }
}

and exception is

Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
    at sun.print.Win32PrintJob.print(Win32PrintJob.java:327)
    at Collections.PrinterJobUtil.main(PrinterJobUtil.java:89)
2
  • 1
    what makes you think that that text string is a valid pdf document? (hint, it's not). Commented Oct 29, 2011 at 19:47
  • You can't just call a file a PDF, it needs to be an actual PDF-format file. Don't know if that's what the issue is, but it certainly won't help. Commented Oct 29, 2011 at 19:48

5 Answers 5

2

your printer may not support text based representation. Have a look at this article java printing, specially page 5.

Sign up to request clarification or add additional context in comments.

Comments

2

As other have pointed out, you can't just create a file called PDF and print it. If you need to generate PDF then you might take a look at itext.

Comments

0

Just to give you another option for creating PDF files. Try using Apache's PDFBox and take a look at the cookbook. The HelloWorld example shows you how to create a simple PDF document like the one you were trying to create in your sample code.

Comments

0

Also take a look on Jasper Reports http://community.jaspersoft.com/project/jasperreports-library

Comments

0

Change DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF to *DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE*.

E Pavan Varma

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.