Loading search indexβ¦
No recent searches
No results for "Query here"
package main import ( "bytes" "encoding/json" "flag" "fmt" "io" "net/http" "time" ) var ( appKey = "sk-xxxxxx" convertApi = "https://api.pdf2document.com/api/v1/pdf2doc/convertapi" convertStatus = "https://api.pdf2document.com/api/v1/pdf2doc/convertapi/%s" ) type ConvertResponse struct { Data struct { ConvertID string `json:"convert_id"` Status string `json:"status"` WordURL string `json:"word_url"` } `json:"data"` Msg string `json:"msg"` } func pdfConvertApi(convertUrl string) { headers := map[string]string{ "APPKEY": appKey, "Content-Type": "application/json", } data := map[string]string{ "pdf_url": convertUrl, } jsonData, _ := json.Marshal(data) client := &http.Client{} req, _ := http.NewRequest("POST", convertApi, bytes.NewBuffer(jsonData)) for key, value := range headers { req.Header.Set(key, value) } resp, err := client.Do(req) if err != nil { fmt.Println("Error:", err) return } defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { } }(resp.Body) if resp.StatusCode == 200 { var response ConvertResponse err := json.NewDecoder(resp.Body).Decode(&response) if err != nil { return } println(response.Data.ConvertID) } else { fmt.Println("Error:", resp.StatusCode) return } } func getConvertStatus(convertID string) { headers := map[string]string{ "APPKEY": appKey, "Content-Type": "application/json", } client := &http.Client{} req, _ := http.NewRequest("GET", fmt.Sprintf(convertStatus, convertID), nil) for key, value := range headers { req.Header.Set(key, value) } resp, err := client.Do(req) if err != nil { fmt.Println("Error:", err) return } defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { } }(resp.Body) if resp.StatusCode == 200 { var response ConvertResponse err := json.NewDecoder(resp.Body).Decode(&response) if err != nil { return } if response.Data.Status == "doing" { fmt.Println("Please wait while the transfer is underway...") time.Sleep(1 * time.Minute) getConvertStatus(convertID) } else if response.Data.Status == "success" { fmt.Println("The conversion is complete, and the download link for the file isοΌ", response.Data.WordURL) } else { fmt.Println(response) } } else { fmt.Println("Error:", resp.StatusCode) } } func main() { convert := flag.String("convert", "", "Start a PDF conversion with the given pdfUrl") status := flag.String("status", "", "Check conversion status for the given ID") flag.Parse() if *convert != "" { pdfConvertApi(*convert) } else if *status != "" { getConvertStatus(*status) } else { fmt.Println("No valid command provided.") fmt.Println("Usage:") flag.PrintDefaults() } }
#run cmd: go run main.go -convert https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf #return task id 670555fab6776ae0982590a6 # Query the conversion status by task ID. go run main.go -status 6703c748b6776ae0982590a3 # Please follow the link below to indicate that the conversion was successful. The conversion is complete, and the download link for the file isοΌ https://api.pdf2document.com/api/v2/docx?md5_id=670555fab6776ae0982590a6