2010-12-14 2 views

답변

0

HTML 페이지를 렌더링하는 것은 매우 복잡 할 수 있기 때문 간단한 일은을 html로 변환 : 당신을 텍스트, 이미지, CSS, 심지어는 평가할 자바 스크립트도 있습니다.

답변을 모르겠지만 HTML 페이지를 PDF 파일로 변환하기위한 iText (PDF 라이팅 라이브러리) 코드가 도움이 될 것입니다.

public static final void convert(final File xhtmlFile, final File pdfFile) throws IOException, DocumentException 
{ 
    final String xhtmlUrl = xhtmlFile.toURI().toURL().toString(); 
    final OutputStream reportPdfStream = new FileOutputStream(pdfFile); 
    final ITextRenderer renderer = new ITextRenderer(); 
    renderer.setDocument(xhtmlUrl); 
    renderer.layout(); 
    renderer.createPDF(reportPdfStream); 
    reportPdfStream.close(); 
} 
+1

바이트 배열로 저장하지 않고도이 파일을 만들면됩니다. 감사합니다 – cls

12

당신이 어떤 복잡한 HTML이없는 경우 정상적인 JLabel를 사용하여 렌더링 할 수 있습니다. 그냥 파일에 기록하려는 경우

<html> 
    <h1>:)</h1> 
    Hello World!<br> 
    <img src="http://img0.gmodules.com/ig/images/igoogle_logo_sm.png"> 
</html> 

alt text

public static void main(String... args) throws IOException { 

    String html = "<html>" + 
      "<h1>:)</h1>" + 
      "Hello World!<br>" + 
      "<img src=\"http://img0.gmodules.com/ig/images/igoogle_logo_sm.png\">" + 
      "</html>"; 

    JLabel label = new JLabel(html); 
    label.setSize(200, 120); 

    BufferedImage image = new BufferedImage(
      label.getWidth(), label.getHeight(), 
      BufferedImage.TYPE_INT_ARGB); 

    { 
     // paint the html to an image 
     Graphics g = image.getGraphics(); 
     g.setColor(Color.BLACK); 
     label.paint(g); 
     g.dispose(); 
    } 

    // get the byte array of the image (as jpeg) 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    ImageIO.write(image, "jpg", baos); 
    byte[] bytes = baos.toByteArray(); 

    .... 
} 

: 아래 코드는이 이미지를 생성합니다

ImageIO.write(image, "png", new File("test.png")); 
+0

바이트 배열로 저장하기 위해서만 그것을 만들 필요가 없습니다 – cls

+0

당신은'ImageIO.write'와 같은 것을 통과해야합니다. 이미지를 먼저 가지지 않고도 마술처럼 바이트 배열을 구성 할 수는 없습니다. – aioobe

+0

@cls 바이트 배열에는 어떤 형식이 필요합니까? – dacwe

3

어떤 메모리 ByteArrayStream 대신 위의 코드에서 FileOutputStream에 사용에 대한? 적어도 바이트 배열이 될 것입니다 ...

관련 문제