Skip to main content

Quick Start for Rust

Rust

Integrate PDFBolt's REST API in Rust to generate PDFs from URLs, HTML, or templates. The examples below cover all three conversion modes (Direct, Sync, Async).

1. Get Your API Key

Find your API key on the API Keys page in your Dashboard. If you don't have an account, sign up – the free plan includes 100 document conversions per month.

2. Make Your First Request

Any HTTP client works – adjust the request structure to match your library.

Examples use the reqwest crate with tokio async runtime. Add to your Cargo.toml:

[dependencies]
reqwest = { version = "0.13", features = ["json"] }
serde_json = "1"
tokio = { version = "1", features = ["full"] }
base64 = "0.22"

Choose your endpoint:

The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.

Choose your source:

Convert a webpage to PDF:

use reqwest::Client;
use serde_json::json;
use std::fs;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let url = "https://api.pdfbolt.com/v1/direct";

let response = client.post(url)
.header("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
.header("Content-Type", "application/json")
.json(&json!({
"url": "https://example.com",
"format": "A4",
"printBackground": true
}))
.send()
.await?;

if !response.status().is_success() {
println!("HTTP {}", response.status().as_u16());
let error_text = response.text().await?;
println!("Error Message: {}", error_text);
return Ok(());
}

let pdf_bytes = response.bytes().await?;
fs::write("webpage.pdf", pdf_bytes)?;
println!("PDF generated successfully");

Ok(())
}

Next Steps



Related reading