6
A
답변
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>
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"));
4
난 당신이 라이브러리를 사용할 수 있다고 생각
html2image-0.9.jar
,이 페이지에서이 라이브러리를 다운로드 할 수 있습니다 http://code.google.com/p/java-html2image/
3
어떤 메모리 ByteArrayStream
대신 위의 코드에서 FileOutputStream
에 사용에 대한? 적어도 바이트 배열이 될 것입니다 ...
렌더링 된 html 페이지의 "screeenshot"과 비슷합니까? –
아니, 난 HTML을 만들고 이미지 소스가없는 그림으로 팩스를 통해 보내야하므로 이미지로 변환 한 다음 이미지를 보내려합니다. – cls