Sign in Join
Wiki copytemporary

fpCEF3: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: fpCEF3

OldNewLine
1+## Description
2+
3+<img src="SimpleChromium.png" title="SimpleChromium.png" width="320" />
4+
5+Chromium is an open source project (from Google) to create a browser for different platforms. Among other things the browser Chrome is based on it.
6+
7+The Chromium Embedded Framework (CEF) is an open source framework for embedding Chromium into user applications. It can be used on Linux, macOS and Windows in 32 and 64 bit versions. Originally there were two versions of CEF, CEF1 and CEF3, but CEF1 is no longer maintained.
8+
9+CEF can be used in Delphi via the Delphi Chromium Embedded Framework (DCEF3). For Lazarus/Free Pascal there is the equivalent framework [fpCEF3](https://github.com/dliw/fpCEF3) (or alternative [CEF4Delphi](https://github.com/salvadordf/CEF4Delphi) with examples for Lazarus including newest versions of Chromium Embedded Framework).
10+
11+## Installation
12+
13+Besides the Lazarus package, the CEF framework itself is needed. It is important to use the correct version of the CEF framework for the fpCEF release used. Further specific installation instructions can be found on Github: [<https://github.com/dliw/fpCEF3>](https://github.com/dliw/fpCEF3)
14+
15+The latest release and its corresponding CEF version can be found on [1](https://github.com/dliw/fpCEF3/releases/latest)
16+
17+CEF releases can be found on <https://cef-builds.spotifycdn.com/index.html> (use the version filter or click "Show more builds" to find the release)
18+
19+### Download Package
20+
21+fpCEF3 with [Git](http://git-scm.com/)
22+
23+[`git://github.com/dliw/fpCEF3.git`](git://github.com/dliw/fpCEF3.git)
24+
25+You can also download a zipped package:
26+
27+[`master.zip`](https://github.com/dliw/fpCEF3/archive/master.zip)
28+
29+### Package Installation
30+
31+![](/wiki-images/3/33/Chromium.png)
32+
33+After downloading of package fpCEF3, it can be installed in Lazarus:
34+
35+- Start Lazarus and open main menu *Package* -\> *Open package file (.lpk)...*
36+- Select the file *cef3.lpk* in the fpCEF3 subdirectory *Component*
37+- The package editing window will open
38+- Click on *Compile*, the package will be compiled
39+- Click on *Use...*-\> *Install* and confirm the dialog window with *Yes*
40+- Lazarus is now compiling and create a new tab *Chromium* in the component palette
41+
42+### Download Framework CEF3
43+
44+You can now download the framework and made the libraries available for a particular project. You can download it from the following pages:\
45+[<https://cefbuilds.com>](https://cefbuilds.com)\
46+[<http://www.magpcss.net/cef_downloads>](http://www.magpcss.net/cef_downloads) (or a older version from [here](http://www.magpcss.net/cef_downloads/index.php?query=label%3ADeprecated#list))
47+
48+> **Note:** Make sure to use the correct framework version for the fpCEF version! See Installation
49+
50+## Usage
51+
52+### Minimal example
53+
54+A minimal example with a simple browser is created.
55+
56+#### Windows
57+
58+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.
59+
60+- Create a new directory for the project to a location of your choice such as *C:\SimpleBrowser*
61+- Copy the complete CEF3 directory *Resources* to *C:\SimpleBrowser*
62+- Copy the contents of the CEF3 directory *Release* in the directory *C:\SimpleBrowser*
63+
64+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*.\
65+To prevent misunderstands, here is a picture how the file and directory structure of the Simplebrowser should look:\
66+
67+![](/wiki-images/a/aa/FilesForSimplebrowser.png)\
68+
69+Now build the project with Lazarus
70+
71+- Create a new empty [[Form Tutorial|GUI application]]
72+- Save the project in the directory you specify for Windows (e.g. under *C:\SimpleBrowser*)
73+- Drop a `TEditButton` on the form
74+- With Object Inspector set the `Name` to `edtURL`, `Align` on `alTop` and `ButtonCaption` to `go`
75+- Put a `TChromium` on the form and set the `Name` to `Chromium` and its `Align` to `alClient`
76+- Create the `OnButtonClick` event handler for the TEditButton `edtURL`. Select the `OnButtonClick` in the Object Inspector tab `events` and click on the button \[...\]
77+- Write following code (chromium should open at each click the URL in the text box):
78+
79+``` pascal
80+procedure TForm1.edtURLClick(Sender: TObject);
81+begin
82+ Chromium.Load(UTF8Decode(edtURL.Text));
83+end;
84+```
85+
86+- `Chromium` must be initialized at the start of the program, you can do this with the `OnCreate` event handler of the form:
87+
88+``` pascal
89+procedure TForm1.FormCreate(Sender: TObject);
90+var
91+ PrjPath: ustring;
92+ Lang, FallbackLang: string;
93+begin
94+ PrjPath := UTF8Decode(GetCurrentDir + PathDelim);
95+ CefLocalesDirPath := PrjPath + 'locales';
96+ GetLanguageIDs(Lang, FallbackLang);
97+ CefLocale := UTF8Decode(FallbackLang);
98+ edtURL.Text:='http://www.lazarus-ide.org/';
99+end;
100+```
101+
102+- If a identifier wouldn't be found, you have to put some units to the uses clause. These units should be in there:
103+
104+``` pascal
105+uses
106+ sysutils, Forms, EditBtn, cef3lcl, cef3intf, cef3types, cef3lib, gettext;
107+```
108+
109+- Run your program, if all goes well it might look like:
110+
111+![](/wiki-images/b/b4/SimpleChromium.png)\
112+
113+### Show URL of loaded page
114+
115+Based on Minimal example
116+
117+- Create the `OnLoadEnd` event handler for `Chromium`
118+- Write following code (URL should be updated after any browsing):
119+
120+``` pascal
121+procedure TForm1.ChromiumLoadEnd(Sender: TObject; const Browser: ICefBrowser;
122+ const Frame: ICefFrame; httpStatusCode: Integer);
123+begin
124+ edtURL.Text:=UTF8Encode(Browser.MainFrame.Url);
125+end;
126+```
127+
128+### Allow downloading of files
129+
130+- Create the `OnBeforeDownload` event handler for `Chromium` and write in it:
131+
132+``` pascal
133+procedure TForm1.ChromiumBeforeDownload(Sender: TObject;
134+ const Browser: ICefBrowser; const downloadItem: ICefDownloadItem;
135+ const suggestedName: ustring; const callback: ICefBeforeDownloadCallback);
136+begin
137+ callback.Cont(suggestedName, True);
138+end;
139+```
140+
141+### Printing per code
142+
143+- Add a `TButton` on your form and write in its `OnClick` event handler:
144+
145+``` pascal
146+procedure TForm1.Button1Click(Sender: TObject);
147+begin
148+ Chromium.Browser.Host.Print;
149+end;
150+```
151+
152+### Creating a pdf
153+
154+- Add a `TSaveDialog` on your form
155+- Add a `TButton` on your form and write in its `OnClick` event handler:
156+
157+``` pascal
158+procedure TForm1.Button1Click(Sender: TObject);
159+var
160+ CefPdfPrintSettings: TCefPdfPrintSettings;
161+ EmptyCefStringUtf16: TCefStringUtf16;
162+begin
163+ SaveDialog1.DefaultExt := 'pdf';
164+ SaveDialog1.Filter := 'Portable Document Format (pdf)|*.pdf';
165+ if SaveDialog1.Execute then
166+ begin
167+ FillByte(EmptyCefStringUtf16, SizeOf(EmptyCefStringUtf16), 0);
168+
169+ CefPdfPrintSettings.backgrounds_enabled := 1;
170+ CefPdfPrintSettings.header_footer_enabled := 0;
171+ CefPdfPrintSettings.header_footer_title := EmptyCefStringUtf16;
172+ CefPdfPrintSettings.header_footer_url := EmptyCefStringUtf16;
173+ CefPdfPrintSettings.landscape := 0;
174+ CefPdfPrintSettings.margin_bottom := 0;
175+ CefPdfPrintSettings.margin_left := 0;
176+ CefPdfPrintSettings.margin_right := 0;
177+ CefPdfPrintSettings.margin_top := 0;
178+ CefPdfPrintSettings.margin_type := PDF_PRINT_MARGIN_DEFAULT;
179+ CefPdfPrintSettings.page_height := 0;
180+ CefPdfPrintSettings.page_width := 0;
181+ CefPdfPrintSettings.selection_only := 0;
182+
183+ Chromium.Browser.Host.PrintToPdf(UTF8Decode(SaveDialog1.FileName), CefPdfPrintSettings, Nil);
184+ end;
185+end;
186+```
187+
188+The description of the `TCefPdfPrintSettings` you will find by the declaration of the `TCefPdfPrintSettings`
189+
190+## Known Problems
191+
192+- If your program doesn't start, maybe a Antivirus program prevent this.
193+- If your program start but your TChromium window keeps empty, maybe a firewall block it (see [Chromium Embedded Framework for Free Pascal (fpCEF3) BRUNCH 2556](http://forum.lazarus.freepascal.org/index.php/topic,31361.0.html)).
194+- If your file structure isn't correct (see Minimal example) and therefore some libraries wouldn't be found, you can get optical errors (see [Fpcef red borders in the browser](http://forum.lazarus.freepascal.org/index.php/topic,30763.0.html)).
195+
196+## Further Information
197+
198+- [Thread in the German Lazarusforum](http://www.lazarusforum.de/viewtopic.php?f=18&t=7676)
199+- [[Webbrowser]]
200+
201+---
202+*From the Free Pascal wiki, page "fpCEF3", as it stood on 2021-01-15 after a change by Michael.vanham, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
203+Category: Components
204+Category: LCL

Back to history