HTML 到图像

如何在 Java 中将 HTML 转换为图像

HTML 是构建网页的实际格式,它以标准文本格式存储内容。 HTML 中的标签定义了网页的页面布局和内容,包括在网络浏览器中显示的文本、表格、图像和超链接。然而,最后,据观察,恶意脚本可以嵌入到 HTML 页面中,并且可以用于各种不同的攻击类型,包括跨站点脚本 (XSS)。因此,许多组织/系统阻止加载在离线模式下共享的 HTML 文件。所以一个可行的解决方案是将 HTML 转换为 Image 格式。在本文中,我们将讨论如何在 Java 中将 HTML 转换为 JPG 的详细信息。

HTML 到图像转换 API

我们将使用 Aspose.HTML Cloud SDK for Java 执行 HTML 到图像的转换。此 API 提供加载和操作现有 HTML 文件的功能。同时,它还提供了将 HTML 渲染为 PDFXPSDOCX 和图像格式(JPEGPNGBMPTIFF)。现在请将以下行添加到您的 Maven 构建类型项目的 pom.xml 中以下载并安装 SDK。

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-html-cloud</artifactId>
<version>20.7.0</version>
</dependency>

下一个主要步骤是使用 GitHub 或 Google 帐户通过 Aspose.Cloud 仪表板 免费订阅我们的云服务。或者,只需 创建一个新帐户 并获取您的客户凭证详细信息。

如何在 Java 中将 HTML 转换为 JPG

请按照下面给出的说明完成将 HTML 转换为 JPG 的要求。

  • 首先,我们需要针对 Configuration.setAPPSID 和 Configuration.setAPIKEY 方法指定详细信息
  • 其次,我们设置 setBasePath(..)、setAuthPath(..) 的细节并指定 setUserAgent(…) 作为 WebKit
  • 第三,为了我们自己的帮助,我们将设置 setDebug(..) 为 true
  • 现在创建一个 ConversionApi 类的对象
  • 为结果文件的信息指定边距详细信息和名称
  • 最后,调用 GetConvertDocumentToImage(…) 启动转换过程。此方法接受输入的 HTML 名称、生成的图像格式、边距和尺寸详细信息作为参数
// 有关更多示例,请访问 https://github.com/aspose-html-cloud/aspose-html-cloud-java

try
    {
    // 从 https://dashboard.aspose.cloud/ 获取 ClientID 和 ClientSecret
    String clientId = "bbf94a2c-6d7e-4020-b4d2-b9809741374e";
    String clientSecret = "1c9379bb7d701c26cc87e741a29987bb";
  
    // Api 调用的详细信息
    com.aspose.html.Configuration.setAPP_SID(clientId);
    com.aspose.html.Configuration.setAPI_KEY(clientSecret);
    com.aspose.html.Configuration.setBasePath("https://api.aspose.cloud/v3.0");
    com.aspose.html.Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
    com.aspose.html.Configuration.setUserAgent("WebKit");
    com.aspose.html.Configuration.setDebug(true);
        
    // 创建 Aspose.HTML Cloud API 对象
    com.aspose.html.api.ConversionApi htmlApi = new ApiClient().createService(ConversionApi.class);
     	
    // 来自云存储的 html 文件
    String name = "list.html";
    // 生成的图像格式
    String outFormat = "PNG";
    	
    Integer width = 800; // Resulting image width.
    Integer height = 1000; // Resulting image height.
    Integer leftMargin = 10; // Left resulting image margin.
    Integer rightMargin = 10; // Right resulting image margin.
    Integer topMargin = 10; // Top resulting image margin.
    Integer bottomMargin = 10; // Bottom resulting image margin.
    Integer resolution = 300; // Resolution of resulting image.
    String folder = null; // The folder in the storage. Should exist.
    String storage = "Internal"; // Name of the storage. null
    	
    // 调用 HTMl 到 JPG 转换的 API
    retrofit2.Call<okhttp3.ResponseBody> call = htmlApi.GetConvertDocumentToImage(name, outFormat, width, height, leftMargin, rightMargin, topMargin, bottomMargin, resolution, folder, storage);
    
    // (可选的自定义方法将结果 JPG 保存到本地驱动器)
    checkAndSave(call, "resultantFile.png");
  
    System.out.println("HTML to JPG conversion sucessfull !");
    }catch(Exception ex)
    {
        System.out.println(ex);
    }

使用 cURL 命令将 HTML 转换为 JPG

Aspose.HTML 云 API 也可以使用命令行终端通过 cURL 命令访问。但作为先决条件,我们需要首先根据您的个人客户端凭据生成一个 JSON Web 令牌 (JWT)。请执行以下命令生成 JWT 令牌。

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

生成 JWT 令牌后,请在终端上执行以下命令以执行 HTML 到图像的转换。

curl -v -X GET "https://api.aspose.cloud/html/list.html/convert/image/JPG" \
-H  "accept: multipart/form-data" \
-H  "authorization: Bearer <JWT Token>"

结论

我们已经学习了使用 Java 代码片段将 HTML 转换为图像,以及如何使用 cURL 命令将 HTML 转换为 JPG 的详细信息。产品 文档 是学习 API 提供的其他惊人功能的重要资源。此外,如果您在使用 API 时遇到任何问题,请随时联系 免费产品支持论坛

相关文章

我们还建议访问以下博客以获取有关以下内容的更多详细信息: