FpCEF3

This is an old revision of FpCEF3, edited by Michl at . The current revision is 106263.

/* Windows */ added picture of file structure

Deutsch (de) | English (en)

Description

Chromium is a open source project (from Google), to create an own browser for different platforms. Among other things the browser Chrome is based on this source data.

The Chromium Embedded Framework (CEF) is an open source framework for embedding a browser control based on Chromium. It can be used on Linux, Mac OS X and Windows in 32 and 64 bit versions. CEF is there in the versions CEF1 and CEF3 where CEF1 is no longer maintained.

There is a framework for Delphi (Delphi Chromium Embedded Framework - DCEF3) and two branches, which can be used for Lazarus/FreePascal (fpCEF3 and WACEF).

Installation

In addition to Lazarus package even the framework itself must be downloaded and made available to the project. It is important that the framework for the package fits. Which it is and some more helpful installation instructions can be found at Github: https://github.com/dliw/fpCEF3

At the time of the revision of this description (October 2015), CEF 3.2454.1342 is the required framework.

Download Package

fpCEF3 with Git

git://github.com/dliw/fpCEF3.git

You can also download a zipped package:

master.zip

Package Installation

After downloading of package fpCEF3, it can be installed in Lazarus:

Download Framework CEF3

You can now download the framework and made the libraries available for a particular project. You can download it from the following pages:
https://cefbuilds.com
http://www.magpcss.net/cef_downloads (or a older version from here)

You'll be sure which framework version with the current fpCEF3 can be used! See Installation

Usage

Minimal example

A minimal example with a simple browser is created.

Windows

The CEF libraries are needed for loading, so it makes sense to create a new project folder, to save the project and the libraries in it together.

The files (libcef.dll, natives_blob.bin, icudtl.dat etc.) are in the same directory as the project and the directory locales in C:\SimpleBrowser\locales.
To prevent misunderstands, here is a picture how the file and directory structure of the Simplebrowser should look:

Now build the project with Lazarus

procedure TForm1.edtURLClick(Sender: TObject);
begin
  Chromium.Load(UTF8Decode(edtURL.Text));
end;
procedure TForm1.FormCreate(Sender: TObject);
var
  PrjPath: ustring;
  Lang, FallbackLang: string;
begin
  PrjPath := UTF8Decode(ExtractFilePath(ParamStr(0)));
  CefLocalesDirPath := PrjPath + 'locales';
  GetLanguageIDs(Lang, FallbackLang);
  CefLocale := UTF8Decode(FallbackLang);
  edtURL.Text:='http://www.lazarus-ide.org/';
end;
uses
  sysutils, Forms, EditBtn, cef3lcl, cef3intf, cef3types, cef3lib, gettext;

Show URL of loaded page

Based on Minimal example

procedure TForm1.ChromiumLoadEnd(Sender: TObject; const Browser: ICefBrowser;
  const Frame: ICefFrame; httpStatusCode: Integer);
begin
  edtURL.Text:=UTF8Encode(Browser.MainFrame.Url);
end;

Allow downloading of files

procedure TForm1.ChromiumBeforeDownload(Sender: TObject;
  const Browser: ICefBrowser; const downloadItem: ICefDownloadItem;
  const suggestedName: ustring; const callback: ICefBeforeDownloadCallback);
begin
  callback.Cont(suggestedName, True);
end;

Printing per code

procedure TForm1.Button1Click(Sender: TObject);
begin
  Chromium.Browser.Host.Print;
end;

Creating a pdf

procedure TForm1.Button1Click(Sender: TObject);
var
  CefPdfPrintSettings: TCefPdfPrintSettings;
  EmptyCefStringUtf16: TCefStringUtf16;
begin
  SaveDialog1.DefaultExt := 'pdf';
  SaveDialog1.Filter     := 'Portable Document Format (pdf)|*.pdf';
  if SaveDialog1.Execute then
  begin
    FillByte(EmptyCefStringUtf16, SizeOf(EmptyCefStringUtf16), 0);

    CefPdfPrintSettings.backgrounds_enabled := 1;
    CefPdfPrintSettings.header_footer_enabled := 0;
    CefPdfPrintSettings.header_footer_title := EmptyCefStringUtf16;
    CefPdfPrintSettings.header_footer_url := EmptyCefStringUtf16;
    CefPdfPrintSettings.landscape := 0;
    CefPdfPrintSettings.margin_bottom := 0;
    CefPdfPrintSettings.margin_left := 0;
    CefPdfPrintSettings.margin_right := 0;
    CefPdfPrintSettings.margin_top := 0;
    CefPdfPrintSettings.margin_type := PDF_PRINT_MARGIN_DEFAULT;
    CefPdfPrintSettings.page_height := 0;
    CefPdfPrintSettings.page_width := 0;
    CefPdfPrintSettings.selection_only := 0;

    Chromium.Browser.Host.PrintToPdf(UTF8Decode(SaveDialog1.FileName), CefPdfPrintSettings, Nil);
  end;
end;

The description of the TCefPdfPrintSettings you will find by the declaration of the TCefPdfPrintSettings

Known Problems

Further Informations

--Michl 22:58, 1 June 2015 (CEST)