codeberg.org/go-pdf/fpdf@v0.11.1/doc.go (about) 1 // Copyright ©2023 The go-pdf Authors. All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 /* 6 Package fpdf implements a PDF document generator with high level 7 support for text, drawing and images. 8 9 # Features 10 11 - UTF-8 support 12 13 - Choice of measurement unit, page format and margins 14 15 - Page header and footer management 16 17 - Automatic page breaks, line breaks, and text justification 18 19 - Inclusion of JPEG, PNG, GIF, TIFF and basic path-only SVG images 20 21 - Colors, gradients and alpha channel transparency 22 23 - Outline bookmarks 24 25 - Internal and external links 26 27 - TrueType, Type1 and encoding support 28 29 - Page compression 30 31 - Lines, Bézier curves, arcs, and ellipses 32 33 - Rotation, scaling, skewing, translation, and mirroring 34 35 - Clipping 36 37 - Document protection 38 39 - Layers 40 41 - Templates 42 43 - Barcodes 44 45 - Charting facility 46 47 - Import PDFs as templates 48 49 go-pdf/fpdf has no dependencies other than the Go standard library. All tests 50 pass on Linux, Mac and Windows platforms. 51 52 go-pdf/fpdf supports UTF-8 TrueType fonts and “right-to-left” languages. Note 53 that Chinese, Japanese, and Korean characters may not be included in 54 many general purpose fonts. For these languages, a specialized font (for 55 example, NotoSansSC for simplified Chinese) can be used. 56 57 Also, support is provided to automatically translate UTF-8 runes to code 58 page encodings for languages that have fewer than 256 glyphs. 59 60 # Installation 61 62 To install the package on your system, run 63 64 go get codeberg.org/go-pdf/fpdf 65 66 Later, to receive updates, run 67 68 go get -u -v codeberg.org/go-pdf/fpdf/... 69 70 # Quick Start 71 72 The following Go code generates a simple PDF file. 73 74 pdf := fpdf.New("P", "mm", "A4", "") 75 pdf.AddPage() 76 pdf.SetFont("Arial", "B", 16) 77 pdf.Cell(40, 10, "Hello, world") 78 err := pdf.OutputFileAndClose("hello.pdf") 79 80 See the functions in the fpdf_test.go file (shown as examples in this 81 documentation) for more advanced PDF examples. 82 83 # Errors 84 85 If an error occurs in an Fpdf method, an internal error field is set. 86 After this occurs, Fpdf method calls typically return without performing 87 any operations and the error state is retained. This error management 88 scheme facilitates PDF generation since individual method calls do not 89 need to be examined for failure; it is generally sufficient to wait 90 until after Output() is called. For the same reason, if an error occurs 91 in the calling application during PDF generation, it may be desirable 92 for the application to transfer the error to the Fpdf instance by 93 calling the SetError() method or the SetErrorf() method. At any time 94 during the life cycle of the Fpdf instance, the error state can be 95 determined with a call to Ok() or Err(). The error itself can be 96 retrieved with a call to Error(). 97 98 # Conversion Notes 99 100 This package is a relatively straightforward translation from the 101 original FPDF library written in PHP (despite the caveat in the 102 introduction to Effective Go). The API names have been retained even 103 though the Go idiom would suggest otherwise (for example, pdf.GetX() is 104 used rather than simply pdf.X()). The similarity of the two libraries 105 makes the original FPDF website a good source of information. It 106 includes a forum and FAQ. 107 108 However, some internal changes have been made. Page content is built up 109 using buffers (of type bytes.Buffer) rather than repeated string 110 concatenation. Errors are handled as explained above rather than 111 panicking. Output is generated through an interface of type io.Writer or 112 io.WriteCloser. A number of the original PHP methods behave differently 113 based on the type of the arguments that are passed to them; in these 114 cases additional methods have been exported to provide similar 115 functionality. Font definition files are produced in JSON rather than 116 PHP. 117 118 # Example PDFs 119 120 A side effect of running go test ./... is the production of a number of 121 example PDFs. These can be found in the go-pdf/fpdf/pdf directory after the 122 tests complete. 123 124 Please note that these examples run in the context of a test. In order 125 run an example as a standalone application, you’ll need to examine 126 fpdf_test.go for some helper routines, for example exampleFilename() and 127 summary(). 128 129 Example PDFs can be compared with reference copies in order to verify 130 that they have been generated as expected. This comparison will be 131 performed if a PDF with the same name as the example PDF is placed in 132 the go-pdf/fpdf/pdf/reference directory and if the third argument to 133 ComparePDFFiles() in internal/example/example.go is true. (By default it 134 is false.) The routine that summarizes an example will look for this 135 file and, if found, will call ComparePDFFiles() to check the example PDF 136 for equality with its reference PDF. If differences exist between the 137 two files they will be printed to standard output and the test will 138 fail. If the reference file is missing, the comparison is considered to 139 succeed. In order to successfully compare two PDFs, the placement of 140 internal resources must be consistent and the internal creation 141 timestamps must be the same. To do this, the methods SetCatalogSort() 142 and SetCreationDate() need to be called for both files. This is done 143 automatically for all examples. 144 145 # Nonstandard Fonts 146 147 Nothing special is required to use the standard PDF fonts (courier, 148 helvetica, times, zapfdingbats) in your documents other than calling 149 SetFont(). 150 151 You should use AddUTF8Font() or AddUTF8FontFromBytes() to add a TrueType 152 UTF-8 encoded font. Use RTL() and LTR() methods switch between 153 “right-to-left” and “left-to-right” mode. 154 155 In order to use a different non-UTF-8 TrueType or Type1 font, you will 156 need to generate a font definition file and, if the font will be 157 embedded into PDFs, a compressed version of the font file. This is done 158 by calling the MakeFont function or using the included makefont command 159 line utility. To create the utility, cd into the makefont subdirectory 160 and run “go build”. This will produce a standalone executable named 161 makefont. Select the appropriate encoding file from the font 162 subdirectory and run the command as in the following example. 163 164 ./makefont --embed --enc=../font/cp1252.map --dst=../font ../font/calligra.ttf 165 166 In your PDF generation code, call AddFont() to load the font and, as 167 with the standard fonts, SetFont() to begin using it. Most examples, 168 including the package example, demonstrate this method. Good sources of 169 free, open-source fonts include Google Fonts and DejaVu Fonts. 170 171 # Related Packages 172 173 The draw2d package is a two dimensional vector graphics library that can 174 generate output in different forms. It uses gofpdf for its document 175 production mode. 176 177 # Contributing Changes 178 179 gofpdf is a global community effort and you are invited to make it even 180 better. If you have implemented a new feature or corrected a problem, 181 please consider contributing your change to the project. A contribution 182 that does not directly pertain to the core functionality of gofpdf 183 should be placed in its own directory directly beneath the contrib 184 directory. 185 186 Here are guidelines for making submissions. Your change should 187 188 - be compatible with the MIT License 189 190 - be properly documented 191 192 - be formatted with go fmt 193 194 - include an example in fpdf_test.go if appropriate 195 196 - conform to the standards of golint and go vet, that is, golint . and 197 go vet . should not generate any warnings 198 199 - not diminish test coverage 200 201 Pull requests are the preferred means of accepting your changes. 202 203 # License 204 205 gofpdf is released under the MIT License. It is copyrighted by Kurt Jung 206 and the contributors acknowledged below. 207 208 # Acknowledgments 209 210 This package’s code and documentation are closely derived from the FPDF 211 library created by Olivier Plathey, and a number of font and image 212 resources are copied directly from it. Bruno Michel has provided 213 valuable assistance with the code. Drawing support is adapted from the 214 FPDF geometric figures script by David Hernández Sanz. Transparency 215 support is adapted from the FPDF transparency script by Martin Hall-May. 216 Support for gradients and clipping is adapted from FPDF scripts by 217 Andreas Würmser. Support for outline bookmarks is adapted from Olivier 218 Plathey by Manuel Cornes. Layer support is adapted from Olivier Plathey. 219 Support for transformations is adapted from the FPDF transformation 220 script by Moritz Wagner and Andreas Würmser. PDF protection is adapted 221 from the work of Klemen Vodopivec for the FPDF product. Lawrence 222 Kesteloot provided code to allow an image’s extent to be determined 223 prior to placement. Support for vertical alignment within a cell was 224 provided by Stefan Schroeder. Ivan Daniluk generalized the font and 225 image loading code to use the Reader interface while maintaining 226 backward compatibility. Anthony Starks provided code for the Polygon 227 function. Robert Lillack provided the Beziergon function and corrected 228 some naming issues with the internal curve function. Claudio Felber 229 provided implementations for dashed line drawing and generalized font 230 loading. Stani Michiels provided support for multi-segment path drawing 231 with smooth line joins, line join styles, enhanced fill modes, and has 232 helped greatly with package presentation and tests. Templating is 233 adapted by Marcus Downing from the FPDF_Tpl library created by Jan 234 Slabon and Setasign. Jelmer Snoeck contributed packages that generate a 235 variety of barcodes and help with registering images on the web. Jelmer 236 Snoek and Guillermo Pascual augmented the basic HTML functionality with 237 aligned text. Kent Quirk implemented backwards-compatible support for 238 reading DPI from images that support it, and for setting DPI manually 239 and then having it properly taken into account when calculating image 240 size. Paulo Coutinho provided support for static embedded fonts. Dan 241 Meyers added support for embedded JavaScript. David Fish added a generic 242 alias-replacement function to enable, among other things, table of 243 contents functionality. Andy Bakun identified and corrected a problem in 244 which the internal catalogs were not sorted stably. Paul Montag added 245 encoding and decoding functionality for templates, including images that 246 are embedded in templates; this allows templates to be stored 247 independently of gofpdf. Paul also added support for page boxes used in 248 printing PDF documents. Wojciech Matusiak added supported for word 249 spacing. Artem Korotkiy added support of UTF-8 fonts. Dave Barnes added 250 support for imported objects and templates. Brigham Thompson added 251 support for rounded rectangles. Joe Westcott added underline 252 functionality and optimized image storage. Benoit KUGLER contributed 253 support for rectangles with corners of unequal radius, modification 254 times, and for file attachments and annotations. 255 256 # Roadmap 257 258 - Remove all legacy code page font support; use UTF-8 exclusively 259 260 - Improve test coverage as reported by the coverage tool. 261 */ 262 package fpdf // import "codeberg.org/go-pdf/fpdf"