Skip to content

Commit

Permalink
- Updated to CEF 89.0.15
Browse files Browse the repository at this point in the history
- Improved error reporting
- Initial support for "print" option (actual printing, experimental, stills shows interactive dialog)
  • Loading branch information
Eric Grange committed Mar 26, 2021
1 parent 680ab39 commit 40fcf9f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 23 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ __history
*.tds
.backup
__recovery
/Chromium88.2
/Chromium88
/Chromium*
/Releases
13 changes: 8 additions & 5 deletions cefHtmlSnapshot.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ uses
{$ELSE}
SysUtils,
{$ENDIF }
uCEFApplication, uCEFApplicationCore,
uCEFApplication, uCEFApplicationCore, TypInfo, uCEFTypes,
{$ifdef USE_BUNDLE} UChromiumBundle,{$endif}
uEncapsulatedBrowser in 'uEncapsulatedBrowser.pas',
uCEFBrowserThread in 'uCEFBrowserThread.pas',
Expand Down Expand Up @@ -139,12 +139,15 @@ begin
try
{$ifdef USE_BUNDLE}
vChromiumBundleQuickCheck := True;
CreateGlobalCEFApp(parameters, ChromiumUnBundledPath);
if CreateGlobalCEFApp(parameters, ChromiumUnBundledPath) then begin
{$else}
CreateGlobalCEFApp(parameters);
if CreateGlobalCEFApp(parameters) then begin
{$endif}
if WaitForMainAppEvent then
WriteResult;
if WaitForMainAppEvent then
WriteResult;
end else begin
Writeln(GlobalCEFApp.LastErrorMessage);
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
Expand Down
2 changes: 1 addition & 1 deletion cefHtmlSnapshot.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<DCC_Optimize>true</DCC_Optimize>
<DCC_GenerateStackFrames>false</DCC_GenerateStackFrames>
<DCC_MapFile>3</DCC_MapFile>
<Debugger_RunParams>https://www.delphitools.info/ --javascript Scripts\cookie-law-buster.js snapshot.jpg</Debugger_RunParams>
<Debugger_RunParams>https://www.google.com test\test1.pdf</Debugger_RunParams>
<DCC_DebugInfoInTds>true</DCC_DebugInfoInTds>
<Icon_MainIcon>cefHtmlSnapshot_Icon.ico</Icon_MainIcon>
<VerInfo_IncludeVerInfo>true</VerInfo_IncludeVerInfo>
Expand Down
Binary file modified cefHtmlSnapshot.res
Binary file not shown.
6 changes: 4 additions & 2 deletions uCEFBrowserThread.pas
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ function TCEFBrowserThread.TakeSnapshot : boolean;
try
FBrowserInfoCS.Acquire;

if FParameters.OutputFormat = sofPDF then begin
if FParameters.OutputFormat in [ sofPDF, sofPrinter ] then begin
FBrowser.PDFPrintOptions.page_width := FParameters.PDFOptions.page_width;
FBrowser.PDFPrintOptions.page_height := FParameters.PDFOptions.page_height;
FBrowser.PDFPrintOptions.margin_type := FParameters.PDFOptions.margin_type;
Expand All @@ -817,7 +817,9 @@ function TCEFBrowserThread.TakeSnapshot : boolean;
FBrowser.PDFPrintOptions.landscape := FParameters.PDFOptions.landscape <> 0;
FBrowser.PDFPrintOptions.header_footer_enabled := (FParameters.PDFTitle <> '') or (FParameters.PDFURL <> '');
FBrowser.PDFPrintOptions.backgrounds_enabled := FParameters.PDFOptions.backgrounds_enabled <> 0;
FBrowser.PrintToPDF(FParameters.OutputFilePath, FParameters.PDFTitle, FParameters.PDFURL);
if FParameters.Print then
FBrowser.Print
else FBrowser.PrintToPDF(FParameters.OutputFilePath, FParameters.PDFTitle, FParameters.PDFURL);
end else begin
if assigned(FPanel.Buffer) and not(FPanel.Buffer.Empty) then
begin
Expand Down
28 changes: 18 additions & 10 deletions uCEFSnapshotParameters.pas
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ interface
uCEFTypes, uCEFMiscFunctions;

const
cChromiumSubFolder = 'Chromium88.2';
cChromiumSubFolder = 'Chromium89.15';
cDLLSubfolder = 'Libraries';

type
TSnapshotOutputFormat = ( sofUnknown, sofBMP, sofJPG, sofPNG, sofPDF );
TSnapshotOutputFormat = ( sofUnknown, sofBMP, sofJPG, sofPNG, sofPDF, sofPrinter );

TSnapshotParameters = record
ErrorText : String; // if not empty, parsing ended up with errors
Expand All @@ -30,6 +30,7 @@ TSnapshotParameters = record
Cookies : array of String;
IgnoreCertificateErrors : Boolean;
NoSandbox : Boolean;
Print : Boolean;

procedure SaveBitmap(bmp : TBitmap);
function URLSchemeDomain : String;
Expand All @@ -48,14 +49,15 @@ implementation
uses LibTurboJPEG, Vcl.Imaging.pngimage, System.StrUtils;

