fcl-pdf: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: fcl-pdf
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## About | |
| 2 | + | ||
| 3 | + | The fcl-pdf package contains a [[PDF]] generating unit **fppdf** that does not depend on any external libraries. | |
| 4 | + | ||
| 5 | + | The PDF generator has the following features: | |
| 6 | + | ||
| 7 | + | - Support for basic shapes. | |
| 8 | + | - Support for basic line styles. | |
| 9 | + | - Dictionary support. | |
| 10 | + | - Multi-page PDF. | |
| 11 | + | - Image support. | |
| 12 | + | - TTF Font support. | |
| 13 | + | - Font embedding. | |
| 14 | + | - Unicode font support. | |
| 15 | + | - Stream Compression. | |
| 16 | + | - Image embedding. | |
| 17 | + | - Several paper types. | |
| 18 | + | - Portrait/Landscape. | |
| 19 | + | - Support for multiple units. | |
| 20 | + | - Rotation matrix system. | |
| 21 | + | - PDF creator information. | |
| 22 | + | - Output validates by several PDF validators. | |
| 23 | + | ||
| 24 | + | Contains units: | |
| 25 | + | ||
| 26 | + | - fppdf | |
| 27 | + | - fpparsettf | |
| 28 | + | - fpttf | |
| 29 | + | - fpttfencodings | |
| 30 | + | ||
| 31 | + | ## Examples | |
| 32 | + | ||
| 33 | + | ### Official test project | |
| 34 | + | ||
| 35 | + | 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. | |
| 36 | + | ||
| 37 | + | ### Writing few lines, without embedded fonts | |
| 38 | + | ||
| 39 | + | Example by forum member **Moritz**, fixed by member **paweld**. Code disables embedding of fonts into PDF. | |
| 40 | + | ||
| 41 | + | ``` pascal | |
| 42 | + | procedure TMainForm.TestButtonClick(Sender: TObject); | |
| 43 | + | var | |
| 44 | + | FontID, FontBoldID: Integer; | |
| 45 | + | Document: TPDFDocument; | |
| 46 | + | Section: TPDFSection; | |
| 47 | + | Page: TPDFPage; | |
| 48 | + | begin | |
| 49 | + | Document := TPDFDocument.Create(nil); | |
| 50 | + | Document.FontDirectory := 'C:\Windows\Fonts'; | |
| 51 | + | Document.StartDocument; | |
| 52 | + | FontID := Document.AddFont('arial.ttf', 'Arial'); | |
| 53 | + | FontBoldID := Document.AddFont('arialbd.ttf', 'Arial Bold'); | |
| 54 | + | ||
| 55 | + | Document.Options := Document.Options + [poPageOriginAtTop, poNoEmbeddedFonts]; | |
| 56 | + | Section := Document.Sections.AddSection; | |
| 57 | + | ||
| 58 | + | Page := Document.Pages.AddPage; | |
| 59 | + | Section.AddPage(Page); | |
| 60 | + | ||
| 61 | + | Page.SetFont(FontID, 11); | |
| 62 | + | Page.WriteText(20, 20, 'This is normal text'); | |
| 63 | + | ||
| 64 | + | Page.SetFont(FontBoldID, 11); | |
| 65 | + | Page.WriteText(20, 30, 'This is bold text'); | |
| 66 | + | ||
| 67 | + | Document.SaveToFile('output.pdf'); | |
| 68 | + | end; | |
| 69 | + | ``` | |
| 70 | + | ||
| 71 | + | ### Enumerate available fonts | |
| 72 | + | ||
| 73 | + | Example by forum member **paweld**. | |
| 74 | + | ||
| 75 | + | ``` pascal | |
| 76 | + | uses | |
| 77 | + | fppdf, fpttf; | |
| 78 | + | ||
| 79 | + | procedure TForm1.FormCreate(Sender: TObject); | |
| 80 | + | var | |
| 81 | + | g: TFPFontCacheList; | |
| 82 | + | i: Integer; | |
| 83 | + | begin | |
| 84 | + | g := TFPFontCacheList.Create; | |
| 85 | + | g.SearchPath.Add('C:\Windows\Fonts'); | |
| 86 | + | g.BuildFontCache; | |
| 87 | + | for i := 0 to g.Count - 1 do | |
| 88 | + | Memo1.Lines.Add(Format('File name: %s > Font name: %s > Family: %s', | |
| 89 | + | [g.Items[i].FileName, g.Items[i].HumanFriendlyName, g.Items[i].FamilyName])); | |
| 90 | + | g.Free; | |
| 91 | + | end; | |
| 92 | + | ``` | |
| 93 | + | ||
| 94 | + | ### Render picture to PDF | |
| 95 | + | ||
| 96 | + | 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. | |
| 97 | + | ||
| 98 | + | ``` pascal | |
| 99 | + | uses | |
| 100 | + | fppdf; | |
| 101 | + | ||
| 102 | + | procedure WritePictureToPDF(const APictureFilename, APdfFilename: string); | |
| 103 | + | var | |
| 104 | + | PDF: TPDFDocument; | |
| 105 | + | Page: TPDFPage; | |
| 106 | + | Section: TPDFSection; | |
| 107 | + | Paper: TPDFPaper; | |
| 108 | + | Index: Integer; | |
| 109 | + | W, H: Integer; | |
| 110 | + | begin | |
| 111 | + | PDF := TPDFDocument.Create(nil); | |
| 112 | + | try | |
| 113 | + | PDF.StartDocument; | |
| 114 | + | Section := PDF.Sections.AddSection; | |
| 115 | + | ||
| 116 | + | Page := PDF.Pages.AddPage; | |
| 117 | + | Page.Orientation := ppoLandscape; | |
| 118 | + | Page.UnitOfMeasure := uomPixels; | |
| 119 | + | ||
| 120 | + | Section.AddPage(Page); | |
| 121 | + | ||
| 122 | + | Index := PDF.Images.AddFromFile(APictureFilename); | |
| 123 | + | W := PDF.Images[Index].Width; | |
| 124 | + | H := PDF.Images[Index].Height; | |
| 125 | + | paper.W := W; | |
| 126 | + | paper.H := H; | |
| 127 | + | Page.Paper := Paper; | |
| 128 | + | Page.PaperType := ptCustom; | |
| 129 | + | Page.DrawImageRawSize(0, 0, W, H, Index); | |
| 130 | + | ||
| 131 | + | PDF.SaveToFile(APdfFilename); | |
| 132 | + | finally | |
| 133 | + | PDF.Free; | |
| 134 | + | end; | |
| 135 | + | end; | |
| 136 | + | ``` | |
| 137 | + | ||
| 138 | + | ## See also | |
| 139 | + | ||
| 140 | + | - [[Package List]] | |
| 141 | + | ||
| 142 | + | --- | |
| 143 | + | *From the Free Pascal wiki, page "fcl-pdf", as it stood on 2023-08-16 after a change by Alextpp, 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).* | |
| 144 | + | Category: PDF | |
| 145 | + | Category: fcl-pdf |
