Sample code for 30+ languages & platforms
DataFlex

Extract PDF from JSON

See more JSON Examples

Demonstrates how to extract a PDF file contained within JSON. The file is represented as a base64 string within the JSON. Note: This example can extract any type of file, not just a PDF file.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJson
    Variant vSb
    Handle hoSb
    Handle hoBd
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End

    // Load the JSON.
    Get ComLoadFile Of hoJson "qa_data/json/JSR5U.json" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoJson To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The JSON we loaded contains this:

    // 	{
    // 	...
    // 	...
    // 	  "data": {
    // 	    "content": "JVBERi0xLjQ..."
    // 	  }
    // 	...
    // 	...
    // 	}

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get pvComObject of hoSb to vSb
    Get ComStringOfSb Of hoJson "data.content" vSb To iSuccess

    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get pvComObject of hoSb to vSb
    Get ComAppendEncodedSb Of hoBd vSb "base64" To iSuccess

    Get ComWriteFile Of hoBd "qa_output/a0015.pdf" To iSuccess


End_Procedure