forked from jung-kurt/gofpdf
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kurt Jung
committed
Aug 2, 2013
0 parents
commit caed6a3
Showing
59 changed files
with
9,462 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
syntax: glob | ||
makefont/makefont | ||
pdf/*.pdf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* | ||
* Copyright (c) 2013 Kurt Jung (Gmail: kurt.w.jung) | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
package gofpdf | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
const FPDF_VERSION = "1.7" | ||
|
||
type sizeType struct { | ||
wd, ht float64 | ||
} | ||
|
||
type imageInfoType struct { | ||
data []byte | ||
smask []byte | ||
i int | ||
n int | ||
w float64 | ||
h float64 | ||
cs string | ||
pal []byte | ||
bpc int | ||
f string | ||
dp string | ||
trns []int | ||
} | ||
|
||
type fontFileType struct { | ||
length1, length2 int64 | ||
n int | ||
} | ||
|
||
type linkType struct { | ||
x, y, wd, ht float64 | ||
link int // Auto-generated link ID or... | ||
linkStr string // ...application-provided link string | ||
} | ||
|
||
type intLinkType struct { | ||
page int | ||
y float64 | ||
} | ||
|
||
type Fpdf struct { | ||
page int // current page number | ||
n int // current object number | ||
offsets []int // array of object offsets | ||
buffer fmtBuffer // buffer holding in-memory PDF | ||
pages []*bytes.Buffer // slice[page] of page content; 1-based | ||
state int // current document state | ||
compress bool // compression flag | ||
k float64 // scale factor (number of points in user unit) | ||
defOrientation string // default orientation | ||
curOrientation string // current orientation | ||
stdpageSizes map[string]sizeType // standard page sizes | ||
defPageSize sizeType // default page size | ||
curPageSize sizeType // current page size | ||
pageSizes map[int]sizeType // used for pages with non default sizes or orientations | ||
wPt, hPt float64 // dimensions of current page in points | ||
w, h float64 // dimensions of current page in user unit | ||
lMargin float64 // left margin | ||
tMargin float64 // top margin | ||
rMargin float64 // right margin | ||
bMargin float64 // page break margin | ||
cMargin float64 // cell margin | ||
x, y float64 // current position in user unit | ||
lasth float64 // height of last printed cell | ||
lineWidth float64 // line width in user unit | ||
fontpath string // path containing fonts | ||
coreFonts map[string]bool // array of core font names | ||
fonts map[string]fontDefType // array of used fonts | ||
fontFiles map[string]fontFileType // array of font files | ||
diffs []string // array of encoding differences | ||
fontFamily string // current font family | ||
fontStyle string // current font style | ||
underline bool // underlining flag | ||
currentFont fontDefType // current font info | ||
fontSizePt float64 // current font size in points | ||
fontSize float64 // current font size in user unit | ||
drawColor string // commands for drawing color | ||
fillColor string // commands for filling color | ||
textColor string // commands for text color | ||
colorFlag bool // indicates whether fill and text colors are different | ||
ws float64 // word spacing | ||
images map[string]imageInfoType // array of used images | ||
pageLinks [][]linkType // pageLinks[page][link], both 1-based | ||
links []intLinkType // array of internal links | ||
autoPageBreak bool // automatic page breaking | ||
acceptPageBreak func() bool // returns true to accept page break | ||
pageBreakTrigger float64 // threshold used to trigger page breaks | ||
inHeader bool // flag set when processing header | ||
headerFnc func() // function provided by app and called to write header | ||
inFooter bool // flag set when processing footer | ||
footerFnc func() // function provided by app and called to write footer | ||
zoomMode string // zoom display mode | ||
layoutMode string // layout display mode | ||
title string // title | ||
subject string // subject | ||
author string // author | ||
keywords string // keywords | ||
creator string // creator | ||
aliasNbPagesStr string // alias for total number of pages | ||
pdfVersion string // PDF version number | ||
fontDirStr string // location of font definition files | ||
err error // Set if error occurs during life cycle of instance | ||
} | ||
|
||
type encType struct { | ||
uv int | ||
name string | ||
} | ||
|
||
type encListType [256]encType | ||
|
||
type fontBoxType struct { | ||
Xmin, Ymin, Xmax, Ymax int | ||
} | ||
|
||
type fontDescType struct { | ||
Ascent int | ||
Descent int | ||
CapHeight int | ||
Flags int | ||
FontBBox fontBoxType | ||
ItalicAngle int | ||
StemV int | ||
MissingWidth int | ||
} | ||
|
||
type fontDefType struct { | ||
Tp string // "Core", "TrueType", ... | ||
Name string // "Courier-Bold", ... | ||
Desc fontDescType // Font descriptor | ||
Up int // Underline position | ||
Ut int // Underline thickness | ||
Cw [256]int // Character width by ordinal | ||
Enc string // "cp1252", ... | ||
Diff string // Differences from reference encoding | ||
File string // "Redressed.z" | ||
Size1, Size2 int // Type1 values | ||
OriginalSize int // Size of uncompressed font file | ||
I int // 1-based position in font list, set by font loader, not this program | ||
N int // Set by font loader | ||
DiffN int // Position of diff in app array, set by font loader | ||
} | ||
|
||
type fontInfoType struct { | ||
Data []byte | ||
File string | ||
OriginalSize int | ||
FontName string | ||
Bold bool | ||
IsFixedPitch bool | ||
UnderlineThickness int | ||
UnderlinePosition int | ||
Widths [256]int | ||
Size1, Size2 uint32 | ||
Desc fontDescType | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright (c) 2013 Kurt Jung (Gmail: kurt.w.jung) | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
/* | ||
Package gofpdf implements a PDF document generator. | ||
This package's code and documentation are closely derived from the FPDF library | ||
created by Olivier Plathey, and a number of font and image resources are copied | ||
directly from it. The FPDF website is http://www.fpdf.org/. | ||
Features | ||
• Choice of measurement unit, page format and margins | ||
• Page header and footer management | ||
• Automatic page break | ||
• Automatic line break and text justification | ||
• Image support (JPEG, PNG and GIF) | ||
• Colors | ||
• Links | ||
• TrueType, Type1 and encoding support | ||
• Page compression | ||
gofpdf has no dependencies other than the Go standard library. All tests pass | ||
on Linux, Mac and Windows platforms. Like FPDF version 1.7, from which gofpdf | ||
is derived, this package does not yet support UTF-8 source text. | ||
Quick Start | ||
The following Go code generates a simple PDF. | ||
pdf := fpdf.New("P", "mm", "A4", "../font") | ||
pdf.AddPage() | ||
pdf.SetFont("Arial", "B", 16) | ||
pdf.Cell(40, 10, "Hello, world") | ||
pdf.Output(os.Stdout) | ||
See the tutorials in the fpdf_test.go file (shown as examples in this | ||
documentation) for more advanced PDF examples. | ||
Errors | ||
If an error occurs in an Fpdf method, an internal error field is set. After | ||
this occurs, Fpdf method calls typically return without performing any | ||
operations and the error state is retained. This error management scheme | ||
facilitates PDF generation since individual method calls do not need to be | ||
examined for failure; it is generally sufficient to wait until after Output() | ||
is called. For the same reason, if an error occurs in the calling application | ||
during PDF generation, it may be desirable for the application to transfer the | ||
error to the Fpdf instance by calling the SetError() method or the SetErrorf() | ||
method. At any time during the life cycle of the Fpdf instance, the error state | ||
can be determined with a call to Ok() or Err(). The error itself can be | ||
retrieved with a call to Error(). | ||
Conversion Notes | ||
This package is a relatively straightforward translation from the original FPDF | ||
library written in PHP (despite the caveat in the introduction to Effective | ||
Go). The API names have been retained even though the Go idiom would suggest | ||
otherwise (for example, pdf.GetX() is used rather than simply pdf.X()). The | ||
similarity of the two libraries makes the original FPDF website a good source | ||
of information. It includes a forum and FAQ. | ||
However, some internal changes have been made. Page content is built up using | ||
buffers (of type bytes.Buffer) rather than repeated string concatenation. | ||
Errors are handled as explained above rather than panicking. Output is | ||
generated through an interface of type io.WriteCloser. A number of the original | ||
PHP methods behave differently based on the type of the arguments that are | ||
passed to them; in these cases additional methods have been exported to provide | ||
similar functionality. Font definition files are produced in JSON rather than | ||
PHP. | ||
Tutorials | ||
A side effect of running "go test" in the fpdf directory is the production of | ||
the tutorial PDFs. These can be found in the gofpdf/pdf directory after the | ||
tests complete. | ||
Roadmap | ||
• Handle UTF-8 source text | ||
• Improve test coverage as reported by gocov (https://github.com/axw/gocov) | ||
*/ | ||
package gofpdf |
Oops, something went wrong.