
WebP 轉 PDF 轉換器在 Java 中
WebP 是一種現代圖像格式,提供比傳統格式如 JPEG 和 PNG 更優越的壓縮和質量。它們相當受歡迎,因為 WebP 文件通常比傳統的 JPEG 小得多,因此網站加載速度更快,提供更好的用戶體驗。然而,它並不總是與所有設備和平台兼容。這就是將 WebP 轉換為 PDF 的地方。在本文中,我們將指導您如何使用 Java REST API 將 WebP 轉換為 PDF。我們將帶您走過創建 Java 項目、調用 REST API 端點以及將 WebP 文件轉換為 PDF 的過程。
WebP 轉 PDF REST API
為了處理光柵圖像、元文件、Photoshop 和其他流行的圖像格式,我們開發了 Aspose.Imaging Cloud SDK for Java。這是一個提供可靠的圖像處理和圖像轉換能力的最佳解決方案。它還支持將 WebP 文件轉換為 PDF 格式的功能。現在,為了在線將 WebP 轉換為 PDF,我們需要通過在 pom.xml 中包含以下信息來在我們的 Java 項目中添加 API 參考(maven 構建類型項目)。
<repositories>
<repository>
<id>aspose-cloud</id>
<name>artifact.aspose-cloud-releases</name>
<url>https://artifact.aspose.cloud/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging-cloud</artifactId>
<version>22.4</version>
</dependency>
</dependencies>
一旦在 Java 專案中添加了 REST API 參考,請從 Cloud Dashboard 獲取您的客戶端憑證。否則,您需要先使用有效的電子郵件地址註冊一個免費帳戶。
將 WebP 轉換為 PDF 在 Java 中
讓我們討論使用 Java 將 WebP 轉換為 PDF 的步驟。請注意,以下代碼片段會期望輸入的 WebP 圖像存儲在雲端,因此我們將上傳輸入的 WebP 圖像到雲端儲存。
- 使用個人化憑證建立 ImagingApi 的實例
- 使用 readAllBytes(…) 方法加載 WebP 文件,並將返回的值傳遞給 byte[] 陣列。
- 現在創建一個 CreateConvertedImageRequest 的物件,它以 WebP 圖像名稱和 ‘pdf’ 格式作為參數。
- 呼叫方法 createConvertedImage(…) 以執行 WebP 到 PDF 的轉換。作為結果,輸出會在響應流中返回。
- 最後,使用 FileOutputStream 物件將結果 WebP 保存到本地驅動器
// 從 https://dashboard.aspose.cloud/ 獲取 ClientID 和 ClientSecret
String clientId = "bb959721-5780-4be6-be35-ff5c3a6aa4a2";
String clientSecret = "4d84d5f6584160cbd91dba1fe145db14";
// 創建影像物件
ImagingApi imageApi = new ImagingApi(clientSecret, clientId);
// 從本地磁碟載入 WebP 圖像
File file1 = new File("input.png");
byte[] imageStream = Files.readAllBytes(file1.toPath());
// 指定輸出格式為 PDF
String format = "pdf";
// 創建圖像轉換請求對象
CreateConvertedImageRequest convertRequest = new CreateConvertedImageRequest(imageStream,format,null,null);
// 將 WebP 轉換為 PDF 並將輸出放在響應流中
byte[] resultantImage = imageApi.createConvertedImage(convertRequest);
// 將結果 PDF 保存到本地驅動器
FileOutputStream fos = new FileOutputStream("/Users/nayyer/Documents/" + "resultant.pdf");
fos.write(resultantImage);
fos.close();

Image:- WebP 轉 PDF 轉換預覽
上面例子中使用的輸入 WebP 圖像可以從 這個連結 下載。
使用 cURL 命令將 WebP 檔案轉換為 PDF
除了 Java 代碼片段外,將 WebP 文件轉換為 PDF 格式的另一種方法是通過 cURL 命令。現在,作為前提條件,我們需要使用以下命令生成 JWT 存取權杖(基於客戶端憑證)。
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
現在執行以下命令將 WebP 轉換為 PDF 格式,並將輸出保存到本地驅動器。
curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/input.webp/convert?format=pdf" \
-H "accept: application/json" \
-H "authorization: Bearer <JWT Token>" \
-o "Output.pdf"
結論
這篇文章明確提供了使用 Java 將 WebP 轉換為 PDF 所需的所有詳細信息。除此之外,我們強烈建議您探索 Product Documentation,這是一個學習 API 提供的其他令人興奮的功能的絕佳信息來源。您也可以通過 SwaggerUI 在網絡瀏覽器中測試 API,如果您需要下載和修改 Cloud SDK 的源代碼,它可以在 GitHub 獲得(根據 MIT 許可證發佈)。
Lastly, in case you encounter any issues while using the API, you may consider approaching us for a quick resolution via free product support forum.
相關文章
請訪問以下鏈接以了解更多資訊: