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

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkFormats.pb"

Procedure ChilkatExample()

    success.i = 0

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

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"payload.text","https://www.chilkatsoft.com/")
    CkJsonObject::ckUpdateString(json,"output.format","svg")

    ;  An SVG is text, so it can be returned as text rather than as base64.
    CkJsonObject::ckUpdateString(json,"output.return","text")

    fmt.i = CkFormats::ckCreate()
    If fmt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkFormats::ckQrCode(fmt,"generate",json,jsonOut)
    If success = 0
        Debug CkFormats::ckLastErrorText(fmt)
        CkJsonObject::ckDispose(json)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    sbSvg.i = CkStringBuilder::ckCreate()
    If sbSvg.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckStringOfSb(jsonOut,"outputs[0].text",sbSvg)

    Debug "MIME type: " + CkJsonObject::ckStringOf(jsonOut,"outputs[0].mimeType")
    Debug "size: " + Str(CkJsonObject::ckIntOf(jsonOut,"outputs[0].width")) + " x " + Str(CkJsonObject::ckIntOf(jsonOut,"outputs[0].height")) + " " + CkJsonObject::ckStringOf(jsonOut,"outputs[0].units")
    Debug "SVG length: " + Str(CkStringBuilder::ckLength(sbSvg)) + " characters"

    ;  The SVG is a complete document that can be saved, or placed inline in an HTML page.
    success = CkStringBuilder::ckWriteFile(sbSvg,"qa_output/qr_chilkat.svg","utf-8",0)
    If success = 0
        Debug CkStringBuilder::ckLastErrorText(sbSvg)
        CkJsonObject::ckDispose(json)
        CkFormats::ckDispose(fmt)
        CkJsonObject::ckDispose(jsonOut)
        CkStringBuilder::ckDispose(sbSvg)
        ProcedureReturn
    EndIf

    ;  The SVG:

    ;  image

    ;  Sample output:

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


    CkJsonObject::ckDispose(json)
    CkFormats::ckDispose(fmt)
    CkJsonObject::ckDispose(jsonOut)
    CkStringBuilder::ckDispose(sbSvg)


    ProcedureReturn
EndProcedure