Quick Start for Rust
Rust has libraries for PDF generation and browser automation, but they often involve complex low-level APIs and setup requirements. PDFBolt's PDF generation API offers a streamlined HTTP-based approach for generating PDFs from HTML, URLs, and dynamic templates that integrates seamlessly with Rust's ecosystem.
1. Get Your API Key
- After signing up, locate your API Key in the Admin Dashboard under the API Keys section.
- This key is essential for authorizing your requests to the PDFBolt API. Remember to keep it secure.
2. Make Your First Request
Configure your requests using these examples to integrate with the PDFBolt API and generate PDFs.
Choose your preferred endpoint and source combination.
➡️ Endpoints:
- Direct
- Sync
- Async
The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.
➡️ Sources:
- URL
- HTML
- Template
Convert any webpage into a PDF:
use reqwest::{Client, Method, header::HeaderMap};
use serde_json::json;
use std::fs;
pub async fn make_request() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let url = "https://api.pdfbolt.com/v1/direct";
let mut headers = HeaderMap::new();
headers.insert("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".parse().unwrap());
headers.insert("Content-Type", "application/json".parse().unwrap());
let response = client.request(Method::POST, url)
.headers(headers)
.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(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
make_request().await?;
Ok(())
}
Convert HTML content directly into a PDF (HTML must be base64 encoded):
use reqwest::{Client, Method, header::HeaderMap};
use serde_json::json;
use base64::{Engine as _, engine::general_purpose};
use std::fs;
pub async fn make_request() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let url = "https://api.pdfbolt.com/v1/direct";
let html_content = "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>";
let base64_html = general_purpose::STANDARD.encode(html_content.as_bytes());
let mut headers = HeaderMap::new();
headers.insert("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".parse().unwrap());
headers.insert("Content-Type", "application/json".parse().unwrap());
let response = client.request(Method::POST, url)
.headers(headers)
.json(&json!({
"html": base64_html,
"margin": {
"top": "30px",
"left": "30px"
}
}))
.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("document.pdf", pdf_bytes)?;
println!("PDF generated successfully");
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
make_request().await?;
Ok(())
}
Use reusable templates for dynamic PDF generation:
use reqwest::{Client, Method, header::HeaderMap};
use serde_json::json;
use std::fs;
pub async fn make_request() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let url = "https://api.pdfbolt.com/v1/direct";
let mut headers = HeaderMap::new();
headers.insert("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".parse().unwrap());
headers.insert("Content-Type", "application/json".parse().unwrap());
let response = client.request(Method::POST, url)
.headers(headers)
.json(&json!({
"templateId": "your-template-id",
"templateData": {
"client_name": "John Doe",
"invoice_number": "INV-001",
"total_amount": "$299.99",
"line_items": [
{
"description": "Web Development",
"unit_price": "$200.00"
},
{
"description": "Design Services",
"unit_price": "$99.99"
}
]
}
}))
.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("invoice.pdf", pdf_bytes)?;
println!("PDF generated successfully");
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
make_request().await?;
Ok(())
}
Create your first template in the app, then use its ID in your API calls.
The Sync endpoint returns a JSON response with a download URL for the PDF.
➡️ Sources:
- URL
- HTML
- Template
Convert webpage and get download URL:
use reqwest::{Client, Method, header::HeaderMap};
use serde_json::{json, Value};
use std::error::Error;
pub async fn make_request() -> Result<(), Box<dyn Error>> {
let client = Client::new();
let url = "https://api.pdfbolt.com/v1/sync";
let mut headers = HeaderMap::new();
headers.insert("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".parse().unwrap());
headers.insert("Content-Type", "application/json".parse().unwrap());
let response = client.request(Method::POST, url)
.headers(headers)
.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 result: Value = response.json().await?;
println!("PDF URL: {}", result["documentUrl"].as_str().unwrap_or(""));
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
make_request().await?;
Ok(())
}
Convert HTML and get download URL:
use reqwest::{Client, Method, header::HeaderMap};
use serde_json::{json, Value};
use base64::{Engine as _, engine::general_purpose};
use std::error::Error;
pub async fn make_request() -> Result<(), Box<dyn Error>> {
let client = Client::new();
let url = "https://api.pdfbolt.com/v1/sync";
let html_content = "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>";
let base64_html = general_purpose::STANDARD.encode(html_content.as_bytes());
let mut headers = HeaderMap::new();
headers.insert("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".parse().unwrap());
headers.insert("Content-Type", "application/json".parse().unwrap());
let response = client.request(Method::POST, url)
.headers(headers)
.json(&json!({
"html": base64_html,
"margin": {
"top": "30px",
"left": "30px"
}
}))
.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 result: Value = response.json().await?;
println!("PDF URL: {}", result["documentUrl"].as_str().unwrap_or(""));
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
make_request().await?;
Ok(())
}
Use templates and get download URL:
use reqwest::{Client, Method, header::HeaderMap};
use serde_json::{json, Value};
use std::error::Error;
pub async fn make_request() -> Result<(), Box<dyn Error>> {
let client = Client::new();
let url = "https://api.pdfbolt.com/v1/sync";
let mut headers = HeaderMap::new();
headers.insert("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".parse().unwrap());
headers.insert("Content-Type", "application/json".parse().unwrap());
let response = client.request(Method::POST, url)
.headers(headers)
.json(&json!({
"templateId": "your-template-id",
"templateData": {
"client_name": "John Doe",
"invoice_number": "INV-001",
"total_amount": "$299.99",
"line_items": [
{
"description": "Web Development",
"unit_price": "$200.00"
},
{
"description": "Design Services",
"unit_price": "$99.99"
}
]
}
}))
.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 result: Value = response.json().await?;
println!("PDF URL: {}", result["documentUrl"].as_str().unwrap_or(""));
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
make_request().await?;
Ok(())
}
The Async endpoint processes requests asynchronously and sends results via webhook.
➡️ Sources:
- URL
- HTML
- Template
Convert webpage with webhook notification:
use reqwest::{Client, Method, header::HeaderMap};
use serde_json::{json, Value};
use std::error::Error;
pub async fn make_request() -> Result<(), Box<dyn Error>> {
let client = Client::new();
let url = "https://api.pdfbolt.com/v1/async";
let mut headers = HeaderMap::new();
headers.insert("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".parse().unwrap());
headers.insert("Content-Type", "application/json".parse().unwrap());
let response = client.request(Method::POST, url)
.headers(headers)
.json(&json!({
"url": "https://example.com",
"format": "A4",
"printBackground": true,
"webhook": "https://your-app.com/webhook"
}))
.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 result: Value = response.json().await?;
println!("Request ID: {}", result["requestId"].as_str().unwrap_or(""));
println!("PDF will be sent to webhook when ready");
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
make_request().await?;
Ok(())
}
Convert HTML with webhook notification:
use reqwest::{Client, Method, header::HeaderMap};
use serde_json::{json, Value};
use base64::{Engine as _, engine::general_purpose};
use std::error::Error;
pub async fn make_request() -> Result<(), Box<dyn Error>> {
let client = Client::new();
let url = "https://api.pdfbolt.com/v1/async";
let html_content = "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>";
let base64_html = general_purpose::STANDARD.encode(html_content.as_bytes());
let mut headers = HeaderMap::new();
headers.insert("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".parse().unwrap());
headers.insert("Content-Type", "application/json".parse().unwrap());
let response = client.request(Method::POST, url)
.headers(headers)
.json(&json!({
"html": base64_html,
"margin": {
"top": "30px",
"left": "30px"
},
"webhook": "https://your-app.com/webhook"
}))
.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 result: Value = response.json().await?;
println!("Request ID: {}", result["requestId"].as_str().unwrap_or(""));
println!("PDF will be sent to webhook when ready");
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
make_request().await?;
Ok(())
}
Use templates with webhook notification:
use reqwest::{Client, Method, header::HeaderMap};
use serde_json::{json, Value};
use std::error::Error;
pub async fn make_request() -> Result<(), Box<dyn Error>> {
let client = Client::new();
let url = "https://api.pdfbolt.com/v1/async";
let mut headers = HeaderMap::new();
headers.insert("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX".parse().unwrap());
headers.insert("Content-Type", "application/json".parse().unwrap());
let response = client.request(Method::POST, url)
.headers(headers)
.json(&json!({
"templateId": "your-template-id",
"templateData": {
"client_name": "John Doe",
"invoice_number": "INV-001",
"total_amount": "$299.99",
"line_items": [
{
"description": "Web Development",
"unit_price": "$200.00"
},
{
"description": "Design Services",
"unit_price": "$99.99"
}
]
},
"webhook": "https://your-app.com/webhook"
}))
.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 result: Value = response.json().await?;
println!("Request ID: {}", result["requestId"].as_str().unwrap_or(""));
println!("PDF will be sent to webhook when ready");
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
make_request().await?;
Ok(())
}
3. What's next?
Discover how PDFBolt can enhance your Rust applications. Start with the API Endpoints and explore the Conversion Parameters to tailor your integration for your specific needs.