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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
| package main
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"time"
)
func createReqBody(htmlContent string, uri string, pageLabel string, useCompression string, messageLabel string, browserHeight string, browserWidth string) (string, io.Reader, error) {
buf := new(bytes.Buffer)
bw := multipart.NewWriter(buf)
htmlContentField, _ := bw.CreateFormField("htmlContent")
htmlContentField.Write([]byte(htmlContent))
uriField, _ := bw.CreateFormField("uri")
uriField.Write([]byte(uri))
useCompressionField, _ := bw.CreateFormField("useCompression")
useCompressionField.Write([]byte(useCompression))
pageLabelField, _ := bw.CreateFormField("pageLabel")
pageLabelField.Write([]byte(pageLabel))
messageLabelField, _ := bw.CreateFormField("messageLabel")
messageLabelField.Write([]byte(messageLabel))
browserHeightField, _ := bw.CreateFormField("browserHeight")
browserHeightField.Write([]byte(browserHeight))
browserWidthField, _ := bw.CreateFormField("browserWidth")
browserWidthField.Write([]byte(browserWidth))
bw.Close()
return bw.FormDataContentType(), buf, nil
}
func submitPdfRenderTask(htmlContent string, uri string, rapidApiKey string) (string, error) {
contType, reader, err := createReqBody(htmlContent, uri, "Home Page", "false",
"Initial Capture", "768", "1024")
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", "https://web-renderer.p.rapidapi.com/SubmitPDFFromHTMLTask", reader)
req.Header.Add("Content-Type", contType)
req.Header.Add("X-RapidAPI-Key", rapidApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Cannot upload request to render error:", err)
return "", err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}
func getRenderingTaskStatus(taskId string, rapidApiKey string) (string, error) {
url := fmt.Sprintf("https://web-renderer.p.rapidapi.com/GetRenderingTaskStatus?taskId=%s", taskId)
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("X-RapidAPI-Key", rapidApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Cannot check rendering status of task with error:", err)
return "", err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}
func downloadResult(taskId string, rapidApiKey string) ([]byte, error) {
url := fmt.Sprintf("https://web-renderer.p.rapidapi.com/DownloadResult?taskId=%s", taskId)
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("X-RapidAPI-Key", rapidApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Cannot download rendered file with error:", err)
return nil, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return b, nil
}
func main() {
htmlContent := "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"></head><body><h1>Hello World!</h1></body></html>"
uriOfHtmlContent := "https://www.google.com?hl=en"
rapidApiKey := "<YOUR RAPIDAPI KEY HERE>"
taskId, err := submitPdfRenderTask(htmlContent, uriOfHtmlContent, rapidApiKey)
if err != nil {
panic(err)
}
retryCount := 0
for retryCount < 100 {
retryCount++
time.Sleep(5 * time.Second)
status, err := getRenderingTaskStatus(taskId, rapidApiKey)
if err != nil {
panic(err)
}
if status == "Completed" {
fileBytes, err := downloadResult(taskId, rapidApiKey)
if err != nil {
panic(err)
}
err = os.WriteFile("html_from_html.pdf", fileBytes, 0644)
if err != nil {
panic(err)
}
break
} else if status == "Waiting" {
continue
} else if status == "Failed" {
panic(errors.New("Cannot render html"))
} else {
panic(errors.New("Cannot render html"))
}
}
}
|