包如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
代码如下:
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/longlong/Desktop/demo.pdf"));
// step 3
document.open();
// InputStreamReader isr = new InputStreamReader(new FileInputStream("C:/Users/longlong/Desktop/customer.html"), "UTF-8");
InputStream in = new FileInputStream(new File("C:/Users/longlong/Desktop/customer.html"));
// step 4
// XMLWorkerHelper.getInstance().parseXHtml(writer, document,isr);
XMLWorkerHelper.getInstance().parseXHtml(writer, document, in, null, new MyFont());
//step 5
document.close();
System.out.println( "PDF Created!" );