Aspose.PDF for Java系列8-转化HTML为PDF
转化HTML为PDF
主要分为以下步骤:
- 创建一个HtmlLoadOptions类。
- 实例化Document对象。
- 调用Document.save()方法保存为PDF文件。
具体代码参考如下:
// Create HTML load options
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// Load HTML file
Document doc = new Document("Sample.html", htmloptions);
// Convert HTML file to PDF
doc.save("HTMLtoPDF.pdf");
还有另外一种版本,具体代码参考如下:
private static void ConvertHTMLtoPDF_Simple() {
// Create a HTML LoadOptions
HtmlLoadOptions options = new HtmlLoadOptions();
// Initialize document object
String htmlFileName = Paths.get(_dataDir.toString(), "test.html").toString();
Document document = new Document(htmlFileName, options);
// Save output PDF document
document.save(Paths.get(_dataDir.toString(), "HTMLtoPDF.pdf").toString());
}
转化HTML为PDF-高级
HTML转化引擎有几个选项,可控制转化过程。
媒体查询支持-Media Queries Support
- 创建一个HTML加载类LoadOptions。
- 设置Print或者Screen模式。
- 实例化Document对象。
- 保存输出PDF文档。
媒体查询技术可以根据不同的设备定制样式表。通过HtmlMEdiaType属性设置媒体类型。
具体代码参考如下:
private static void ConvertHTMLtoPDFAdvanced_MediaType() {
// Create a HTML LoadOptions
HtmlLoadOptions options = new HtmlLoadOptions();
// Set Print or Screen mode
options.setHtmlMediaType(HtmlMediaType.Print);
// Initialize document object
String htmlFileName = Paths.get(_dataDir.toString(), "test.html").toString();
Document document = new Document(htmlFileName, options);
// Save output PDF document
document.save(Paths.get(_dataDir.toString(), "HTMLtoPDF.pdf").toString());
}
打开(关闭)字体嵌入
HTML页面通常会使用字体。通过IsEmbedFonts属性可以控制文档嵌入的字体。
具体代码参考如下:
public static void ConvertHTMLtoPDFAdvanced_EmbedFonts() {
HtmlLoadOptions options = new HtmlLoadOptions();
// Disable font embedding
options.setEmbedFonts(true);
Document document = new Document(_dataDir + "test_fonts.html", options);
document.save(_dataDir + "html_test.PDF");
}
管理外部资源加载
转化引擎提供了控制加载HTML文档相关的外部资源。HtmlLoadOptions有个属性CustomLoaderOfExternalResources,通过它我们可以自己定义如何加载资源。
具体代码参考如下:
public static void ConvertHTMLtoPDFAdvanced_DummyImage() {
HtmlLoadOptions options = new HtmlLoadOptions();
options.CustomLoaderOfExternalResources = new LoadOptions.ResourceLoadingStrategy() {
public LoadOptions.ResourceLoadingResult invoke(String resourceURI) {
// Creating clear template resource for replacing:
LoadOptions.ResourceLoadingResult res = new LoadOptions.ResourceLoadingResult(new byte[] {});
// Return empty byte array in case i.imgur.com server
if (resourceURI.contains("i.imgur.com")) {
return res;
} else {
// Process resources with default resource loader
res.LoadingCancelled = true;
return res;
}
}
};
Document pdfDocument = new Document(_dataDir + "test.html", options);
pdfDocument.save(_dataDir + "html_test.PDF");
}
转化MHTML为PDF
MHTML,是MIME HTML的简称,是一种存档格式,经常用来以HTML代码组合资源(类似外部链接)到一个文件。MHTML文件用MIME类型multipart/related进行编码,类似于HTML邮件信息。
具体代码参考如下:
public final class ConvertMHTMLtoPDF {
private ConvertMHTMLtoPDF() {
}
private static Path _dataDir = Paths.get("/home/aspose/pdf-examples/Samples");
public static void main(String[] args) throws FileNotFoundException {
// Instantiate MHTML Load option object
MhtLoadOptions options = new MhtLoadOptions();
// Create Document object
String mhtmlFileName = Paths.get(_dataDir.toString(), "samplefile.mhtml").toString();
Document document = new Document(mhtmlFileName, options);
// Save output PDF document
document.save(Paths.get(_dataDir.toString(),"TEXtoPDF.pdf").toString());
}
}