Print gif using java on a 4x6" paper - java

What is the best way in Java to print a gif given as byte[] or ByteArrayInputStream on a paper with a size of 4x6 inches?
This:
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new MediaSize(4, 6, Size2DSyntax.INCH));
aset.add(new Copies(1));
PrintService[] pservices =
PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, aset);
DocPrintJob printJob = pservices[0].createPrintJob();
Doc doc = new SimpleDoc(sap.getGraphicImageBytes(), DocFlavor.INPUT_STREAM.GIF, null);
printJob.print(doc, aset);
does not work because the MediaSize is not a PrintRequestAttribute. This should be almost the same as in Package javax.print Description

I found a way to solve my problem
PageFormat format = new PageFormat();
format.setOrientation(PageFormat.REVERSE_LANDSCAPE);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.REVERSE_LANDSCAPE);
aset.add(MediaSizeName.JAPANESE_POSTCARD);
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintable(new ImagePrintable(sap.getGraphicImage()));
printerJob.defaultPage(format);
printerJob.print(aset);
The trick was using japanese postcard as media size.

Related

Programmatically print .doc using a virtual printer

Is there a way to print a .doc file (this specific case) using a virtual printer without showing the print dialog?
I have tried using javax.print library and I can print(convert in this case) images and text files, using for example:
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));
aset.add(Sides.ONE_SIDED);
DocPrintJob pj = selectedPrinter.createPrintJob();
FileInputStream fis = new FileInputStream(filePath);
Doc doc = new SimpleDoc(fis, flavor, null);
pj.print(doc, aset);
Trying following this logic with several other options with .doc files always result in a corrupted pdf.

java printing : incorrect paper size

im trying to print a pdf in A4 size but the output varies from the expected one.
this is what it looks like
but it supposed to be like this
both images are same resolution
this is the code that generates this output.
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintService(service);
PageFormat pf = new PageFormat();
Paper paper = new Paper();
paper.setSize(595, 842); // a4 in px
paper.setImageableArea(0, 0, 595, 842);
pf.setPaper(paper);
Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
pj.setPageable(book);
pj.print();
basically its just shrunk. what should i do to fix this?
and by the way, im not using a real printer. im using a virtual printer that takes a print request and outputs a pdf.
i have found a solution to this. i used javax.print library instead of java.awt.print.
File file = new File("path/to/pdf");
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
attr.add(MediaSizeName.ISO_A4);
FileInputStream fis = new FileInputStream(file);
Doc doc = new SimpleDoc(fis, flavor, null);
DocPrintJob job = printService.createPrintJob();
job.print(doc, attr);
fis.close();
now it gets printed correctly.

javax print not actually printing anything

Desperately trying to print a document using the following code. A document is being added to the printer queue but nothing actually comes out of the printer.
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService printService[] = PrintServiceLookup.lookupPrintServices(null, null);
InputStream stream = new ByteArrayInputStream("hello world!\f".getBytes("UTF8"));
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
DocPrintJob job = service.createPrintJob();
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(stream, flavor, das);
PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(doc, pras);
pjw.waitForDone();
The print dialog I get shows the correct default printer (as below), plus the list of other available printers:
If I change the printer to "Microsoft print to PDF", I get an empty (0kb) pdf. Interestingly, if I tick "Print to File" I get a prn file with the correct contents ("hello world!FF").
What am I missing?

Thermal Printing in java using DocFlavor

I am new to DocFlavor java .
Anyone please help to start like I have simple text file and want to print on thermal printer ,which will be suitable DocFlavor type ?
It depends...If you want to print from a txt file then AUTO_SENSE or PostScript are ussually good ideas.
DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
or
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Here is a Snippet as an example.
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
FileInputStream in = new FileInputStream(new File("C:*PATH_TO_STRING_HERE"));
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(in, flavor, null);
DocPrintJob job = service.createPrintJob();
PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(doc, pras);
I got this code when i was trying to figure out how to use my thermal receipt printer...If you want a more thourough explanation. IF you need a more in depth answer...
Then go here !
NOTE if you need to print out charactesr such as ä, ö or å then this will propably not work well as they will be obscured... I do not know how to print out special characters.
Sincerly...
//Orville
For more info on docflavors..
Go here !

