Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.StringBuilder,
  Chilkat.BinData,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  json: TJsonObject;
  sb: TStringBuilder;
  bd: TBinData;

begin
  success := False;

  json := TJsonObject.Create;

  //  Load the JSON.
  success := json.LoadFile('qa_data/json/JSR5U.json');
  if (success <> True) then
    begin
      WriteLn(json.LastErrorText);
      Exit;
    end;

  //  The JSON we loaded contains this:

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

  sb := TStringBuilder.Create;
  json.StringOfSb('data.content',sb);

  bd := TBinData.Create;
  bd.AppendEncodedSb(sb,'base64');

  success := bd.WriteFile('qa_output/a0015.pdf');


  json.Free;
  sb.Free;
  bd.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.