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