Difference between revisions of "fcl-pdf"

From Lazarus wiki
Jump to navigationJump to search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Info ==
+
== About ==
fcl-pdf is [[PDF]] implementation.
+
The fcl-pdf package contains a [[PDF]] generating unit '''fppdf''' that does not depend on any external libraries.  
  
 
The PDF generator has the following features:
 
The PDF generator has the following features:
 +
 
* Support for basic shapes.
 
* Support for basic shapes.
 
* Support for basic line styles.
 
* Support for basic line styles.
Line 20: Line 21:
 
* Output validates by several PDF validators.
 
* Output validates by several PDF validators.
  
 
 
Contains units:  
 
Contains units:  
 +
 
* fppdf
 
* fppdf
 
* fpparsettf
 
* fpparsettf
 
* fpttf
 
* fpttf
 
* fpttfencodings
 
* fpttfencodings
 +
 +
== Examples ==
 +
=== Official test project ===
 +
Take a look at the "fcl-pdf/examples/testfppdf.lpr" project included with FPC's source code. It was purposely designed to help explain how to use the fcl-pdf package. Each page of that demo is defined in a separate method to help remove clutter, and explain the usage and functionality in smaller chucks of code.
 +
 +
=== Writing few lines, without embedded fonts ===
 +
Example by forum member '''Moritz''', fixed by member '''paweld'''. Code disables embedding of fonts into PDF.
 +
 +
<syntaxhighlight lang="pascal">
 +
procedure TMainForm.TestButtonClick(Sender: TObject);
 +
var
 +
  FontID, FontBoldID: Integer;
 +
  Document: TPDFDocument;
 +
  Section: TPDFSection;
 +
  Page: TPDFPage;
 +
begin
 +
  Document := TPDFDocument.Create(nil);
 +
  Document.FontDirectory := 'C:\Windows\Fonts';
 +
  Document.StartDocument;
 +
  FontID := Document.AddFont('arial.ttf', 'Arial');
 +
  FontBoldID := Document.AddFont('arialbd.ttf', 'Arial Bold');
 +
 +
  Document.Options := Document.Options + [poPageOriginAtTop, poNoEmbeddedFonts];
 +
  Section := Document.Sections.AddSection;
 +
 +
  Page := Document.Pages.AddPage;
 +
  Section.AddPage(Page);
 +
 +
  Page.SetFont(FontID, 11);
 +
  Page.WriteText(20, 20, 'This is normal text');
 +
 +
  Page.SetFont(FontBoldID, 11);
 +
  Page.WriteText(20, 30, 'This is bold text');
 +
 +
  Document.SaveToFile('output.pdf');
 +
end;
 +
</syntaxhighlight>
 +
 +
=== Enumerate available fonts ===
 +
Example by forum member '''paweld'''.
 +
<syntaxhighlight lang="pascal">
 +
uses
 +
  fppdf, fpttf;
 +
 +
procedure TForm1.FormCreate(Sender: TObject);
 +
var
 +
  g: TFPFontCacheList;
 +
  i: Integer;
 +
begin
 +
  g := TFPFontCacheList.Create;
 +
  g.SearchPath.Add('C:\Windows\Fonts');
 +
  g.BuildFontCache;
 +
  for i := 0 to g.Count - 1 do
 +
    Memo1.Lines.Add(Format('File name: %s > Font name: %s > Family: %s',
 +
      [g.Items[i].FileName, g.Items[i].HumanFriendlyName, g.Items[i].FamilyName]));
 +
  g.Free;
 +
end;
 +
</syntaxhighlight>
 +
 +
=== Render picture to PDF ===
 +
Example by forum member '''CynicRus''' changed by '''AlexTP'''.
 +
Note: on Ubuntu 20.04, default PDF viewer cannot show the picture correctly, it crops some top part of the picture.
 +
<syntaxhighlight lang="pascal">
 +
uses
 +
  fppdf;
 +
 +
procedure WritePictureToPDF(const APictureFilename, APdfFilename: string);
 +
var
 +
  PDF: TPDFDocument;
 +
  Page: TPDFPage;
 +
  Section: TPDFSection;
 +
  Paper: TPDFPaper;
 +
  Index: Integer;
 +
  W, H: Integer;
 +
