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