2014-09-08 2 views
1

Java 응용 프로그램에서 HTML 파일을 인쇄해야합니다. 몇 가지 방법을 시도했습니다. 그 중 두 명은 일하고 있지만 예상대로는 아닙니다.Java에서 HTML 파일 인쇄

방법 1 :

문제 : 인쇄는 시트의 3 분기 이후의 절단한다.

try { 
     PrinterJob printJob = PrinterJob.getPrinterJob(); 

     PageFormat pageFormat = new PageFormat(); 
     pageFormat.setOrientation(PageFormat.LANDSCAPE); 

     Paper paper = new Paper(); // Set to A4 size. 
     paper.setSize(594.936, 841.536); // Set the margins. 
     paper.setImageableArea(0, 0, 594.936, 841.536); 
     pageFormat.setPaper(paper); 

     XHTMLPanel panel = new XHTMLPanel(); 
     panel.setDocument(new File("./documents/" + "Personalstamm" 
       + ".html")); 

     printJob.setPrintable(new XHTMLPrintable(panel), pageFormat); 
     if (printJob.printDialog()) { 

      printJob.print(); 
     } 
    } catch (Exception x) { 
     x.printStackTrace(); 
    } 

방법 2 :

문제 : 출력은 HTML 파일에 설정된 스타일 시트없이.

PageFormat pageFormat = new PageFormat(); 
    Paper a4paper = new Paper(); 
    double paperWidth = 8.26; 
    double paperHeight = 11.69; 
    a4paper.setSize(paperWidth * 72.0, paperHeight * 72.0); 

    /* 
    * set the margins respectively the imageable area 
    */ 
    double leftMargin = 0.78; /* should be about 2cm */ 
    double rightMargin = 0.78; 
    double topMargin = 0.78; 
    double bottomMargin = 0.78; 

    a4paper.setImageableArea(leftMargin * 72.0, topMargin * 72.0, 
      (paperWidth - leftMargin - rightMargin) * 72.0, (paperHeight 
        - topMargin - bottomMargin) * 72.0); 
    pageFormat.setPaper(a4paper); 
    pageFormat.setOrientation(PageFormat.LANDSCAPE); 

    DocumentRenderer documentRenderer = new DocumentRenderer(pageFormat, 
      "Report"); 
    try { 
     FileInputStream stringReader = new FileInputStream(new File(
       "./documents/" + "Personalstamm" + ".html")); 
     HTMLEditorKit htmlKit = new HTMLEditorKit(); 
     HTMLDocument htmlDoc = (HTMLDocument) htmlKit 
       .createDefaultDocument(); 

     htmlKit.read(stringReader, htmlDoc, 0); 
     documentRenderer.print(htmlDoc); 
    } catch (Exception x) { 
     x.printStackTrace(); 
    } 

아무도이 방법 중 하나에서 문제를 해결하는 방법을 알고 있습니까? 아니면 누구든지 Java에서 파일을 인쇄하는 더 좋은 방법이 있습니까?

답변

1

HTMLCSS과 함께 인쇄 할 수 없습니다. 가장 좋은 것은 PDFs을 사용하는 것입니다. Java를 사용하여 HTML에서 PDF를 작성하고 인쇄하십시오.

+0

인쇄'HTML'이 가능합니다. 하지만 인쇄물은 잘려나 간다. 이미'PDF로 변환하려고 시도했지만 끔찍한보고있다. – Stone

+0

첫 번째 방법은 'panel'을'JScrollPane' 안에 넣고 출력 해보는 것입니다. 'JScrollPane scroll = new JScrollPane (panel);을 실행 해, scrollPane를 출력합니다. 전체 내용을 인쇄하도록 코드를 작성해야하지만 스크롤 팬 – ItachiUchiha

0

Jasper Reports을 사용하면이 문제를 해결할 수 있습니다.

+0

을 사용하면 더 많은 제어 기능을 사용할 수 있습니다. 감사하겠습니다. – Stone

1

지금은 Apache PDFBox - A Java PDF Library을 사용하고 있으며 거의 ​​내가 찾고있는 것이 었습니다.

내 코드 : CSS``로 첫 번째 방법에서

public void printFile(String fileName) { 
    //Convert to PDF 
    try { 
     ITextRenderer renderer = new ITextRenderer(); 
     renderer.setDocument(new File("./documents/html/"+fileName+".html")); 

     renderer.layout(); 

     String fileNameWithPath = "./documents/pdf/"+fileName+".pdf"; 
     FileOutputStream fos = new FileOutputStream(fileNameWithPath); 
     renderer.createPDF(fos); 

     fos.close(); 
    } catch (Exception x) { 
     x.printStackTrace(); 
    } 
    //Print with Dialog 
    try { 
     PrinterJob printJob = PrinterJob.getPrinterJob(); 
     PageFormat pageFormat = new PageFormat(); 
     pageFormat.setOrientation(PageFormat.LANDSCAPE); 
     Paper paper = new Paper(); 
     paper.setSize(595, 842); 
     paper.setImageableArea(0, 0, 595, 842); 
     pageFormat.setPaper(paper); 

     PDDocument doc = PDDocument.load(new File("./documents/pdf/"+fileName+".pdf")); 

     printJob.setPrintable(new PDPageable(doc), pageFormat); 

     if (printJob.printDialog()) { 
      printJob.print(); 
     } 
     doc.close(); 

    } catch (Exception x) { 
     x.printStackTrace(); 
    } 

} 
+2

소스 코드에 따라 PDFBox가 아니라 itext입니다. –