const
cHelp = 'cefHtmlSnaphot utility v0.3.88 - Html to image or pdf coversion using Chromium Embedded Framework'#10
+ 'Using CEF 88.0.4324.150, CEF4Delphi, TurboJPEG see https://github.com/EricGrange/cefHtmlSnapshot'#10#10
cHelp = 'cefHtmlSnaphot utility v0.4.89 - Html to image or pdf coversion using Chromium Embedded Framework'#10
+ 'Using CEF 89.0.15, CEF4Delphi, TurboJPEG see https://github.com/EricGrange/cefHtmlSnapshot'#10#10
+ 'cefHtmlSnapshot.exe url_or_file [-arg1 value1] [-arg2 value2] ... output_file'#10
+ #10
+ ' -?, -h, --help This inline documentation'#10
+ ' url_or_file URL of the website or file to be snapshotted (required)'#10
+ ' If a .url file is specified, the URL will be read from it'#10
+ ' output_file Output file pathname, extension determines format (default snapshot.bmp)'#10
+ ' If the printing mode is enabled, this is the name of the printer.'#10
+ #10
+ ' -w, --width Width of the snapshot, between 1 and 2048 (default 1024)'#10
+ ' -h, --height Height of the snapshot, between 1 and 2048 (default 768)'#10
Expand Down Expand Up @@ -85,6 +87,8 @@ implementation
+ ' margin-bottom bottom margin in points (default 20)'#10
+ ' landscape portait (default, 0) or landscape (1)'#10
+ ' backgrounds enable backgrounds (1) or not (default, 0)'#10
+ #10
+ ' --print If this option 1 then output_file is the name of a printer (by default 0)'#10
;

// ParseCommandLineParameters
Expand Down Expand Up @@ -177,11 +181,7 @@ function ParseCommandLineParameters : TSnapshotParameters;
else if ext = '.png' then
Result.OutputFormat := sofPNG
else if ext = '.pdf' then
Result.OutputFormat := sofPDF
else begin
Result.ErrorText := 'Unsupported output file format "' + Result.OutputFilePath + '"';
Exit;
end;
Result.OutputFormat := sofPDF;

// parse arguments in between

Expand Down Expand Up @@ -256,6 +256,12 @@ function ParseCommandLineParameters : TSnapshotParameters;
// property scale_factor : integer read Fscale_factor write Fscale_factor default 0;
// property header_footer_enabled : boolean read Fheader_footer_enabled write Fheader_footer_enabled default False;
// property selection_only : boolean read Fselection_only write Fselection_only default False;
end else if lastP = '-print' then begin
if p = '1' then begin
Result.Print := True;
Result.OutputFormat := sofPrinter;
end else if p <> '0' then
Result.ErrorText := 'Unsupported option "' + p + '" for print';
end else begin
Result.ErrorText := 'Unsupported parameter "' + p + '"';
end;
Expand All @@ -266,7 +272,9 @@ function ParseCommandLineParameters : TSnapshotParameters;

if lastP <> '' then begin
Result.ErrorText := 'Argument missing for parameter "' + lastP + '"';
end else if Result.URL = '' then begin
end else if Result.OutputFormat = sofUnknown then begin
Result.ErrorText := 'Unsupported output file format "' + Result.OutputFilePath + '"';
end else if (Result.URL = '') and not Result.Print then begin
Result.ErrorText := 'Missing URL parameter, it is required';
end;
end;
Expand Down
7 changes: 4 additions & 3 deletions uEncapsulatedBrowser.pas
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ TEncapsulatedBrowser = class
property ErrorText : ustring read FErrorText;
end;

procedure CreateGlobalCEFApp(const parameters : TSnapshotParameters; const chromiumPath : String = '');
function CreateGlobalCEFApp(const parameters : TSnapshotParameters; const chromiumPath : String = '') : Boolean;
function WaitForMainAppEvent : boolean;
procedure WriteResult;

Expand Down Expand Up @@ -111,7 +111,7 @@ procedure WriteResult;
WriteLn('Snapshot saved successfully as ' + EncapsulatedBrowser.Parameters.OutputFilePath);
end;

procedure CreateGlobalCEFApp(const parameters : TSnapshotParameters; const chromiumPath : String = '');
function CreateGlobalCEFApp(const parameters : TSnapshotParameters; const chromiumPath : String = '') : Boolean;
begin
GlobalCEFApp := TCefApplication.Create;
GlobalCEFApp.WindowlessRenderingEnabled := True;
Expand All @@ -135,6 +135,7 @@ procedure CreateGlobalCEFApp(const parameters : TSnapshotParameters; const chrom
GlobalCEFApp.EnableUsermediaScreenCapturing := False;
GlobalCEFApp.EnablePrintPreview := False;
GlobalCEFApp.DisableJavascriptAccessClipboard := True;
GlobalCEFApp.DisableJavascriptDomPaste := True;
GlobalCEFApp.DisableSpellChecking := True;
GlobalCEFApp.MuteAudio := True;
GlobalCEFApp.AllowFileAccessFromFiles := True;
Expand All @@ -148,7 +149,7 @@ procedure CreateGlobalCEFApp(const parameters : TSnapshotParameters; const chrom
GlobalCEFApp.PersistSessionCookies := False;
GlobalCEFApp.PersistUserPreferences := False;

GlobalCEFApp.StartMainProcess;
Result := GlobalCEFApp.StartMainProcess;
end;

constructor TEncapsulatedBrowser.Create(const aParameters : TSnapshotParameters);
Expand Down

0 comments on commit 40fcf9f

Please sign in to comment.