Code sample for add/text
API
Rust
This Rust sample demonstrates how to access the PDFTeam add/text
API endpoint. The sample loads a PDF with a report and adds two text watermarks: a text 'TOP SECRET' in red, centered on the page and rotated 45 degrees and a "Request for Comments"-header near the top of the page in blue. The resulting PDF will be stored in AWS S3, the document's permalink is then returned to the caller.
We are using Rust's reqwest
-module with the tokio
runtime. The POST parameters are loaded from a separate file which are then parsed with serde
.
Dependencies are listed in cargo.toml
:
// Sample Rust code for accessing the PDFTeam API, endpoint: add/text
// File: cargo.toml
[package]
name = "add-text"
version = "0.1.0"
edition = "2024"
[dependencies]
reqwest = {version = "0.12.15", features = ["blocking","json"]}
tokio = { version = "1.44.2", features = ["full"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
POST parameters are in a separate file:
// Sample Rust code for accessing the PDFTeam API, endpoint: add/text
// File: params.json
{
"job": "00012345",
"user": "YOUR_USERNAME",
"key": "YOUR_APIKEY",
"test": true,
"inputOptions": {
"source": "https://pdfteam.com/samples/coyote_report.pdf"
},
"outputOptions": {
"storage": true,
"file": "processed_report_rust.pdf"
},
"overlays": [
{
"rotate": 45,
"position": {
"x": "center",
"y": "middle",
},
"firstPageOnly": false,
"type": "text",
"config": {
"data": "TOP SECRET",
"font": "Helvetica Bold",
"size": 72,
"color": "#ff0000",
"opacity": 0.5,
},
},
{
"position": {
"x": "center",
"y": "270mm",
},
"firstPageOnly": false,
"type": "text",
"config": {
"data": "Send comments and suggestions to fatalon@coyoteduty.com. Deadline is April, 10.",
"font": "Helvetica Bold",
"size": 12,
"color": "#0000FF",
},
}
]
}
NOTE: Setting "test" to TRUE will not consume any credits but the generated PDF carries a watermark.
Main program:
// Sample Rust code for accessing the PDFTeam API, endpoint: add/text
// File: main.rs
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Read POST params as string
let params = std::fs::read_to_string("params.json").unwrap();
// Parse into JSON object
let params_json: serde_json::Value = serde_json::from_str(¶ms).expect("Malformed JSON");
let client = reqwest::Client::new();
let res = client
.post("https://api.pdfteam.com/api/add/text")
.json(¶ms_json) // NOTE: json() will also set the content header properly
.send()
.await?
.text()
.await?;
println!("{}", res);
Ok(())
}
Sample response:
{"endpoint": "add/text", "job": "00012345", "credits": 1, "result": "https://pdfteam.s3.eu-north-1.amazonaws.com/.../processed_report_rust.pdf"}
Sample result: