In this tutorial we will cover how to implement asynchronous approach of Web Renderer. You only need to send HTML content to SubmitPDFFromHTMLTask or SubmitImageFromHTMLTask API methods to receive task identifier as response. There is also SubmitPDFFromUrlTask and SubmitImageFromUrlTask methods to render PDF or Image from public Web Uri.

After receiving task identifier you need to check task status by calling GetRenderingTaskStatus method. You need to call same method after a few seconds delay as long as task status equals to “Waiting”. Once you receive “Completed” from GetRenderingTaskStatus method you can call DownloadResult method to download final file. You can use the Visual Basic implementation below.

 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