HTML 到 XPS

将 HTML 转换为 XPS

超文本标记语言 (HTML) 是一种用于创建网页的标准标记语言。它允许使用 HTML 元素/标签创建和构建部分、段落和链接。如果在 HTML 文件中使用了某些自定义字体或引用了任何需要与源计算机/服务器建立活动连接的动态对象,则可能会损害文档的保真度。另一方面,在 XML 纸张规范 (XPS) 中,页面元素的定义独立于特定的操作系统、打印机或查看应用程序。因此,一种明智的方法是将 HTML 转换为 XPS 格式。

HTML 转换 API

Aspose.HTML Cloud SDK for Java 是一种基于 REST 架构的轻量级 API,提供创建、操作 HTML 文件并将其转换为 PDFDOCXTIFF、[JPEG] 的功能]6等,还支持HTML到XPS的转换。因此,首先,我们需要在 Maven 构建类型项目的 pom.xml 中添加以下详细信息,以将 SDK 包含在我们的 Java 项目中。

<repositories> 
    <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>http://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>

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

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

在 Java 中将 HTML 转换为 XPS

请按照下面指定的说明开发 HTML 到 XPS 转换器。

  • 首先,我们需要针对 Configuration.setAPPSID 和 Configuration.setAPIKEY 方法指定详细信息
  • 其次,我们设置 setBasePath(..)、setAuthPath(..) 的细节并指定 setUserAgent(…) 作为 WebKit
  • 第三,为了我们自己的帮助,我们将设置 setDebug(..) 为 true
  • 现在创建一个 ConversionApi 类的对象
  • 为结果文件的信息指定边距详细信息和名称
  • 最后,调用 GetConvertDocumentToXps(…) 方法,该方法需要输入 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 到 XPS 转换的 API
    retrofit2.Call<okhttp3.ResponseBody> call = htmlApi.GetConvertDocumentToXps(name, width, height, leftMargin, rightMargin, topMargin, bottomMargin, folder, storage);
      
    System.out.println("HTML to XPS conversion sucessfull !");
    }catch(Exception ex)
    {
        System.out.println(ex);
    }

上面的代码在响应流中返回结果,为了将输出保存在本地驱动器上,我们可以考虑使用以下自定义方法。

/*
* 以 ResponseBody 和生成的文件名作为参数的方法
*/
public static void checkAndSave(retrofit2.Call<okhttp3.ResponseBody> call, String fileName) throws IOException 
	{
	    // 同步发送请求并返回其响应。
	    retrofit2.Response<okhttp3.ResponseBody> res = call.execute();
	    
	    // 成功响应的反序列化响应体
	    okhttp3.ResponseBody answer = res.body();
	    
	    //保存到测试目录
	    boolean result = saveToDisc(answer, fileName);
	    
	    // 验证结果值是否为真(可选步骤)
	    Assert.assertTrue(result);
	}
	
  /*
  *
  * 调用此方法将响应内容保存为本地驱动器上的文件
  *
  */ 
	public static boolean saveToDisc(okhttp3.ResponseBody body, String fileName) 
	{
            // 创建文件对象指示结果文件的位置
	    File savedFile = new File("c:\\Downloads\\"+fileName);
      
	    try (InputStream inputStream = body.byteStream();
	    OutputStream outputStream = new FileOutputStream(savedFile))
	    {
	    byte[] fileReader = new byte[4096];
	    long fileSizeDownloaded = 0;

	    while (true) {
	        int read = inputStream.read(fileReader);
		if (read == -1) break;

                // 将文件流保存到本地驱动器
		outputStream.write(fileReader, 0, read);
		fileSizeDownloaded += read;
            }
	    // 清除输出流实例
	    outputStream.flush();
	    
	    // 文件保存成功返回true
	    return true;
	    } catch (Exception e) {
		e.printStackTrace();
		return false;
	    }
	} // saveToDisc ends here
HTML 到 XPS

Image1:- HTML 到 XPS 预览

上例中使用的示例文件可以从 list.htmlresultantFile.xps 下载。

使用 cURL 命令将 HTML 转换为 XPS

REST API 也可以通过 cURL 命令访问,因此在本节中,我们将学习如何使用 cURL 命令执行 HTML 到 XPS 转换的步骤。现在作为先决条件,我们需要首先根据您的个人客户端凭据生成一个 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 到 XPS 的转换。

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

结论

本文详细介绍了使用 REST API 将 HTML 转换为 XPS。我们已经学习了使用 Java 代码片段以及通过 cURL 命令将 HTML 转换为 XPS 的步骤。此外,请注意产品 文档 是学习 API 提供的惊人功能的重要资源。此外,如果您在使用 API 时遇到任何问题,请随时联系免费产品支持论坛

相关文章

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