|
1 | 1 | package com.hmkcode; |
2 | 2 |
|
3 | 3 | import java.io.FileInputStream; |
| 4 | +import java.io.FileNotFoundException; |
4 | 5 | import java.io.FileOutputStream; |
5 | 6 | import java.io.IOException; |
6 | | -import com.itextpdf.text.Document; |
7 | | -import com.itextpdf.text.DocumentException; |
8 | | -import com.itextpdf.text.pdf.PdfWriter; |
9 | | -import com.itextpdf.tool.xml.XMLWorkerHelper; |
| 7 | + |
| 8 | +import com.itextpdf.html2pdf.HtmlConverter; |
10 | 9 |
|
11 | 10 | public class App |
12 | 11 | { |
13 | | - public static void main( String[] args ) throws DocumentException, IOException |
| 12 | + public static final String HTML = "<h1>Hello</h1>" |
| 13 | + + "<p>This was created using iText</p>" |
| 14 | + + "<a href='hmkcode.com'>hmkcode.com</a>"; |
| 15 | + |
| 16 | + |
| 17 | + public static void main( String[] args ) throws FileNotFoundException, IOException |
14 | 18 | { |
15 | | - // step 1 |
16 | | - Document document = new Document(); |
17 | | - // step 2 |
18 | | - PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf")); |
19 | | - // step 3 |
20 | | - document.open(); |
21 | | - // step 4 |
22 | | - XMLWorkerHelper.getInstance().parseXHtml(writer, document, |
23 | | - new FileInputStream("index.html")); |
24 | | - //step 5 |
25 | | - document.close(); |
26 | | - |
| 19 | + |
| 20 | + // String to PDF |
| 21 | + HtmlConverter.convertToPdf(HTML, new FileOutputStream("string-to-pdf.pdf")); |
| 22 | + |
| 23 | + |
| 24 | + // HTML file to PDF |
| 25 | + HtmlConverter.convertToPdf(new FileInputStream("index.html"), |
| 26 | + new FileOutputStream("index-to-pdf.pdf")); |
| 27 | + |
| 28 | + |
27 | 29 | System.out.println( "PDF Created!" ); |
28 | 30 | } |
29 | 31 | } |
0 commit comments