CEF4Delphi/source/uCEFPDFPrintOptions.pas

358 lines
13 KiB
ObjectPascal
Raw Normal View History

2017-01-27 16:37:51 +01:00
unit uCEFPDFPrintOptions;
{$IFDEF FPC}
{$MODE OBJFPC}{$H+}
{$ENDIF}
2017-02-05 20:56:46 +01:00
{$I cef.inc}
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
{$MINENUMSIZE 4}
2017-01-27 16:37:51 +01:00
interface
uses
2017-02-05 20:56:46 +01:00
{$IFDEF DELPHI16_UP}
2017-01-27 16:37:51 +01:00
System.Classes,
2017-02-05 20:56:46 +01:00
{$ELSE}
Classes,
{$ENDIF}
2017-01-27 16:37:51 +01:00
uCEFTypes;
type
2023-09-24 11:21:05 +02:00
/// <summary>
/// The TPDFPrintOptions properties are used to fill the TCefPdfPrintSettings record which is used in the TChromiumCore.PrintToPDF call.
/// </summary>
2022-12-16 11:29:15 +01:00
TPDFPrintOptions = class
2017-01-27 16:37:51 +01:00
protected
2024-02-01 15:13:22 +01:00
FLandscape : boolean;
FPrintBackground : boolean;
FScale : double;
FPaperWidth : double;
FPaperHeight : double;
FPreferCSSPageSize : boolean;
FMarginType : TCefPdfPrintMarginType;
FMarginTop : double;
FMarginRight : double;
FMarginBottom : double;
FMarginLeft : double;
FPageRanges : ustring;
FDisplayHeaderFooter : boolean;
FHeaderTemplate : ustring;
FFooterTemplate : ustring;
FGenerateTaggedPDF : boolean;
FGenerateDocumentOutline : boolean;
2022-12-16 11:29:15 +01:00
function GetScalePct: double;
function GetPaperWidthMM: double;
function GetPaperHeightMM: double;
function GetMarginTopMM: double;
function GetMarginRightMM: double;
function GetMarginBottomMM: double;
function GetMarginLeftMM: double;
procedure SetScalePct(const aValue: double);
procedure SetPaperWidthMM(const aValue: double);
procedure SetPaperHeightMM(const aValue: double);
procedure SetMarginTopMM(const aValue: double);
procedure SetMarginRightMM(const aValue: double);
procedure SetMarginBottomMM(const aValue: double);
procedure SetMarginLeftMM(const aValue: double);
function InchesToMM(const aInches: double): double;
function MMToInches(const aMM: double): double;
2017-01-27 16:37:51 +01:00
public
2023-09-24 11:21:05 +02:00
/// <summary>
/// Constructor of TPDFPrintOptions
/// </summary>
2017-01-27 16:37:51 +01:00
constructor Create; virtual;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Copy the fields of this class to the TCefPdfPrintSettings parameter.
/// </summary>
2022-12-16 11:29:15 +01:00
procedure CopyToSettings(var aSettings : TCefPdfPrintSettings);
2023-09-24 11:21:05 +02:00
/// <summary>
/// Set to true for landscape mode or false for portrait mode.
/// </summary>
2024-02-01 15:13:22 +01:00
property Landscape : boolean read FLandscape write FLandscape;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Set to true to print background graphics.
/// </summary>
2024-02-01 15:13:22 +01:00
property PrintBackground : boolean read FPrintBackground write FPrintBackground;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Set to true to prefer page size as defined by css. Defaults to false,
/// in which case the content will be scaled to fit the paper size.
/// </summary>
2024-02-01 15:13:22 +01:00
property PreferCSSPageSize : boolean read FPreferCSSPageSize write FPreferCSSPageSize;
2023-09-24 11:21:05 +02:00
/// <summary>
/// <para>Paper ranges to print, one based, e.g., '1-5, 8, 11-13'. Pages are printed
/// in the document order, not in the order specified, and no more than once.
/// Defaults to empty string, which implies the entire document is printed.</para>
/// <para>The page numbers are quietly capped to actual page count of the document,
/// and ranges beyond the end of the document are ignored. If this results in
/// no pages to print, an error is reported. It is an error to specify a range
/// with start greater than end.</para>
/// </summary>
2024-02-01 15:13:22 +01:00
property PageRanges : ustring read FPageRanges write FPageRanges;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Set to true to display the header and/or footer. Modify
/// HeaderTemplate and/or FooterTemplate to customize the display.
/// </summary>
2024-02-01 15:13:22 +01:00
property DisplayHeaderFooter : boolean read FDisplayHeaderFooter write FDisplayHeaderFooter;
2023-09-24 11:21:05 +02:00
/// <summary>
/// <para>HTML template for the print header. Only displayed if
/// DisplayHeaderFooter is true. Should be valid HTML markup with
/// the following classes used to inject printing values into them:</para>
/// <code>
/// - date: formatted print date
/// - title: document title
/// - url: document location
/// - pageNumber: current page number
/// - totalPages: total pages in the document
/// </code>
/// <para>For example, "<span class=title></span>" would generate a span containing
/// the title.</para>
/// </summary>
2024-02-01 15:13:22 +01:00
property HeaderTemplate : ustring read FHeaderTemplate write FHeaderTemplate;
2023-09-24 11:21:05 +02:00
/// <summary>
/// HTML template for the print footer. Only displayed if
/// DisplayHeaderFooter is true. Uses the same format as
/// HeaderTemplate.
/// </summary>
2024-02-01 15:13:22 +01:00
property FooterTemplate : ustring read FFooterTemplate write FFooterTemplate;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Set to true to generate tagged (accessible) PDF.
/// </summary>
2024-02-01 15:13:22 +01:00
property GenerateTaggedPDF : boolean read FGenerateTaggedPDF write FGenerateTaggedPDF;
/// <summary>
/// Set to true to generate a document outline.
/// </summary>
property GenerateDocumentOutline : boolean read FGenerateDocumentOutline write FGenerateDocumentOutline;
2023-09-24 11:21:05 +02:00
/// <summary>
/// The percentage to scale the PDF by before printing (e.g. .5 is 50%).
/// If this value is less than or equal to zero the default value of 1.0
/// will be used.
/// </summary>
2024-02-01 15:13:22 +01:00
property Scale : double read FScale write FScale;
2023-09-24 11:21:05 +02:00
/// <summary>
/// The percentage value to scale the PDF by before printing (e.g. 50 is 50%).
/// </summary>
2024-02-01 15:13:22 +01:00
property ScalePct : double read GetScalePct write SetScalePct;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Output paper width in inches. If either of these values is less than or
/// equal to zero then the default paper size (letter, 8.5 x 11 inches) will
/// be used.
/// </summary>
2024-02-01 15:13:22 +01:00
property PaperWidthInch : double read FPaperWidth write FPaperWidth;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Output paper height in inches. If either of these values is less than or
/// equal to zero then the default paper size (letter, 8.5 x 11 inches) will
/// be used.
/// </summary>
2024-02-01 15:13:22 +01:00
property PaperHeightInch : double read FPaperHeight write FPaperHeight;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Output paper width in mm.
/// </summary>
2024-02-01 15:13:22 +01:00
property PaperWidthMM : double read GetPaperWidthMM write SetPaperWidthMM;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Output paper height in mm.
/// </summary>
2024-02-01 15:13:22 +01:00
property PaperHeightMM : double read GetPaperHeightMM write SetPaperHeightMM;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Margin type.
/// </summary>
2024-02-01 15:13:22 +01:00
property MarginType : TCefPdfPrintMarginType read FMarginType write FMarginType;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Top margin in inches. Only used if MarginType is set to
/// PDF_PRINT_MARGIN_CUSTOM.
/// </summary>
2024-02-01 15:13:22 +01:00
property MarginTopInch : double read FMarginTop write FMarginTop;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Right margin in inches. Only used if MarginType is set to
/// PDF_PRINT_MARGIN_CUSTOM.
/// </summary>
2024-02-01 15:13:22 +01:00
property MarginRightInch : double read FMarginRight write FMarginRight;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Bottom margin in inches. Only used if MarginType is set to
/// PDF_PRINT_MARGIN_CUSTOM.
/// </summary>
2024-02-01 15:13:22 +01:00
property MarginBottomInch : double read FMarginBottom write FMarginBottom;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Left margin in inches. Only used if MarginType is set to
/// PDF_PRINT_MARGIN_CUSTOM.
/// </summary>
2024-02-01 15:13:22 +01:00
property MarginLeftInch : double read FMarginLeft write FMarginLeft;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Top margin in mm.
/// </summary>
2024-02-01 15:13:22 +01:00
property MarginTopMM : double read GetMarginTopMM write SetMarginTopMM;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Right margin in mm.
/// </summary>
2024-02-01 15:13:22 +01:00
property MarginRightMM : double read GetMarginRightMM write SetMarginRightMM;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Bottom margin in mm.
/// </summary>
2024-02-01 15:13:22 +01:00
property MarginBottomMM : double read GetMarginBottomMM write SetMarginBottomMM;
2023-09-24 11:21:05 +02:00
/// <summary>
/// Left margin in mm.
/// </summary>
2024-02-01 15:13:22 +01:00
property MarginLeftMM : double read GetMarginLeftMM write SetMarginLeftMM;
2017-01-27 16:37:51 +01:00
end;
implementation
2022-12-16 11:29:15 +01:00
uses
uCEFMiscFunctions;
const
MM_IN_ONE_INCH = 25.4;
2017-01-27 16:37:51 +01:00
constructor TPDFPrintOptions.Create;
begin
2024-02-01 15:13:22 +01:00
FLandscape := False;
FPrintBackground := False;
FScale := 0;
FPaperWidth := 0;
FPaperHeight := 0;
FPreferCSSPageSize := False;
FMarginType := PDF_PRINT_MARGIN_DEFAULT;
FMarginTop := 0;
FMarginRight := 0;
FMarginBottom := 0;
FMarginLeft := 0;
FPageRanges := '';
FDisplayHeaderFooter := False;
FHeaderTemplate := '';
FFooterTemplate := '';
FGenerateTaggedPDF := False;
FGenerateDocumentOutline := False;
2022-12-16 11:29:15 +01:00
end;
function TPDFPrintOptions.InchesToMM(const aInches: double): double;
begin
Result := aInches * MM_IN_ONE_INCH;
end;
function TPDFPrintOptions.MMToInches(const aMM: double): double;
begin
Result := aMM / MM_IN_ONE_INCH;
end;
function TPDFPrintOptions.GetScalePct: double;
begin
if (FScale <= 0) then
Result := 100
else
Result := FScale * 100;
end;
function TPDFPrintOptions.GetPaperWidthMM: double;
begin
Result := InchesToMM(FPaperWidth);
end;
function TPDFPrintOptions.GetPaperHeightMM: double;
begin
Result := InchesToMM(FPaperHeight);
end;
function TPDFPrintOptions.GetMarginTopMM: double;
begin
Result := InchesToMM(FMarginTop);
end;
function TPDFPrintOptions.GetMarginRightMM: double;
begin
Result := InchesToMM(FMarginRight);
end;
function TPDFPrintOptions.GetMarginBottomMM: double;
begin
Result := InchesToMM(FMarginBottom);
end;
function TPDFPrintOptions.GetMarginLeftMM: double;
begin
Result := InchesToMM(FMarginLeft);
end;
procedure TPDFPrintOptions.SetScalePct(const aValue: double);
begin
if (aValue <= 0) then
FScale := 0
else
FScale := aValue / 100;
end;
procedure TPDFPrintOptions.SetPaperWidthMM(const aValue: double);
begin
if (aValue <= 0) then
FPaperWidth := 0
else
FPaperWidth := MMToInches(aValue);
end;
procedure TPDFPrintOptions.SetPaperHeightMM(const aValue: double);
begin
if (aValue <= 0) then
FPaperHeight := 0
else
FPaperHeight := MMToInches(aValue);
end;
procedure TPDFPrintOptions.SetMarginTopMM(const aValue: double);
begin
if (aValue <= 0) then
FMarginTop := 0
else
FMarginTop := MMToInches(aValue);
end;
procedure TPDFPrintOptions.SetMarginRightMM(const aValue: double);
begin
if (aValue <= 0) then
FMarginRight := 0
else
FMarginRight := MMToInches(aValue);
end;
procedure TPDFPrintOptions.SetMarginBottomMM(const aValue: double);
begin
if (aValue <= 0) then
FMarginBottom := 0
else
FMarginBottom := MMToInches(aValue);
end;
procedure TPDFPrintOptions.SetMarginLeftMM(const aValue: double);
begin
if (aValue <= 0) then
FMarginLeft := 0
else
FMarginLeft := MMToInches(aValue);
end;
procedure TPDFPrintOptions.CopyToSettings(var aSettings : TCefPdfPrintSettings);
begin
2024-02-01 15:13:22 +01:00
aSettings.landscape := Ord(FLandscape);
aSettings.print_background := Ord(FPrintBackground);
aSettings.scale := FScale;
aSettings.paper_width := FPaperWidth;
aSettings.paper_height := FPaperHeight;
aSettings.prefer_css_page_size := Ord(FPreferCSSPageSize);
aSettings.margin_type := FMarginType;
aSettings.margin_top := FMarginTop;
aSettings.margin_right := FMarginRight;
aSettings.margin_bottom := FMarginBottom;
aSettings.margin_left := FMarginLeft;
aSettings.page_ranges := CefString(FPageRanges);
aSettings.display_header_footer := Ord(FDisplayHeaderFooter);
aSettings.header_template := CefString(FHeaderTemplate);
aSettings.footer_template := CefString(FFooterTemplate);
aSettings.generate_tagged_pdf := Ord(FGenerateTaggedPDF);
aSettings.generate_document_outline := Ord(FGenerateDocumentOutline);
2017-01-27 16:37:51 +01:00
end;
end.