Last active
April 20, 2024 08:50
-
-
Save pdfcrowd/4da1f3ee6d039d575c098537d05048bb to your computer and use it in GitHub Desktop.
Convert a web page or HTML file to PDF in Rust. Complete tutorial: https://pdfcrowd.com/blog/convert-html-to-pdf-in-rust/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fs::File; | |
use std::error::Error; | |
use reqwest::blocking::Client; | |
use reqwest::blocking::multipart::{Form, Part}; | |
// API endpoint and Pdfcrowd credentials | |
static API_ENDPOINT: &'static str = "https://api.pdfcrowd.com/convert/latest/"; | |
static USERNAME: &'static str = "demo"; | |
static API_KEY: &'static str = "ce544b6ea52a5621fb9d55f8b542d14d"; | |
// conversion function as a multipart post to Pdfcrowd API servers | |
fn convert(fields: Vec<(&str, &str)>, files: Vec<(&str, &str)>, output_filename: &str) -> Result<(), Box<dyn Error>> { | |
// create multipart form | |
let mut form = Form::new(); | |
// set fields to upload | |
for (key, value) in fields { | |
let part = Part::text(value.to_string()).mime_str("text/plain")?; | |
form = form.part(key.to_string(), part); | |
} | |
// set files to upload | |
for (key, filename) in files { | |
form = form.file(key.to_string(), filename)?; | |
} | |
// create client and make the POST request | |
let client = Client::builder().build()?; | |
let mut response = client.post(API_ENDPOINT) | |
.multipart(form) | |
.basic_auth(USERNAME.to_string(), Some(API_KEY)) | |
.send()?; | |
// check the HTTP response | |
if !response.status().is_success() { | |
eprintln!("Pdfcrowd Error Code: {}", response.status().as_u16()); | |
eprintln!("Pdfcrowd Error Details: {}", response.text()?); | |
return Err("Unexpected conversion error".into()); | |
} | |
// write the output to a local file | |
let mut file = File::create(output_filename)?; | |
response.copy_to(&mut file)?; | |
Ok(()) | |
} | |
fn convert_url_example() -> Result<(), Box<dyn Error>> { | |
let options = vec![ | |
("input_format", "html"), | |
("output_format", "pdf"), | |
("page_size", "letter"), | |
("url", "https://example.com/") | |
]; | |
convert(options, vec![], "example_url.pdf") | |
} | |
fn convert_text_example() -> Result<(), Box<dyn Error>> { | |
let options = vec![ | |
("input_format", "html"), | |
("output_format", "pdf"), | |
("page_size", "letter"), | |
("text", "<h1>Hello from Pdfcrowd</h1>") | |
]; | |
convert(options, vec![], "example_text.pdf") | |
} | |
fn convert_file_example() -> Result<(), Box<dyn Error>> { | |
let options = vec![ | |
("input_format", "html"), | |
("output_format", "pdf"), | |
("page_size", "letter") | |
]; | |
let files = vec![ | |
("file", "your-file.html") | |
]; | |
convert(options, files, "example_file.pdf") | |
} | |
fn main() -> Result<(), Box<dyn Error>> { | |
convert_url_example()?; | |
convert_text_example()?; | |
convert_file_example()?; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment