Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.7.0+

Generate a QR Code as SVG

See more QR Code Examples

Demonstrates SVG output. An SVG is a vector image, so it stays sharp at any size, which suits print and responsive web pages. Setting output.format to svg and output.return to text returns the SVG document as a string in outputs[0].text. The SVG can also be written straight to a file with output.path, the same as a PNG.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let json = chilkat::JsonObject::new();
let _ = json.update_string("payload.text", "https://www.chilkatsoft.com/");
let _ = json.update_string("output.format", "svg");

// An SVG is text, so it can be returned as text rather than as base64.
let _ = json.update_string("output.return", "text");

let fmt = chilkat::Formats::new();
let json_out = chilkat::JsonObject::new();
if fmt.qr_code("generate", &json, &json_out).is_err() {
    println!("{}", fmt.last_error_text());
    return;
}

let sb_svg = chilkat::StringBuilder::new();
let _ = json_out.string_of_sb("outputs[0].text", &sb_svg);

println!("MIME type: {}", json_out.string_of("outputs[0].mimeType").unwrap_or_default());
println!("size: {} x {} {}", json_out.int_of("outputs[0].width"), json_out.int_of("outputs[0].height"), json_out.string_of("outputs[0].units").unwrap_or_default());
println!("SVG length: {} characters", sb_svg.length());

// The SVG is a complete document that can be saved, or placed inline in an HTML page.
if sb_svg.write_file("qa_output/qr_chilkat.svg", "utf-8", false).is_err() {
    println!("{}", sb_svg.last_error_text());
    return;
}

// The SVG:

// image

// Sample output:

// MIME type: image/svg+xml
// size: 148 x 148 px
// SVG length: 12500 characters