Sample code for 30+ languages & platforms
Tcl 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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set json [new_CkJsonObject]

CkJsonObject_UpdateString $json "payload.text" "https://www.chilkatsoft.com/"
CkJsonObject_UpdateString $json "output.format" "svg"

#  An SVG is text, so it can be returned as text rather than as base64.
CkJsonObject_UpdateString $json "output.return" "text"

set fmt [new_CkFormats]

set jsonOut [new_CkJsonObject]

set success [CkFormats_QrCode $fmt "generate" $json $jsonOut]
if {$success == 0} then {
    puts [CkFormats_lastErrorText $fmt]
    delete_CkJsonObject $json
    delete_CkFormats $fmt
    delete_CkJsonObject $jsonOut
    exit
}

set sbSvg [new_CkStringBuilder]

CkJsonObject_StringOfSb $jsonOut "outputs[0].text" $sbSvg

puts "MIME type: [CkJsonObject_stringOf $jsonOut {outputs[0].mimeType}]"
puts "size: [CkJsonObject_IntOf $jsonOut {outputs[0].width}] x [CkJsonObject_IntOf $jsonOut {outputs[0].height}] [CkJsonObject_stringOf $jsonOut {outputs[0].units}]"
puts "SVG length: [CkStringBuilder_get_Length $sbSvg] characters"

#  The SVG is a complete document that can be saved, or placed inline in an HTML page.
set success [CkStringBuilder_WriteFile $sbSvg "qa_output/qr_chilkat.svg" "utf-8" 0]
if {$success == 0} then {
    puts [CkStringBuilder_lastErrorText $sbSvg]
    delete_CkJsonObject $json
    delete_CkFormats $fmt
    delete_CkJsonObject $jsonOut
    delete_CkStringBuilder $sbSvg
    exit
}

#  The SVG:

#  image

#  Sample output:

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

delete_CkJsonObject $json
delete_CkFormats $fmt
delete_CkJsonObject $jsonOut
delete_CkStringBuilder $sbSvg