在本教程中,我们将介绍如何实现 Web 呈现器的异步方法。您只需要将 HTML 内容发送到 SubmitPDFFromHTMLTask 或 SubmitImageFromHTMLTask API 方法即可接收任务标识符作为响应。还有SubmitPDFFromUrlTask和SubmitImageFromUrlTask方法,用于从公共Web Uri呈现PDF或图像。

收到任务标识符后,您需要通过调用 GetRenderingTaskStatus 方法来检查任务状态。只要任务状态等于“Waiting”,您需要在几秒钟延迟后调用相同的方法。一旦你从GetRenderingTaskStatus方法收到“Completed”,你可以调用DownloadResult方法来下载最终文件。您可以使用下面的 Visual Basic 实现。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Imports System
Imports System.IO
Imports System.Net.Http

Module Program
    Async Function SubmitPDFFromHTMLTask(htmlContent As String, uri As String, rapidApiKey As String) As Task(Of String)
        Using httpClient As New HttpClient()
            Using form As New MultipartFormDataContent()

                form.Headers.Add("X-RapidAPI-Key", rapidApiKey)
                form.Add(New StringContent(uri), "uri")
                form.Add(New StringContent("false"), "useCompression")
                form.Add(New StringContent("Home Page"), "pageLabel")
                form.Add(New StringContent("Initial Capture"), "messageLabel")
                form.Add(New StringContent("768"), "browserHeight")
                form.Add(New StringContent("1024"), "browserWidth")
                form.Add(New StringContent(htmlContent), "htmlContent")
                Using response = Await httpClient.PostAsync("https://web-renderer.p.rapidapi.com/SubmitPDFFromHTMLTask", form)

                    response.EnsureSuccessStatusCode()
                    Return Await response.Content.ReadAsStringAsync()
                End Using
            End Using
        End Using
    End Function
    Async Function GetRenderingTaskStatus(taskId As String, rapidApiKey As String) As Task(Of String)
        Using httpClient As New HttpClient()
            httpClient.DefaultRequestHeaders.Add("X-RapidAPI-Key", rapidApiKey)
            Using response = Await httpClient.GetAsync($"https://web-renderer.p.rapidapi.com/GetRenderingTaskStatus?taskId={taskId}")

                response.EnsureSuccessStatusCode()
                Return Await response.Content.ReadAsStringAsync()
            End Using
        End Using
    End Function
    Async Function DownloadResult(taskId As String, rapidApiKey As String) As Task(Of Byte())
        Using httpClient As New HttpClient()
            httpClient.DefaultRequestHeaders.Add("X-RapidAPI-Key", rapidApiKey)
            Using response = Await httpClient.GetAsync($"https://web-renderer.p.rapidapi.com/DownloadResult?taskId={taskId}")

                response.EnsureSuccessStatusCode()
                Return Await response.Content.ReadAsByteArrayAsync()
            End Using
        End Using
    End Function
    Public Async Function RenderHtmlAsync() As Task
        Dim rapidApiKey = "<YOUR RAPIDAPI KEY HERE>"
        Dim htmlContent = "<HTML CONTENT TO RENDER>"
        Dim uriOfHtmlContent = "https://www.google.com?hl=en"
        Dim taskId = Await SubmitPDFFromHTMLTask(htmlContent, uriOfHtmlContent, rapidApiKey)
        Dim retryCount = 0
        Do While (retryCount < 100)
            retryCount += 1
            Await Task.Delay(5000)
            Dim status = Await GetRenderingTaskStatus(taskId, rapidApiKey)
            If (status = "Completed") Then
                Dim fileBytes = Await DownloadResult(taskId, rapidApiKey)
                Await File.WriteAllBytesAsync("html_from_html.pdf", fileBytes)
                Exit Do
            ElseIf (status = "Waiting") Then
                Continue Do
            ElseIf (status = "Failed") Then
                Throw New Exception("Cannot convert file")
            Else
                Throw New Exception("Invalid status")
            End If
        Loop
    End Function
    Sub Main(args As String())
        RenderHtmlAsync().Wait()
    End Sub
End Module