doc.go 11 KB

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