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

PDF File Encoding to Base64

See more Base64 Examples

Demonstrates how to encode a PDF file to base64, and then decode.

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.BinData;

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

procedure RunDemo;
var
  success: Boolean;
  pdfData: TBinData;
  b64: string;
  pdfData2: TBinData;

begin
  success := False;

  pdfData := TBinData.Create;

  success := pdfData.LoadFile('qa_data/helloWorld.pdf');
  if (success <> True) then
    begin
      WriteLn('failed to load PDF file.');
      Exit;
    end;

  //  Encode the PDF to base64
  //  Note: to produce base64 on multiple lines (as it would appear in the MIME of an email),
  //  pass the string "base64_mime" instead of "base64".
  b64 := pdfData.GetEncoded('base64');
  WriteLn(b64);

  //  Decode from base64 PDF.
  pdfData2 := TBinData.Create;
  pdfData2.AppendEncoded(b64,'base64');
  success := pdfData2.WriteFile('qa_output/helloWorld2.pdf');
  if (success <> True) then
    begin
      WriteLn('failed to write PDF file.');
      Exit;
    end;


  pdfData.Free;
  pdfData2.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.