Process documents with Document Engine and Rust

This guide walks you through the steps necessary to start Document Engine. It also shows you how to use it to process documents. By the end, you’ll be able to merge two PDF documents into one using Document Engine’s HTTP API from Rust.

Requirements

Document Engine is compatible with a range of platforms. Below is the list of supported operating systems.

  • macOS:

    • Ventura
    • Monterey
    • Mojave
    • Catalina
    • Big Sur
  • Linux:

    • Ubuntu, Fedora, Debian, and CentOS
    • Ubuntu and Debian derivatives (such as Kubuntu, Xubuntu) are also supported

Processor requirements:

  • 64-bit Intel (x86_64) processors
  • ARM (AArch64) processors

Minimum system requirements:

  • At least 4GB of RAM, regardless of the operating system

Installing Docker

Document Engine is distributed as a Docker container. To run it on your computer, you need to install a Docker runtime distribution for your operating system.

Install and start Docker Desktop for Mac. For detailed instructions, refer to the Docker website(opens in a new tab).

Starting Document Engine

To start Document Engine, follow the steps below.

  1. Open your terminal emulator.

    Use the terminal emulator integrated with your code editor or IDE. Alternatively, you can use Terminal.app or iTerm2(opens in a new tab).

  2. Run the following command to start the Document Engine container:

Terminal window
docker run --rm -t -p 5000:5000 -e API_AUTH_TOKEN=secret pspdfkit/document-engine:1.10.0

This command may take some time to complete depending on your internet connection speed, as it needs to pull the Docker image. You’ll know that Document Engine is successfully running when you see a message similar to the following in your terminal:

[info] 2024-02-05 18:56:45.286 Running Document Engine version 1.10.0

Document Engine is now up and running!

Installing Rust

The interaction with Document Engine happens through its HTTP API. Documents and commands are sent in API request calls, and the resulting files are received in response. API calls are invoked from the Rust code, so you need to install Rust.

To install Rust:

  1. Follow the instructions for your operating system in Rust’s installation guide(opens in a new tab).

  2. Go to any directory in your system using your terminal. Create a new directory called merging-pdfs and go the newly created directory:

    mkdir merging-pdfs
    cd merging-pdfs
  3. Create a new cargo project:

    cargo new merging-pdfs-pspdfkit
  4. Run the project with the cargo run command.

Merging PDFs with Rust

If you don’t have any sample documents, download and use these files: cover.pdf and document.pdf.

Paste the following content into the Cargo.toml file in the merging-pdfs-pspdfkit project directory:

[dependencies]
tokio = { version = "1.23.0", features = ["full"] }
reqwest = { version = "0.11.13", features = ["json", "multipart"] }
serde_json = "1.0.91"

Next, replace /path/to/cover.pdf on line 24 and /path/to/document.pdf on line 30 with the actual paths to the example documents on your machine. Then, replace the contents of the src/main.rs file with this code:

use reqwest::Result;
use std::borrow::Cow;
use std::fs;
use std::fs::File;
use std::io::Write;
#[tokio::main]
async fn main() -> Result<()> {
// Multipart Request
let body = serde_json::json!({
"parts": [
{
"file": "cover"
},
{
"file": "document"
}
],
"output": {
"type": "pdf"
}
});
let cover = fs::read("src/cover.pdf").unwrap();
let cover_part = reqwest::multipart::Part::bytes(cover)
.file_name("cover.pdf")
.mime_str("application/pdf")
.unwrap();
let document = fs::read("src/document.pdf").unwrap();
let document_part = reqwest::multipart::Part::bytes(document)
.file_name("document.pdf")
.mime_str("application/pdf")
.unwrap();
let instructions = serde_json::to_vec(&body).unwrap();
let instructions_bytes = Cow::from(instructions);
let instructions_part = reqwest::multipart::Part::bytes(instructions_bytes);
let form = reqwest::multipart::Form::new()
.part("cover", cover_part)
.part("document", document_part)
.part("instructions", instructions_part);
let client = reqwest::Client::new();
let res = client
.post("http://localhost:5000/api/build")
.header("Authorization", "Token token=secret")
.multipart(form)
.send()
.await?;
let mut result_file = File::create("result.pdf").expect("Error creating file");
result_file
.write_all(&res.bytes().await.unwrap())
.expect("Error writing to file");
Ok(())
}

Most of this code deals with creating and sending a multipart request containing files and instructions to Document Engine’s /api/build endpoint using Rust’s reqwest crate.

To run the code, ensure you’re in the merging-pdfs directory and type the following command in your terminal:

cargo run
The result is a merged document with a cover page

To learn more about the various actions you can apply to PDFs using Document Engine, go to Document Engine’s API Reference.