doc.go 10 KB

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