itext and dotmatrix printers

3.2k Views Asked by At

I have a JSP page which is simply getting parameters, querying database, generating invoice PDF, and sending silently to the default printer. I use itext library.

The invoice has to be printed on a dot-matrix printer with continuous paper.

Each invoice page size is sized a5 landscape.

If i choose the pagesize as a5, code produces a PDF as its seen on here

a5 portrait

when its printed, it prints one page and leave another page blank. User has to scroll the paper back manually.

If i choose the pagesize as a5 landscape (a5.rotate()), code produces a PDF as its seen on here, which is better.

a5 landscape

but when this is printed on paper, it starts to print the page vertically, treating the printer has a a4 paper tray.

Seems to me, I need to define my printer as a dotmatrix printer with continuous paper.

The code i am using is :

    Document document = new Document(PageSize.A5,0,0,0,0);
try {   
 PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
 writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
document.open();


StringBuffer javascript = new StringBuffer();
 javascript.append("this.print({bUI: false, bSilent: true, bShrinkToFit: true});");
 PdfAction pdfAction= PdfAction.javaScript(javascript.toString(), writer);
 writer.addJavaScript(pdfAction);
 writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
 PdfPTable table = new PdfPTable(3); // 3 columns.
 table.setWidthPercentage(100);

 PdfPCell cell1 = new PdfPCell(new Paragraph(""));
 PdfPCell cell2 = new PdfPCell(new Paragraph(""));
 PdfPCell cell3 = new PdfPCell(new Paragraph(MakbuzNo,FontFactory.getFont(FontFactory.COURIER,9)));
 cell3.setLeading(16f, 0f);
 cell1.setBorder(Rectangle.NO_BORDER);
 cell2.setBorder(Rectangle.NO_BORDER);
 cell3.setBorder(Rectangle.NO_BORDER);
 
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell1);
table.addCell(cell2);    
table.addCell(cell3);    

cell1 = new PdfPCell(new Paragraph(""));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(Duzenleyen,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);

cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);    
table.addCell(cell3);    


cell1 = new PdfPCell(new Paragraph(MSISDN,FontFactory.getFont(FontFactory.COURIER,9)));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(DuzenlemeSaati,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setLeading(16f, 0f);
cell3.setLeading(16f, 0f);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);

cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);    
table.addCell(cell3);   

cell1 = new PdfPCell(new Paragraph(""));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(DuzenlemeTarihi,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);

cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);    
table.addCell(cell3);

cell1 = new PdfPCell(new Paragraph(" "));
cell2 = new PdfPCell(new Paragraph(" "));
cell3 = new PdfPCell(new Paragraph(" "));
cell1.setLeading(45f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);

table.addCell(cell1);
table.addCell(cell2);    
table.addCell(cell3);

cell1 = new PdfPCell(new Paragraph(izahat,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setColspan(3);
cell1.setBorder(Rectangle.NO_BORDER);  
table.addCell(cell1);

cell1 = new PdfPCell(new Paragraph(" "));
cell2 = new PdfPCell(new Paragraph(" "));
cell3 = new PdfPCell(new Paragraph(" "));
cell1.setLeading(75f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);

table.addCell(cell1);
table.addCell(cell2);    
table.addCell(cell3);

cell1 = new PdfPCell(new Paragraph(kopyayazi,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setColspan(3);
cell1.setBorder(Rectangle.NO_BORDER);  
table.addCell(cell1);

cell1 = new PdfPCell(new Paragraph(TutarYazi,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setColspan(2);
cell3 = new PdfPCell(new Paragraph(ToplamTutar,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setLeading(16f, 0f);
cell3.setLeading(16f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);

table.addCell(cell1);
table.addCell(cell3);
document.add(table);
document.newPage();
    } catch (DocumentException de) {
        de.printStackTrace();
        System.err.println("document: " + de.getMessage());
    }
    
    document.close();

So how to use dotmatrix printer with continous paper and stop paper scroller when character on page is already printed ?

3

There are 3 best solutions below

0
On

After long researches, I could not find any way to print on clients' dot-matrix printer without any extra installation.

Most efficient way is to have a signed applet. Java installation required at clients' side.

2
On

Welcome to hell :-) Here are all the daemons that have their tiny claws in your process:

  • The PDF file itself has an orientation
  • The PDF viewer/printer might try to rotate the pages to fit the page
  • The printer driver might do it, too
  • If you had a laser printer, that might rotate the page as well.

The most "simple" solution is to render the PDF in a BufferedImage, save that as a pixel image (say, PNG) and print that. This will allow you to make sure the orientation is what you want.

Also check the settings of the printer driver: Some of them send a "Form Feed" character after printing the file which would result in an empty page if you fill the whole a5 page.

1
On

I have experienced the similar problem with the Jasper reports printing(with continuous paper), After fiddling with the code for days I found a solution I think will work for you.

I think you have to do is to generate a PDF file as output and simply get the AdobeReader exe to do the printing, you can use

AcroRd32.exe /N /T PdfFile PrinterName

parameters to achieve this.