begin
 +
  PDF := TPDFDocument.Create(nil);
 +
  try
 +
    PDF.StartDocument;
 +
    Section := PDF.Sections.AddSection;
 +
 +
    Page := PDF.Pages.AddPage;
 +
    Page.Orientation := ppoLandscape;
 +
    Page.UnitOfMeasure := uomPixels;
 +
 +
    Section.AddPage(Page);
 +
 +
    Index := PDF.Images.AddFromFile(APictureFilename);
 +
    W := PDF.Images[Index].Width;
 +
    H := PDF.Images[Index].Height;
 +
    paper.W := W;
 +
    paper.H := H;
 +
    Page.Paper := Paper;
 +
    Page.PaperType := ptCustom;
 +
    Page.DrawImageRawSize(0, 0, W, H, Index);
 +
 +
    PDF.SaveToFile(APdfFilename);
 +
  finally
 +
    PDF.Free;
 +
  end;
 +
end;
 +
</syntaxhighlight>
  
 
== See also ==
 
== See also ==

Latest revision as of 13:16, 16 August 2023

About

The fcl-pdf package contains a PDF generating unit fppdf that does not depend on any external libraries.

The PDF generator has the following features:

  • Support for basic shapes.
  • Support for basic line styles.
  • Dictionary support.
  • Multi-page PDF.
  • Image support.
  • TTF Font support.
  • Font embedding.
  • Unicode font support.
  • Stream Compression.
  • Image embedding.
  • Several paper types.
  • Portrait/Landscape.
  • Support for multiple units.
  • Rotation matrix system.
  • PDF creator information.
  • Output validates by several PDF validators.

Contains units:

  • fppdf
  • fpparsettf
  • fpttf
  • fpttfencodings

Examples

Official test project

Take a look at the "fcl-pdf/examples/testfppdf.lpr" project included with FPC's source code. It was purposely designed to help explain how to use the fcl-pdf package. Each page of that demo is defined in a separate method to help remove clutter, and explain the usage and functionality in smaller chucks of code.

Writing few lines, without embedded fonts

Example by forum member Moritz, fixed by member paweld. Code disables embedding of fonts into PDF.

procedure TMainForm.TestButtonClick(Sender: TObject);
var
  FontID, FontBoldID: Integer;
  Document: TPDFDocument;
  Section: TPDFSection;
  Page: TPDFPage;
begin
  Document := TPDFDocument.Create(nil);
  Document.FontDirectory := 'C:\Windows\Fonts';
  Document.StartDocument;
  FontID := Document.AddFont('arial.ttf', 'Arial');
  FontBoldID := Document.AddFont('arialbd.ttf', 'Arial Bold');
 
  Document.Options := Document.Options + [poPageOriginAtTop, poNoEmbeddedFonts];
  Section := Document.Sections.AddSection;
 
  Page := Document.Pages.AddPage;
  Section.AddPage(Page);
 
  Page.SetFont(FontID, 11);
  Page.WriteText(20, 20, 'This is normal text');
 
  Page.SetFont(FontBoldID, 11);
  Page.WriteText(20, 30, 'This is bold text');
 
  Document.SaveToFile('output.pdf');
end;

Enumerate available fonts

Example by forum member paweld.

uses
  fppdf, fpttf;
 
procedure TForm1.FormCreate(Sender: TObject);
var
  g: TFPFontCacheList;
  i: Integer;
begin
  g := TFPFontCacheList.Create;
  g.SearchPath.Add('C:\Windows\Fonts');
  g.BuildFontCache;
  for i := 0 to g.Count - 1 do
    Memo1.Lines.Add(Format('File name: %s > Font name: %s > Family: %s', 
      [g.Items[i].FileName, g.Items[i].HumanFriendlyName, g.Items[i].FamilyName]));
  g.Free;
end;

Render picture to PDF

Example by forum member CynicRus changed by AlexTP. Note: on Ubuntu 20.04, default PDF viewer cannot show the picture correctly, it crops some top part of the picture.

uses
  fppdf;

procedure WritePictureToPDF(const APictureFilename, APdfFilename: string);
var
  PDF: TPDFDocument;
  Page: TPDFPage;
  Section: TPDFSection;
  Paper: TPDFPaper;
  Index: Integer;
  W, H: Integer;
begin
  PDF := TPDFDocument.Create(nil);
  try
    PDF.StartDocument;
    Section := PDF.Sections.AddSection;

    Page := PDF.Pages.AddPage;
    Page.Orientation := ppoLandscape;
    Page.UnitOfMeasure := uomPixels;

    Section.AddPage(Page);

    Index := PDF.Images.AddFromFile(APictureFilename);
    W := PDF.Images[Index].Width;
    H := PDF.Images[Index].Height;
    paper.W := W;
    paper.H := H;
    Page.Paper := Paper;
    Page.PaperType := ptCustom;
    Page.DrawImageRawSize(0, 0, W, H, Index);

    PDF.SaveToFile(APdfFilename);
  finally
    PDF.Free;
  end;
end;

See also