tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
2673
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Nasenbrecher Nasenbrecher ist offline Rookie
    Registriert seit
    Oct 2004
    Beiträge
    5
    hallo,

    ich hab das Problem, dass ich ein HTML-Dokument drucken möchte - hab dafür auch schon eine geeignete Funktion geschrieben:

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    
    [LEFT]public void print(){
     try  { 
          PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
          DocFlavor flavor =  DocFlavor.INPUT_STREAM.AUTOSENSE;   
          PrintService service = PrintServiceLookup.lookupDefaultPrintService(); 
     
         if (service != null) 
         { 
           DocPrintJob job = service.createPrintJob(); 
           job.addPrintJobListener(printerListener); 
           FileInputStream fis = new FileInputStream(dir + "\\" + fileName); 
           DocAttributeSet das = new HashDocAttributeSet(); 
           Doc doc = new SimpleDoc(fis, flavor, das); 
           job.print(doc, pras); 
         } 
       } 
       catch (ArrayIndexOutOfBoundsException ex) 
       { 
         ex.printStackTrace(); 
         System.out.println("Keine Drucker gefunden!!"); 
       } 
       catch (Exception ex) 
       { 
         ex.printStackTrace(); 
       } 
    } [/LEFT]

    Das Problem an dem ganzen ist, dass mir die TAGs ausgedruckt werden.
    Ist ja klar bei AUTOSENSE...

    Sobald ich aber DocFlavor.INPUT_STREAM.TEXT_HTML_UTF_8 verwende kommt "invalid flavor" als Exception

    Kann mir einer sagen woran das liegt ?
    Hab natürlich auch nix gegen bessere varianten zum Drucken
     

  2. #2
    flashray flashray ist offline Mitglied Rubin
    Registriert seit
    Sep 2005
    Ort
    Mannheim
    Beiträge
    1.325
    Hallo Nasenbrecher,

    JSP ist die Abkürzung für Java Server Pages, du meinst doch eigentlich JPS - Java Print Service? Oder?

    Also mit JPS habe ich keine guten Erfahrungen gemacht. Denn es ist immer die Frage inwieweit die installierten Drucker die vorgegebenen Druckformate des JPS unterstützen.

    Sicherer ist es auf ein Printable per paint zu zeichnen und dann diese zu drucken. So hättest du keine Probleme ob jener oder dieser Drucker dieses oder jenes Format unterstützt. Deine Implementation würde überall funktionieren.


    Ich habe vor kurzer Zeit versucht ähnlich deiner Vorgehensweise aus deinem Beispielcode reinen Text zu drucken. Es haben weder der Rand gestimmt, noch waren Formatierungen vorhanden, noch war die Qualität akzeptabel. Ich habe mir dahingehend geholfen das ich den gewünschten String einem JTextArea übergeben und dann diese Komponente gedruckt habe.

    http://www.tutorials.de/forum/swing-...printtool.html


    Vg Erdal
     

  3. #3
    flashray flashray ist offline Mitglied Rubin
    Registriert seit
    Sep 2005
    Ort
    Mannheim
    Beiträge
    1.325
    Hallo Nasenbrecher,

    hier mal Beispiel, wie man mit Hilfe eines JEditorPane's eine beliebige Webseite oder einen generierten HTML-Code drucken könnte. Du kannst den Inhalt sowohl als Bilder speichern als auch drucken. Das Speichern als Bilder funktioniert. Das Drucken habe ich allerdings noch nicht ausprobiert.

    Allerdings solltest du folgendes beachten. Wenn du Webseiten drucken möchtest, werden die Seiten so gut aussehen, wie sie das JEditorPane darstellen kann. Um das Druckergebnis zu verbessern wäre eine Komponente notwendig, welche Webseiten korrekter und besser als das JEditorPane darstellen kann. Auch müsste die Komponente die Möglichkeit bieten ihr Abbild in eine Image zu speichern.
    Möchtest du jedoch nur selbst generierten einfachen HTML Code ausdrucken müsste es von der Qualität her reichen. Du müsstest halt noch feineinstellungen vornehmen wie du die Seiten splitten möchtest falls sie größer als eine Seite ist.

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.awt.print.*;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    public class PrintHTML_JEditorPane_Example extends JFrame implements Printable, ActionListener {
     
        // GUI //
        private JEditorPane htmlPane = new JEditorPane();
     
        private JButton button1 = new JButton("Save All");
        
        private JButton button2 = new JButton("Print All");
     
        private JScrollPane sPane = new JScrollPane();
     
        // ScreenShot //
        private BufferedImage bImage;
     
        private List<BufferedImage> bImageList;
     
        private PageFormat pf = new PageFormat();
     
        private PrinterJob printerJob = PrinterJob.getPrinterJob();
     
        private static final int CONS = 2;
     
        // Constructor //
        public PrintHTML_JEditorPane_Example(String s, char c) {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(600, 500);
            this.setLocationByPlatform(true);
            this.setAlwaysOnTop(true);
     
            if (c == 'u') {
            try {
                htmlPane.setPage(s);
            } catch (IOException e) {
                e.printStackTrace();
            }
            }
            else if (c == 'h'){
                htmlPane.setContentType("text/html");
                htmlPane.setText(s);    
            }
     
            htmlPane.setEditable(false);
            sPane.setViewportView(htmlPane);
     
            button1.addActionListener(this);
            button2.addActionListener(this);
            
            this.add(button1, BorderLayout.NORTH);
            this.add(sPane, BorderLayout.CENTER);
            this.add(button2, BorderLayout.SOUTH);
     
            this.setVisible(true);
        }
     
        public static void main(String[] args) {
            // u für URL
            // h für hmtlquelltext
            new PrintHTML_JEditorPane_Example("http://www.faz.de", 'u');
            //new PrintHTML_JEditorPane_Example(htmlCode, 'h');
        }
     
        public void actionPerformed(ActionEvent e) {
            bImageList = new ArrayList<BufferedImage>();
            bImage = new BufferedImage(htmlPane.getWidth(), htmlPane.getHeight(),
                    BufferedImage.TYPE_INT_BGR);
     
            htmlPane.paint(bImage.getGraphics());
     
            int wT = bImage.getWidth();
            int hT = bImage.getHeight();
     
            int w = (int) pf.getImageableWidth() * CONS;
            int h = (int) pf.getImageableHeight() * CONS;
     
            int x = (int) (wT / w);
            int y = (int) (hT / h);
     
            for (int i = 0; i <= x; i++) {
                for (int j = 0; j <= y; j++) {
                    if (i == x && j == y)
                        bImageList.add(bImage.getSubimage(i * w, j * h, wT - x * w,
                                hT - y * h));
                    else if (i == x)
                        bImageList.add(bImage.getSubimage(i * w, j * h, wT - x * w,
                                h));
                    else if (j == y)
                        bImageList.add(bImage.getSubimage(i * w, j * h, w, hT - y
                                * h));
                    else
                        bImageList.add(bImage.getSubimage(i * w, j * h, w, h));
                }
            }
            
            if (e.getActionCommand().equals("Print All"))
                printAll();
            else saveAll();
        }
        
        public void saveAll(){
            for (int i = 0; i < bImageList.size(); i++) {
                try {
                    ImageIO.write(bImageList.get(i), "JPEG", new File("page" + i
                            + ".jpg"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        
        public boolean printAll() {
            printerJob.setPrintable(this, pf);
            try {
                printerJob.print();
            } catch (PrinterException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
     
        public int print(Graphics g, PageFormat pFormat, int pageIndex)
                throws PrinterException {
     
            int page = 0;
     
            Graphics2D g2 = (Graphics2D) g;
     
            g2.translate((int) pFormat.getImageableX(), (int) pFormat
                    .getImageableY());
            g2.scale(1.0 / CONS, 1.0 / CONS);
     
            g2.drawImage(bImageList.get(page), 0, 0, this);
     
            page++;
     
            return page > bImageList.size() ? Printable.PAGE_EXISTS
                    : Printable.NO_SUCH_PAGE;
        }
        
        private static final String htmlCode = "<html>" +
        "<head>" +
        "<title>Beschreibung der Seite</title>" +
        "</head>" +
        "<body>" +
        "<h1>Überschrift Überschrift Überschrift Überschrift<h1>" +
        "Text Text Text Text Text Text Text Text Text Text" +
        "Text Text Text Text Text Text Text Text Text Text" +
        "Text Text Text Text Text Text Text Text Text Text" +
        "Text Text Text Text Text Text Text Text Text Text" +
        "Text Text Text Text Text Text Text Text Text Text" +
        "</body>" +
        "</html>";
    }

    Vg Erdal
     

Ähnliche Themen

  1. automatisches Drucken von html
    Von Bamboocha_10 im Forum Visual Basic 6.0
    Antworten: 9
    Letzter Beitrag: 27.01.08, 17:22
  2. Etiketten drucken mit php/html
    Von Das Gelbe vom Ei im Forum PHP
    Antworten: 5
    Letzter Beitrag: 01.11.07, 13:42
  3. C# - HTML drucken
    Von soyo im Forum .NET Windows Forms
    Antworten: 1
    Letzter Beitrag: 06.08.07, 09:03
  4. HTML Mails drucken
    Von CharlysTante im Forum Office-Anwendungen
    Antworten: 1
    Letzter Beitrag: 06.04.05, 08:12
  5. HTML Drucken
    Von dondq im Forum C/C++
    Antworten: 2
    Letzter Beitrag: 07.10.03, 11:51