Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按浏览器所见打印HTML-java_Java_Html_Printing - Fatal编程技术网

按浏览器所见打印HTML-java

按浏览器所见打印HTML-java,java,html,printing,Java,Html,Printing,我正在尝试为我们公司自动打印运输标签。我有一个程序,它接收电子邮件,收集我们需要的所有信息,并将其放入HTML文件中。我之所以使用HTML,是因为我们希望包含我们的徽标,一个在线托管的JPG文件(如果您知道一种不使用HTML的方法,请分享)。然后,我将该文件设置为自动打印到默认打印机,而不显示对话框。问题是,打印出来的是文件的文本,这意味着所有html标记都会打印出来。这是我的代码(对不起,有点乱,我会在它工作时清理它) import java.util.*; 导入java.io.*; 导入ja

我正在尝试为我们公司自动打印运输标签。我有一个程序,它接收电子邮件,收集我们需要的所有信息,并将其放入HTML文件中。我之所以使用HTML,是因为我们希望包含我们的徽标,一个在线托管的JPG文件(如果您知道一种不使用HTML的方法,请分享)。然后,我将该文件设置为自动打印到默认打印机,而不显示对话框。问题是,打印出来的是文件的文本,这意味着所有html标记都会打印出来。这是我的代码(对不起,有点乱,我会在它工作时清理它)