How to print PDFs automatically

We have a number of systems that produce PDFs that need to be printed. These are stored on a central document store. A message then goes onto a JMS queue that the document needs printing. A service, written in Java , picks these up and then invokes a native command. This is to call Adobe Reader with the /t flag. This causes the document to print without the GUI showing.
However since a power cut this no longer works. In the interim we are having to manually print hundreds of documents. We originally tried using Java printing, but the PDFs came out malformed.
What is a better solution to this?
This code only works if the printer supports PDF. Otherwise you need to use a native printer or a Java library. There is a blog article on this at http://pdf.jpedal.org/java-pdf-blog/bid/25566/Printing-PDF-files-from-Java
Show us the code. I remember printing PDF with no issues using Java Print API. Below is the code, might need some modification, but should run as it is,
InputStream in = new FileInputStream(file);
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
// find the printing service
AttributeSet attributeSet = new HashAttributeSet();
attributeSet.add(new PrinterName("FX", null));
attributeSet.add(new Copies(1));
PrintService[] services = PrintServiceLookup.lookupPrintServices(
DocFlavor.INPUT_STREAM.PDF, attributeSet);
//create document
Doc doc = new SimpleDoc(in, flavor, null);
// create the print job
PrintService service = services[0];
DocPrintJob job = service.createPrintJob();
// monitor print job events
PrintJobWatcher watcher = new PrintJobWatcher(job);
System.out.println("Printing...");
job.print(doc, null);
// wait for the job to be done
watcher.waitForDone();
System.out.println("Job Completed!!");
Note:
Flavor is not needed in 2 places, 1 place should be enough. You find that out.
PrintJobWatcher is a nested class, to add a PrintJobListener.
Ever since Java 1.5, Sun developed a pdf renderer library for handling PDF. Now this one is left to Swing Labs. And not sure whether this one would be added into future java APIs.
http://java.net/projects/pdf-renderer/
It is used to view or print pdf files. to print pdf files, you can call this libray. Here is some part of the code.
File input = new File(docName);
FileInputStream fis = new FileInputStream(input);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
PDFFile curFile=null;
PDFPrintPage pages=null;
curFile = new PDFFile(bb); // Create PDF Print Page
pages = new PDFPrintPage(curFile);
PrinterJob pjob = PrinterJob.getPrinterJob();
PrintService[] services = pjob.lookupPrintServices();
for(PrintService ps:services){
String pName = ps.getName();
if(pName.equalsIgnoreCase("PrinterName")){
pjob.setPrintService(ps);
System.out.println(pName);
break;
}
}
pjob.setJobName(docName);
Book book = new Book();
PageFormat pformat = PrinterJob.getPrinterJob().defaultPage();
book.append(pages, pformat, curFile.getNumPages());
pjob.setPageable(book);
// print
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
// Print it
pjob.print(aset);
Try using ICEpdf. Here's an example from documentation page:
Document pdf = new Document();
pdf.setFile(filePath);
// create a new print helper with a specified paper size and print
// quality
PrintHelper printHelper = new PrintHelper(null, pdf.getPageTree(),
0f, MediaSizeName.NA_LEGAL, PrintQuality.DRAFT);
// try and print pages 1 - 10, 1 copy, scale to fit paper.
printHelper.setupPrintService(selectedService, 0, 0, 1, true);
// print the document
printHelper.print();
You can use Apache PDFBox. Examples:
a) Printing PDF as Pageable
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = printService.createPrintJob();
PDDocument pdDocument = PDDocument.load(new File("doc.pdf"));
PDFPageable pdfPageable = new PDFPageable(pdDocument);
SimpleDoc doc = new SimpleDoc(pdfPageable, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
printJob.print(doc, null);
b) Printing PDF as Printable
This option has advantage that you can control page dimensions, margins, etc. by modifying pageFormat variable.
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = printService.createPrintJob();
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
PDDocument pdDocument = PDDocument.load(new File("doc.pdf"));
PDFPrintable pdfPrintable = new PDFPrintable(pdDocument);
Book book = new Book();
book.append(pdfPrintable, pageFormat);
SimpleDoc doc = new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
printJob.print(doc, null);

Categories

Resources