2

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 ?

Syscall
  • 19,327
  • 10
  • 37
  • 52
ndonmez
  • 21
  • 1
  • 5

3 Answers3

1

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Hello Aaron. thanks for the prompt suggestion. the idea behind sending to printer via PDF was, the system is a web application. so; to send the data to clients printer is done by adding javascript to PDF file, and when its opened, it prints directly. Unfortunately, having the invoice to be converted to graphic on server side wont help. – ndonmez Nov 28 '11 at 11:27
  • In that case, you need to fix the client's printer setup. Make sure the printer driver doesn't send form feeds after printing the job. As for the page rotation, there is little you can do: If the client uses the same PDF viewer to print other PDFs to a different printer, the orientation can change outside your control. – Aaron Digulla Nov 28 '11 at 16:31
0

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.

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43
  • Hello. thanks for your reply, as i have commented on the previous answer, its a web application, and the document needs to be printed on client side, so cant use such a commandline on a JSP page. – ndonmez Nov 28 '11 at 11:35
0

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.

ndonmez
  • 21
  • 1
  • 5