I have to offer some export functions to my website such as CSV or PDF. Is there a powerful and free tool for Java to convert HTML pages to PDF format?
我必须为我的网站提供一些导出功能,比如CSV或PDF。Java是否有一个强大的免费工具可以将HTML页面转换成PDF格式?
2 个解决方案
#1
29
Using Flying Saucer API
with iText PDF
you can convert HTML content to PDF.
Following examples help you in understanding, to some extent, conversion of XHTML to PDF.
使用带有iText PDF的飞碟API,您可以将HTML内容转换为PDF。以下示例在某种程度上帮助您理解XHTML到PDF的转换。
Examples Using Flying Saucer API:
You require following libraries:
使用飞碟API的示例:您需要以下库:
- core-renderer.jar
- core-renderer.jar
- iText-2.0.8.jar
- iText-2.0.8.jar
You can find these resources in flyingsaucer-R8.zip
.
你可以在flyingsaucer-R8.zip中找到这些资源。
Example1: Using XML Resource:
例二:使用XML资源:
// if you have html source in hand, use it to generate document object
Document document = XMLResource.load( new ByteArrayInputStream( yourXhtmlContentAsString.getBytes() ) ).getDocument();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument( document, null );
renderer.layout();
String fileNameWithPath = outputFileFolder + "PDF-XhtmlRendered.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
renderer.createPDF( fos );
fos.close();
System.out.println( "File 1: '" + fileNameWithPath + "' created." );
Example2: Using XHTML direct input to Document:
示例2:使用XHTML直接输入文档:
ITextRenderer renderer = new ITextRenderer();
// if you have html source in hand, use it to generate document object
renderer.setDocumentFromString( yourXhtmlContentAsString );
renderer.layout();
String fileNameWithPath = outputFileFolder + "PDF-FromHtmlString.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
renderer.createPDF( fos );
fos.close();
System.out.println( "File 2: '" + fileNameWithPath + "' created." );
Examples Using iText API:
You require following libraries:
使用iText API的示例:您需要以下库:
- core-renderer.jar
- core-renderer.jar
- itextpdf-5.2.1.jar
- itextpdf-5.2.1.jar
You can find these resources at here.
你可以在这里找到这些资源。
Example3: Using HTML Worker:
青年们:使用HTML工作人员:
com.itextpdf.text.Document document =
new com.itextpdf.text.Document( com.itextpdf.text.PageSize.A4 );
String fileNameWithPath = outputFileFolder + "PDF-HtmlWorkerParsed.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
com.itextpdf.text.pdf.PdfWriter pdfWriter =
com.itextpdf.text.pdf.PdfWriter.getInstance( document, fos );
document.open();
//**********************************************************
// if required, you can add document meta data
document.addAuthor( "Ravinder" );
//document.addCreator( creator );
document.addSubject( "HtmlWoker Parsed Pdf from iText" );
document.addCreationDate();
document.addTitle( "HtmlWoker Parsed Pdf from iText" );
//**********************************************************/
com.itextpdf.text.html.simpleparser.HTMLWorker htmlWorker =
new com.itextpdf.text.html.simpleparser.HTMLWorker( document );
htmlWorker.parse( new StringReader( sb.toString() ) );
document.close();
fos.close();
System.out.println( "File 3: '" + fileNameWithPath + "' created." );
#2
-2
You can use JTidy framework for this. This will convert HTML to XHTML and translate again to XSL-FO. This object can be used for generating PDF.
您可以为此使用JTidy框架。这将把HTML转换为XHTML并再次转换为XSL-FO。此对象可用于生成PDF。
http://www.javaworld.com/javaworld/jw-04-2006/jw-0410-html.html
http://www.javaworld.com/javaworld/jw - 04 - 2006/jw - 0410 html.html
#1
29
Using Flying Saucer API
with iText PDF
you can convert HTML content to PDF.
Following examples help you in understanding, to some extent, conversion of XHTML to PDF.
使用带有iText PDF的飞碟API,您可以将HTML内容转换为PDF。以下示例在某种程度上帮助您理解XHTML到PDF的转换。
Examples Using Flying Saucer API:
You require following libraries:
使用飞碟API的示例:您需要以下库:
- core-renderer.jar
- core-renderer.jar
- iText-2.0.8.jar
- iText-2.0.8.jar
You can find these resources in flyingsaucer-R8.zip
.
你可以在flyingsaucer-R8.zip中找到这些资源。
Example1: Using XML Resource:
例二:使用XML资源:
// if you have html source in hand, use it to generate document object
Document document = XMLResource.load( new ByteArrayInputStream( yourXhtmlContentAsString.getBytes() ) ).getDocument();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument( document, null );
renderer.layout();
String fileNameWithPath = outputFileFolder + "PDF-XhtmlRendered.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
renderer.createPDF( fos );
fos.close();
System.out.println( "File 1: '" + fileNameWithPath + "' created." );
Example2: Using XHTML direct input to Document:
示例2:使用XHTML直接输入文档:
ITextRenderer renderer = new ITextRenderer();
// if you have html source in hand, use it to generate document object
renderer.setDocumentFromString( yourXhtmlContentAsString );
renderer.layout();
String fileNameWithPath = outputFileFolder + "PDF-FromHtmlString.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
renderer.createPDF( fos );
fos.close();
System.out.println( "File 2: '" + fileNameWithPath + "' created." );
Examples Using iText API:
You require following libraries:
使用iText API的示例:您需要以下库:
- core-renderer.jar
- core-renderer.jar
- itextpdf-5.2.1.jar
- itextpdf-5.2.1.jar
You can find these resources at here.
你可以在这里找到这些资源。
Example3: Using HTML Worker:
青年们:使用HTML工作人员:
com.itextpdf.text.Document document =
new com.itextpdf.text.Document( com.itextpdf.text.PageSize.A4 );
String fileNameWithPath = outputFileFolder + "PDF-HtmlWorkerParsed.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
com.itextpdf.text.pdf.PdfWriter pdfWriter =
com.itextpdf.text.pdf.PdfWriter.getInstance( document, fos );
document.open();
//**********************************************************
// if required, you can add document meta data
document.addAuthor( "Ravinder" );
//document.addCreator( creator );
document.addSubject( "HtmlWoker Parsed Pdf from iText" );
document.addCreationDate();
document.addTitle( "HtmlWoker Parsed Pdf from iText" );
//**********************************************************/
com.itextpdf.text.html.simpleparser.HTMLWorker htmlWorker =
new com.itextpdf.text.html.simpleparser.HTMLWorker( document );
htmlWorker.parse( new StringReader( sb.toString() ) );
document.close();
fos.close();
System.out.println( "File 3: '" + fileNameWithPath + "' created." );
#2
-2
You can use JTidy framework for this. This will convert HTML to XHTML and translate again to XSL-FO. This object can be used for generating PDF.
您可以为此使用JTidy框架。这将把HTML转换为XHTML并再次转换为XSL-FO。此对象可用于生成PDF。
http://www.javaworld.com/javaworld/jw-04-2006/jw-0410-html.html
http://www.javaworld.com/javaworld/jw - 04 - 2006/jw - 0410 html.html