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 時遇到任何問題,請隨時聯繫 免費產品支持論壇

相關文章

我們還建議訪問以下博客以獲取有關以下內容的更多詳細信息: