Convert HTML to PDF
This method of generating PDF does not require templates or JSON data. It only needs a standard HTML file or a standard HTML string.
java
String htmlPath = "/test/convert.html";
String baseUrl = "/test/";
String outputPdf = "/test/convert.pdf";
int pageWidth = 842;
int pageHeight = 596;
CConverterProperties properties = new CConverterProperties();
CSize size = new CSize(pageWidth, pageHeight);
properties.setPageSize(size);
properties.setBaseUri(baseUrl);
CFontProvider fontProvider = new CFontProvider();
properties.setFontProvider(fontProvider);
CHtmlConverter.CErrorCode id = CHtmlConverter.ConvertToPdf(htmlPath, outputPdf, properties);The HTML can be a content String, InputStream, File object, or file path. The generated PDF can be an OutputStream, File object, or file path. The previous example used file paths, while the following are examples using other types.
Use InputStream and OutputStream:
java
String htmlPath = "/test/convert_1.html";
String outPdfPath = "/test/outPdf.pdf";
try {
InputStream htmlIn = new FileInputStream(htmlPath);
OutputStream pdfOut = new FileOutputStream(outPdfPath);
CHtmlConverter.CErrorCode error = CHtmlConverter.ConvertToPdf(htmlIn, pdfOut);
} catch (Exception e) {
}Use Content String:
java
String htmlPath = "/test/convert_1.html";
String outPdfPath = "/test/outPdf.pdf";
try {
String htmlStr = getFileContent(htmlPath);
OutputStream pdfOut = new FileOutputStream(outPdfPath);
CHtmlConverter.CErrorCode error = CHtmlConverter.ConvertToPdf(htmlStr, pdfOut);
} catch (Exception e) {
}Use File:
java
String htmlPath = "/test/convert_1.html";
String outPdfPath = "/test/outPdf.pdf";
try {
File htmlFile = new File(htmlPath);
File pdfFile = new File(outPdfPath);
CHtmlConverter.CErrorCode error = CHtmlConverter.ConvertToPdf(htmlFile, pdfFile);
} catch (Exception e) {
}