Loading search indexβ¦
No recent searches
No results for "Query here"
# Cargo.toml [dependencies] reqwest = { version = "0.11", features = ["json"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" tokio = { version = "1", features = ["full"] }
use reqwest::Client; use serde::{Deserialize, Serialize}; use std::time::Duration; use std::env; #[derive(Serialize)] struct ConvertRequest { pdf_url: String, } #[derive(Deserialize)] struct ConvertResponse { data: ConvertData, msg: Option<String>, } #[derive(Deserialize)] struct ConvertData { convert_id: Option<String>, status: Option<String>, word_url: Option<String>, } const APP_API_KEY: &str = "sk-xxxxxx"; const CONVERT_API: &str = "https://api.pdf2document.com/api/v1/pdf2doc/convertapi"; #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let args: Vec<String> = env::args().collect(); if args.len() < 2 { println!("Usage: {} [command] [args]", args[0]); println!("Commands:"); println!(" convert [pdfUrl] Convert PDF to DOC with the given pdfUrl"); println!(" status [ID] Check conversion status with the given ID"); return Ok(()); } match args[1].as_str() { "convert" => { if args.len() < 3 { println!("Usage: {} convert [pdfUrl]", args[0]); return Ok(()); } let pdf_url = &args[2]; let convert_id = pdf_convert_api(pdf_url).await?; println!("Conversion started with ID: {}", convert_id); } "status" => { if args.len() < 3 { println!("Usage: {} status [ID]", args[0]); return Ok(()); } let convert_id = &args[2]; get_convert_status(convert_id).await?; } _ => println!("Invalid command: {}", args[1]), } Ok(()) } async fn pdf_convert_api(pdf_url: &str) -> Result<String, Box<dyn std::error::Error>> { let client = Client::new(); let data = ConvertRequest { pdf_url: pdf_url.parse().unwrap(), }; let res = client .post(CONVERT_API) .header("APPKEY", APP_API_KEY) .header("Content-Type", "application/json") .json(&data) .send() .await?; if res.status().is_success() { let response_data: ConvertResponse = res.json().await?; if let Some(convert_id) = response_data.data.convert_id { Ok(convert_id) } else { Err("No convert_id found".into()) } } else { Err(format!("Error: {}", res.status()).into()) } } async fn get_convert_status(convert_id: &str) -> Result<(), Box<dyn std::error::Error>> { let client = Client::new(); let convert_status = format!("https://api.pdf2document.com/api/v1/pdf2doc/convertapi/{}", convert_id); loop { let res = client .get(&convert_status) .header("APPKEY", APP_API_KEY) .header("Content-Type", "application/json") .send() .await?; if res.status().is_success() { let response_data: ConvertResponse = res.json().await?; match response_data.data.status.as_deref() { Some("doing") => { println!("Please wait while the transfer is underway..."); tokio::time::sleep(Duration::from_secs(60)).await; } Some("success") => { if let Some(word_url) = response_data.data.word_url { println!("The conversion is complete, and the download link for the file is: {}", word_url); break; } } _ => { if let Some(msg) = response_data.msg { println!("Error: {}", msg); break; } } } } else { println!("Error: {}", res.status()); break; } } Ok(()) }
#run cmd: cargo run 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. cargo run status 6703bdd637c17f67b1f9aabd# 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