Aspose.PDF for Java系列8-转化HTML为PDF

Aspose.PDF for Java系列8-转化HTML为PDF

转化HTML为PDF

主要分为以下步骤:

  1. 创建一个HtmlLoadOptions类。
  2. 实例化Document对象。
  3. 调用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

  1. 创建一个HTML加载类LoadOptions。
  2. 设置Print或者Screen模式。
  3. 实例化Document对象。
  4. 保存输出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());
    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值