Code sample for add/barcode API

Rust


This Rust sample demonstrates how to access the PDFTeam add/barcode API endpoint. The sample takes an existing PDF and adds two barcodes: A QR code is positioned in the top right, a Code 128 in the middle left. Both codes are created without text line, the Code 128 barcode is rotated 90 degrees. The resulting PDF will be stored in AWS S3, the permalink is returned to the caller.


We are using the reqwest-module with tokio. serde is used to parse the JSON parameters which are loaded from a separate file.

Please see the dependencies below:

// Sample Rust code for accessing the PDFTeam API, endpoint: add/barcode
// File: cargo.toml

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

{
  "job": "00012345",
  "user": "YOUR_USERNAME",
  "key": "YOUR_APIKEY",
  "test": false,
  "inputOptions": {
    "source": "https://pdfteam.com/samples/invoice.pdf"
  },
  "outputOptions": {
    "storage": true,
    "file": "addbarcode_rust.pdf"
  },
  "overlays": [
    {
      "position": {
        "x": "179mm",
        "y": "237mm"
      },
      "type": "barcode",
      "config": {
        "data": "https://pdfteam.com"
      }
    },
    {
      "rotate": 90,
      "position": {
        "x": "10mm",
        "y": "middle"
      },
      "type": "barcode",
      "config": {
        "humanReadableText": false,
        "type": 7,
        "moduleHeight": 10,
        "data": "00012345"
      }
    }
  ]
}


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/barcode
// File: main.rs

use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    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();
    let res = client
        .post("https://api.pdfteam.com/api/add/barcode")
        .json(&params_json)
        .send()
        .await?
        .text()
        .await?;

    println!("{}", res);
    Ok(())
}


Sample response:

{"endpoint": "add/barcode", "job": "00012345", "credits": 1, "result": "https://pdfteam.s3.eu-north-1.amazonaws.com/.../addbarcode_rust.pdf"}


Sample result:

PDF with QR code and a Code 128 barcode created in Rust