Code Sample for html/2/pdf API

Rust


This Rust code demonstrates how to access the PDFTeam html/2/pdf API endpoint. This endpoint loads HTML input (either as a URL or directly as a string) and converts it to PDF. In this case, we convert an HTML invoice to a PDF and add watermarks with some text, a company logo and a QR code. The resulting PDF will be stored in AWS S3, the permalink is then returned to the caller.

We are using Rust's reqwest-module with the asynchronous tokio runtime. The POST parameters are loaded from a separate file and are then parsed with serde into a JSON object. Note that setting the json content of the reqwest-client adds the required POST content headers automatically; this is different from Node's fetch or Python's request where you have to set the headers explicitly.

Dependencies are listed in cargo.toml as follows:

// Sample Rust code for accessing the PDFTeam API, endpoint: html/2/pdf
// File: cargo.toml

[package]
name = "html-2-pdf"
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: html/2/pdf
// File: params.json

{
  "job": "ACME INVOICE",
  "user": "YOUR_USERNAME",
  "key": "YOUR_APIKEY",
  "test": true,
  "inputOptions": {
    "source": "https://pdfteam.com/samples/invoice.html"
  },
  "outputOptions": {
    "storage": true,
    "file": "invoice_rust.pdf"
  },
  "pdfOptions": {
    "format": "Letter"
  },
  "overlays": [
    {
      "position": {
        "x": "left",
        "y": "30mm"
      },
      "type": "barcode",
      "config": {
        "type": 19,
        "data": "https://pdfteam.com"
      }
    },
    {
      "rotate": 45,
      "position": {
        "x": "center",
        "y": "middle"
      },
      "type": "text",
      "config": {
        "data": "PAST DUE",
        "font": "Helvetica Bold",
        "size": 72,
        "color": "#ff0000",
        "opacity": 0.5
      }
    },
    {
      "position": {
        "x": "132mm",
        "y": "228mm"
      },
      "type": "image",
      "config": {
        "source": "https://pdfteam.com/samples/acme_logo.png"
      }
    }
  ]
}


NOTE: Setting "test" to TRUE will not consume any credits but the generated PDF carries a watermark. This flag is intended to dial in settings, like positions or style of watermarks.


Main program:

// Sample Rust code for accessing the PDFTeam API, endpoint: html/2/pdf
// File: main.rs

use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Read params and parse into JSON object
    let params = std::fs::read_to_string("params.json").unwrap();
    let params_json: serde_json::Value = serde_json::from_str(&params).expect("Malformed JSON");
    let client = reqwest::Client::new();
    // Call API endpoint, json() automatically adds req'd POST headers
    let res = client
        .post("https://api.pdfteam.com/api/html/2/pdf")
        .json(&params_json)
        .send()
        .await?
        .text()
        .await?;
    // Print result
    println!("{}", res);
    Ok(())
}


Sample response with the URL of the PDF:

{"endpoint": "html/2/pdf", "job": "ACME INVOICE", "credits": 1, "result": "https://pdfteam.s3.eu-north-1.amazonaws.com/.../invoice_rust.pdf"}


Sample result:

PDF Invoice with QR, watermark, logo converted from HTML with Rust