package com;
import java.awt.Insets;
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringReader;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
public class PdfKit {
public static void main(String[] args) throws Throwable{
String pdf="H:/a.pdf";
String html="H:/a.html";
htmlFilegeneratePDF(pdf, html);
}
/**
* @description TODO html文件生成pdf文件
* @param @param pdfFileName
* @param @param htmlName
* @param @throws Exception
*/
public static void htmlFilegeneratePDF(String pdfFileName, String htmlName)
throws Exception {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(pdfFileName));
PD4ML pd4ml = new PD4ML();
pd4ml.setPageInsets(new Insets(5, 20, 20, 20));
pd4ml.setHtmlWidth(1024);
pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
pd4ml.useTTF("java:fonts", true);
pd4ml.setDefaultTTFs("SimHei", "Arial", "Courier New");
// pd4ml.enableDebugInfo();
pd4ml.render("file:" + htmlName, fos);
} finally {
fos.close();
}
}
/**
* @description TODO 把html代码字符串转换为Pdf文件
* @param @param pdfFileName
* @param @param htmlContent
* @param @throws Exception
*/
public static void htmlContentgeneratePDF(String pdfFileName,
String htmlContent) throws Exception {
StringReader strReader = null;
FileOutputStream fos = null;
try {
strReader = new StringReader(htmlContent);
fos = new FileOutputStream(new File(pdfFileName));
PD4ML pd4ml = new PD4ML();
pd4ml.setPageInsets(new Insets(20, 10, 10, 10));
pd4ml.setHtmlWidth(1024);
pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
pd4ml.useTTF("java:fonts", true);
pd4ml.setDefaultTTFs("SimHei", "Arial", "Courier New");
// pd4ml.enableDebugInfo();
pd4ml.render(strReader, fos);
} finally {
fos.close();
strReader.close();
}
}
}