import java.util.*;
导入java.io.*;
导入java.io.File;
导入java.lang.Object;
导入java.nio.charset.charset;
导入java.nio.file.Files;
导入java.nio.file.Path;
导入java.nio.file.path;
导入java.io.BufferedWriter;
导入java.io.FileWriter;
导入java.io.IOException;
导入javax.print.*;
导入javax.print.attribute.*;
导入java.awt.print.*;
导入java.awt.Desktop;
导入javax.print.*;
导入javax.print.attribute.*;
导入java.io.*;
导入java.awt.*;
公共类Main2
{
公共事务2()
{
路径文件=Path.get(“D:\\email\\2016.txt”);
ArrayList text=新的ArrayList(1000);
布尔停止=假;
int i=0;
字符串行1;
弦线2;
File myFile=新文件(“”);
试一试{
File printme=File.createTempFile(“printme”、“.html”、新文件(“D:\\email\\output\”);
myFile.deleteOnExit();
字符串路径名=(myFile.getAbsolutePath());
BufferedWriter bw=新的BufferedWriter(新的文件写入程序(printme));
bw.write({text-decoration:none;}A:link{color:#3366cc;text-decoration:none;}A:visted{color:#663399;text-decoration:none;}A:active{color:#cccccc;text-decoration:none;}A:Hover{text-decoration:underline;}BODY,TD,CENTER,P{字体系列:日内瓦,Verdana,Arial,Helvetica;字体大小:12px;颜色:#333333;}正文{字体系列:日内瓦,Verdana,Arial,Helvetica;字体大小:10px;颜色:#333333;}。内容{字体系列:Arial,Helvetica,无衬线;字体大小:11px;字体重量:正常;颜色:#000000;}。标题{字体系列:Helvetica,Arial,无衬线;字体大小:10px;字体重量:正常;颜色:#000000;}。标题{字体系列:Helvetica,Arial,无衬线;字体大小:14px;字体重量:粗体;颜色:#000000;}。消息{字体系列:日内瓦,Verdana,Arial,Helvetica;字体大小:9px;});
写(“
”); BufferedReader reader=Files.newBufferedReader(文件,Charset.defaultCharset()); line1=reader.readLine(); line1=reader.readLine(); line1=reader.readLine(); while(stop==false) { line1=reader.readLine(); line2=reader.readLine(); 如果((line1.length()>=22)和&(line1.substring(0,21).equals(“特殊说明:)) { 停止=真; 打破 } else如果((line2.length()>=22)和&(line2.substring(0,21).equals(“特殊说明:)) { 写(“

”); 写入(第1行); bw.写(“”); 停止=真; 打破 } 其他的 { 写入(第1行); 写(“
”); 写入(第2行); 写(“
”); } } bw.写(“”); reader.close(); bw.close(); print(printme); }捕获(例外e){ e、 printStackTrace(); } } 公共静态无效打印(文件){ PrintRequestAttributeSet pras=新的HashPrintRequestAttributeSet(); DocFlavor=DocFlavor.INPUT\u STREAM.AUTOSENSE; PrintService PrintService[]=PrintServiceLookup.lookupPrintServices(flavor,pras); PrintService defaultService=PrintServiceLookup.lookupDefaultPrintService(); //PrintService service=ServiceUI.printDialog(GraphicsEnvironment.getLocalGraphicsEnvironment().GetDefaultScreenDevice().getDefaultConfiguration(),200, //printService、defaultService、flavor、pras); //if(服务!=null){ DocPrintJob作业=defaultService.createPrintJob(); 试一试{ FileInputStream fis=新的FileInputStream(文件); DocAttributeSet das=新的HashDocAttributeSet(); 文档=新SimpleDoc(fis、flavor、das); 打印作业(文件、pras); //睡眠(10000); }捕获(ioe异常ioe){ ioe.printStackTrace(); } 捕获(例外e){ } } }

请帮我解决这个问题。

您似乎想要的是解析HTML,然后像浏览器一样进行布局和渲染。这是一项从零开始就要完成的相当艰巨的任务


您可能想要的是找到一个现有的布局引擎,您可以将HTML交给它,然后它将返回一个呈现的文档/图像/任何东西。

您似乎想要的是解析HTML,然后像浏览器一样进行布局和呈现。这是一个从头开始就要完成的相当艰巨的任务


您可能希望找到一个现有的布局引擎,您可以将HTML交给它,然后它将返回呈现的文档/图像/任何内容。

您需要首先呈现HTML。 您可以使用JEditorPane。只需将内容类型设置为“text/html”,设置您的内容。现在,您可以获得呈现内容的快照,如下所示
import java.util.*;
import java.io.*;
import java.io.File;
import java.lang.Object;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.print.*;
import javax.print.attribute.*;
import java.awt.print.*;
import java.awt.Desktop;
import javax.print.*;
import javax.print.attribute.*;
import java.io.*;
import java.awt.*;
public class Main2
{
    public Main2()
    {
        Path file = Paths.get("D:\\email\\2016.txt");
        ArrayList<String> text = new ArrayList<String>(1000);
        boolean stop = false;
        int i = 0;
        String line1;
        String line2;
        File myFile = new File("");
        try{
            File printme = File.createTempFile("printme", ".html", new     File("D:\\email\\output\\"));
            myFile.deleteOnExit();
            String pathName = (myFile.getAbsolutePath());

            BufferedWriter bw = new BufferedWriter(new FileWriter(printme));  
            bw.write("<html> <head> <meta name=vs_targetSchema     content=\"http://schemas.microsoft.com/intellisense/ie5\"><style     type=\"text/css\"> A { text-decoration: none; } A:link { color: #3366cc; text-    decoration: none; }   A:visited { color: #663399; text-decoration: none; }        A:active { color: #cccccc; text-decoration: none; } A:Hover { text-decoration:     underline; } BODY, TD, CENTER, P { font-family: Geneva, Verdana, Arial,     Helvetica; font-size: 12px; color: #333333; }    .body { font-family: Geneva,     Verdana, Arial, Helvetica; font-size: 10px; color: #333333; }  .content { font-    family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal;     color: #000000; }   .title { font-family: Helvetica, Arial, sans-serif; font-    size: 10px; font-weight: normal; color: #000000; } .headline { font-family:     Helvetica, Arial, sans-serif; font-size: 14px; font-weight: bold; color:     #000000; }    .message { font-family: Geneva, Verdana, Arial, Helvetica; font-    size: 9px; }    </style> </head><body bgcolor=\"#ffffff\" LINK=\"#3366cc\"     VLINK=\"#3366cc\" ALINK=\"#3366cc\" LEFTMARGIN=\"0\" TOPMARGIN=\"0\">    <table     cellSpacing=1 cellPadding=3 width=\"100%\" border=\"0\" runat=\"server\">    <tr>            <td colSpan=1><IMG src=\"http://www.eshanes.com//Images/eshaneslogo.jpg\"     border=0></td>    </tr>");
            bw.write("<TD class=FormLabel vAlign=top align=left     width=\"50%\"><br><h3><b>");

            BufferedReader reader = Files.newBufferedReader(file,     Charset.defaultCharset());
            line1 = reader.readLine();
            line1 = reader.readLine();
            line1 = reader.readLine();
            while(stop == false)
            {
                line1 = reader.readLine();
                line2 = reader.readLine();
                if ((line1.length() >= 22) && (line1.substring(0,     21).equals("Special Instructions:")))
                {
                    stop = true;
                    break;
                }

                else if ((line2.length() >= 22) && (line2.substring(0,        21).equals("Special Instructions:")))
                {
                    bw.write("<TD class=FormLabel vAlign=top align=left        width=\"50%\"><h3><b>            <br><br><b>");
                    bw.write(line1);
                    bw.write("</h3></b>");
                    stop = true;
                    break;
                }
                else
                {
                    bw.write(line1);
                    bw.write("<br>");
                    bw.write(line2);
                    bw.write("<br>");
                }
            }
            bw.write("</h3></b>             </TD>");
            reader.close();
            bw.close();
            print(printme);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void print(File file) {

        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        PrintService printService[] =     PrintServiceLookup.lookupPrintServices(flavor, pras);
        PrintService defaultService =     PrintServiceLookup.lookupDefaultPrintService();
        // PrintService service =     ServiceUI.printDialog(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefau    ltScreenDevice().getDefaultConfiguration(), 200, 200,
        //     printService, defaultService, flavor, pras);
        //if (service != null) {
        DocPrintJob job = defaultService.createPrintJob();
        try{
            FileInputStream fis = new FileInputStream(file);
            DocAttributeSet das = new HashDocAttributeSet();
            Doc document = new SimpleDoc(fis, flavor, das);
            job.print(document, pras);
            //Thread.sleep(10000);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        catch (Exception e) {
        }

    }
}
JEditorPane jp = new JEditorPane("text/html", textString);
jp.validate();
int w = jp.getWidth(), h = jp.getHeight();
BufferedImage saveimg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = saveimg.createGraphics();
jp.paint(g2);
public static void print(File file) {
    JFrame frame = new JFrame();
    JEditorPane pane= new JEditorPane();
    pane.setContentType("text/html");
    try{
        pane.setPage(file.toURI().toURL());
    }catch (IOException ex){
        System.out.println("MALFORMED ERROR!");
    }
    frame.add(pane);
    frame.setSize(200,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    try{
        pane.print(null, null, false, PrintServiceLookup.lookupDefaultPrintService(), null, false);
    } catch (Exception e){
        System.out.println("PRINT ERROR!");
    }
}