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