Código PHP:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class testPackagePrint implements Printable {
JTable dataTable;
protected int m_maxNumPage = 1;
protected JLabel strTitle;
public testPackagePrint()
{
String rows[][] = {
{"1", "1"},
{"2", "ll"},
{"3", "ll"},
{"4", "ll"},
{"5", "mm"},
{"6", "nmm"},
{"7", "mmC"},
{"8", "mm"},
{"9", "mm"},
{"10", "mm"},
{"11", "mm"},
{"12", "mm"},
{"13", "mm"},
{"14", "mm"},
{"15", "mm"},
{"16", "mm"},
{"17", "mm"},
{"18", "mm"},
{"19", "ni - \u4E8C"},
{"20", "san - \u4E09"},
{"30", "ni - \u4E8C"},
{"40", "san - \u4E09"},
{"50", "ni - \u4E8C"},
{"60", "ni - \u4E8C"},
{"61", "san - \u4E09"},
{"62", "ni - \u4E8C"},
{"63", "ni - \u4E8C"},
{"64", "san - \u4E09"},
{"65", "ni - \u4E8C"},
{"two", "ni - \u4E8C"},
{"three", "san - \u4E09"},
{"two", "ni - \u4E8C"},
{"two", "ni - \u4E8C"},
{"three", "san - \u4E09"},
{"two", "ni - \u4E8C"},
{"two", "ni - \u4E8C"},
{"three", "san - \u4E09"},
{"two", "ni - \u4E8C"},
{"two", "ni - \u4E8C"},
{"three", "san - \u4E09"},
{"two", "ni - \u4E8C"},
{"three", "san - \u4E09"}
};
String headers[] = {"Producto", "Cant"};
JFrame ventana = new JFrame("Impresion de factura");
strTitle= new JLabel();
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contenedor = ventana.getContentPane();
strTitle.setText("Preview");
dataTable = new JTable(rows, headers);
JScrollPane jsp = new JScrollPane(dataTable);
contenedor.add(jsp, BorderLayout.CENTER);
contenedor.add(strTitle, BorderLayout.SOUTH);
ventana.pack();
ventana.setVisible(true);
new PrintPreview(testPackagePrint.this, " preview");
//
}
public static void main(String[] args)
{
new testPackagePrint();
}
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
{
if (pageIndex >= m_maxNumPage)
{
return NO_SUCH_PAGE;
}
PrinterJob printJob = PrinterJob.getPrinterJob();
// Se asigna el tamaño del papel a A4.
Graphics2D g = (Graphics2D) graphics;
pageFormat = printJob.defaultPage();
Paper paper = pageFormat.getPaper();
g.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY());
paper.setSize(306, 396);
paper.setImageableArea(1.0, 1.0, 300, 390);
pageFormat.setPaper(paper);
pageFormat.setOrientation(PageFormat.PORTRAIT);
int wPage = 0;
int hPage = 0;
if (pageFormat.getOrientation() == pageFormat.PORTRAIT)
{
wPage = (int)pageFormat.getImageableWidth()-5;
hPage = (int)pageFormat.getImageableHeight()-5;
}
else
{
wPage = (int)pageFormat.getImageableWidth();
wPage += wPage/2;
hPage = (int)pageFormat.getImageableHeight();
g.setClip(0,0,wPage,hPage);
}
Font fontTitle = new Font("Arial", Font.ITALIC, 8);
g.setFont(fontTitle);
int y = 0;
g.setFont(strTitle.getFont());
g.setColor(Color.black);
Font fn = g.getFont();
FontMetrics fm = g.getFontMetrics();
y += fm.getAscent();
g.drawRect(5, 5, (int)pageFormat.getImageableWidth()-10, (int)pageFormat.getImageableHeight()-10);
g.drawString(strTitle.getText(), 8, y+10);//Titulo
g.drawString("JUEGUETERIA \" *** JOSE MANUEL *** \" ", 8, y+20);
g.drawString("Dir: Ituzaingó 368", 8, y+30);
g.drawString("CP: 4400", 8, y+40);
g.drawString("Telf: 0387-4390362", 8, y+50);
g.drawString("Salta - Argentina", 8, y+60);
g.drawString("FACTURA", 8, y+70);
g.drawString("Total Facturado:", 0+80/*centro*/, y + hPage- 35/*desde abajo*/);
g.drawString("Página - " + (pageIndex+1), 0+80/*centro*/, y + hPage- 25/*desde abajo*/);
y += 75; // space between title and table headers
Font headerFont = dataTable.getFont().deriveFont(Font.BOLD);
g.setFont(headerFont);
fm = g.getFontMetrics();
TableColumnModel colModel = dataTable.getColumnModel();
int nColumns = colModel.getColumnCount();
int x[] = new int[nColumns];
x[0] = (int)paper.getImageableX()+10; //Escala X
int x1=x[0];
int h = fm.getAscent();
y += h; // add ascent of header font because of baseline
// positioning (see figure 2.10)
int nRow, nCol;
for (nCol=0; nCol<nColumns; nCol++) {
TableColumn tk = colModel.getColumn(nCol);
int width;
if (nCol>0)
{
width = tk.getWidth()/3;//Ancho de las columnas
}
else
{
width = 80;//Ancho de las columnas
}
if (x[nCol] + width > wPage)
{
nColumns = nCol;
break;
}
if (nCol+1<nColumns)
x[nCol+1] = x[nCol] + width;
String title = (String)tk.getIdentifier();
g.drawString(title, x[nCol], y);
}
g.setFont(dataTable.getFont());
fm = g.getFontMetrics();
int header = y;
h = fm.getHeight();
int rowH = Math.max((int)(h*1.5), 10);
int rowPerPage = (hPage-header)/rowH;
m_maxNumPage = Math.max((int)Math.ceil(dataTable.getRowCount()/
(double)rowPerPage), 1);
TableModel tblModel = dataTable.getModel();
int iniRow = pageIndex*rowPerPage;
int endRow = Math.min(dataTable.getRowCount(),
iniRow+rowPerPage);
for (nRow=iniRow; nRow<endRow; nRow++)
{
y += h;
for (nCol=0; nCol<nColumns; nCol++) {
int col = dataTable.getColumnModel().getColumn(nCol).getModelIndex();
Object obj = dataTable.getValueAt(nRow, col);
String str = obj.toString();
if (obj instanceof ColorData)
g.setColor(((ColorData)obj).m_color);
else
g.setColor(Color.black);
g.drawString(str, x[nCol], y);
}
}
System.gc();
return PAGE_EXISTS;
}
}
en el segundo post esta la clase donde se genera la vista previa por favor aumenten mas filas a los datos de la tabla para que genere mas paginas
gracias de antemano espero me puedan ayudar.