3

我正在使用它直接向打印机发送一个 htlm 文件,它说无效的味道,这意味着打印机不支持这些格式。任何人都有这样做的想法..

/**
 * @param args
 */
public static void main(String[] args) {
    // Input the file
    FileInputStream textStream = null; 
    try { 
        textStream = new FileInputStream("./some.html"); 
    } catch (FileNotFoundException ffne) { 
    } 
    if (textStream == null) { 
        return; 
    } 
    // Set the document type
    DocFlavor myFormat  = DocFlavor.INPUT_STREAM.TEXT_HTML_HOST;

    // Create a Doc
    Doc myDoc = new SimpleDoc(textStream, myFormat , null);         
    // Build a set of attributes
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
    aset.add(new Copies(1)); 
    //aset.add(MediaSize.NA.LEGAL);
    aset.add(Sides.ONE_SIDED); 
    // discover the printers that can print the format according to the
    // instructions in the attribute set
    PrintService services = PrintServiceLookup.lookupDefaultPrintService();
        //PrintServiceLookup.lookupPrintServices(myFormat, aset);
    // Create a print job from one of the print services
    //System.out.println("====5======="+service.get);
    //if (services.length > 0) { 
    for (int i = 0; i < services.getSupportedDocFlavors().length; i++) {
        System.out.println("====getSupportedDocFlavors======="+services.getSupportedDocFlavors()[i]);
    }

        DocPrintJob job = services.createPrintJob(); 
        try { 
            job.print(myDoc, aset); 
        } catch (PrintException pe) {
            System.out.println("====PrintException======="+pe);
        } 
    //} 

}

它说

sun.print.PrintJobFlavorException: invalid flavor
4

2 回答 2

2

您正在尝试强制打印机将 HTML 文档处理(呈现)到纸上。它永远不会那样工作。当然,不支持您发送的风味。

首先,您需要将 HTML 呈现为一些图形表示,然后将其发送到打印机。没有可以渲染现代 HTML 页面的 Java 跨平台工具。但是JavaFX中有一个,我想你可以用它来处理任务。

关于打印最终图像,您可以在此处阅读:http:
//www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-Printing.html
或在此处查看代码:
http://www。 java2s.com/Code/Java/2D-Graphics-GUI/PrintanImagetoprintdirectly.htm
或者只是找到任何其他资源 - 有很多关于打印的。

于 2012-08-24T15:01:34.397 回答
0

公共类 POSPrinter {

private static final Log LOG = LogFactory.getLog(POSPrinter.class);

public POSPrinter(Long billID, String printMode) {
}

/**
 * 
 * This method prints the specified PDF to specified printer under specified
 * 
 * job name
 * 
 * 
 * 
 * @param filePath
 *            Path of PDF file
 * 
 * @param printerName
 *            Printer name
 * 
 * @param jobName
 *            Print job name
 * 
 * @throws IOException
 * 
 * @throws PrinterException
 */

public void printPDF(String filePath, String printerName, String jobName,
        Integer height, Integer width) throws IOException, PrinterException {

    FileInputStream fileInputStream = new FileInputStream(filePath);

    byte[] pdfContent = new byte[fileInputStream.available()];

    fileInputStream.read(pdfContent, 0, fileInputStream.available());

    ByteBuffer buffer = ByteBuffer.wrap(pdfContent);

    final PDFFile pdfFile = new PDFFile(buffer);

    Printable printable = new Printable() {

        public int print(Graphics graphics, PageFormat pageFormat,
                int pageIndex) throws PrinterException {

            int pagenum = pageIndex + 1;

            if ((pagenum >= 1) && (pagenum <= pdfFile.getNumPages())) {

                Graphics2D graphics2D = (Graphics2D) graphics;

                PDFPage page = pdfFile.getPage(pagenum);

                Rectangle imageArea = new Rectangle(

                (int) pageFormat.getImageableX(),

                (int) pageFormat.getImageableY(),

                (int) pageFormat.getImageableWidth(),

                (int) pageFormat.getImageableHeight());

                graphics2D.translate(0, 0);

                PDFRenderer pdfRenderer = new PDFRenderer(page, graphics2D,
                        imageArea, null, null);

                try {
                    page.waitForFinish();

                    pdfRenderer.run();

                } catch (InterruptedException exception) {
                    exception.printStackTrace();
                }

                return PAGE_EXISTS;

            } else {

                return NO_SUCH_PAGE;

            }

        }

    };

    PrinterJob printJob = PrinterJob.getPrinterJob();

    PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();

    printJob.setJobName(jobName);

    Book book = new Book();

    book.append(printable, pageFormat, pdfFile.getNumPages());

    printJob.setPageable(book);

    Paper paper = new Paper();

    paper.setSize(width, height);
    paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());

    // pageFormat
    pageFormat.setPaper(paper);

    // PrintService[] printServices = PrinterJob.lookupPrintServices();
    //
    // for (int count = 0; count < printServices.length; ++count) {
    //
    // if (printerName.equalsIgnoreCase(printServices[count].getName())) {
    //
    // printJob.setPrintService(printServices[count]);
    //
    // break;
    //
    // }
    //
    // }

    PrintService printService = PrintServiceLookup
            .lookupDefaultPrintService();
    printJob.setPrintService(printService);

    printJob.print();

}
于 2012-09-10T11:19:55.990 回答