6
A
回答
0
這是平凡的,因爲渲染HTML頁面可以是相當複雜:你有文字,圖片,CSS,甚至可能要評估JavaScript。
我不知道答案,但我確實有一些可以幫助你的東西:代碼爲iText(PDF書寫庫)將HTML頁面轉換爲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 2010-12-14 10:34:50
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
3
什麼內存ByteArrayStream
,而不是在上面的代碼中FileOutputStream
使用?這將是一個字節數組,至少...
相關問題
- 1. 如何將字節數組轉換爲java中的圖像?
- 2. 將圖像轉換爲Java中的字節數組
- 3. 將圖像轉換爲Java類中的字節數組
- 4. 將圖像轉換爲字節數組?
- 5. 將字節數組轉換爲圖像
- 6. 將字節數組轉換爲圖像
- 7. 將YUV攝像頭圖像轉換爲java字節數組
- 8. 在java中將字節數組轉換爲圖像(blob)
- 9. 使用Rails將字節數組轉換爲HTML圖像?
- 10. 如何將圖像轉換爲字節數組並將字節數組轉換爲圖像
- 11. 如何將Java圖像轉換爲JPEG字節數組?
- 12. 將PDF字節數組轉換爲jpg圖像(JAVA項目)
- 13. 在C中將圖像轉換爲可用的字節數組?
- 14. 將圖像轉換爲android中的字節數組
- 15. 將字節數組轉換爲視圖上的圖像
- 16. 在打字稿中將圖像轉換爲字節數組
- 17. 將Java字符串轉換爲16字節的字節數組
- 18. 將Java字節數組轉換爲Python字節數組
- 19. 如何將Java字節數組轉換爲Scala字節數組?
- 20. 字節數組Base64String(圖像轉換)
- 21. 轉換字節數組圖像
- 22. 字節數組到圖像轉換
- 23. 轉換字節數組圖像
- 24. 將字節數組轉換爲.wav java
- 25. 轉換字節數組(byte [])到圖像在Java中
- 26. 將字節數組轉換爲Java中的圖像 - 不知道類型
- 27. 將圖像數據轉換爲字節[]
- 28. 如何將特定的字節數組轉換爲圖像
- 29. 無法將上傳的圖像轉換爲字節數組
- 30. 將圖像轉換爲字節數組的最快方法
像一個呈現html頁面的「screeenshot」? – 2010-12-14 09:49:36
不,我創建html,我需要通過傳真發送圖片沒有圖像源,所以我想將其轉換爲圖像,然後發送圖像 – cls 2010-12-14 09:58:52