Skip to content

Commit

Permalink
Small tweaks to make comply with goreportcard issues, augmented comme…
Browse files Browse the repository at this point in the history
…nts a little, and added acknowledgment to Benoit KUGLER
  • Loading branch information
jung-kurt committed Nov 9, 2019
1 parent 851f1e4 commit 9fbe8c0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ printing PDF documents. Wojciech Matusiak added supported for word
spacing. Artem Korotkiy added support of UTF-8 fonts. Dave Barnes added
support for imported objects and templates. Brigham Thompson added
support for rounded rectangles. Joe Westcott added underline
functionality and optimized image storage.
functionality and optimized image storage. Benoit KUGLER contributed
support for rectangles with corners of unequal radius, and for file
attachments and annotations.

## Roadmap

Expand Down
40 changes: 22 additions & 18 deletions attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func checksum(data []byte) string {
return hex.EncodeToString(sl)
}

// Writes a compressed file like object as ``/EmbeddedFile``.
// Compressing is done with deflate. Includes length, compressed length and MD5 checksum.
// Writes a compressed file like object as ``/EmbeddedFile``. Compressing is
// done with deflate. Includes length, compressed length and MD5 checksum.
func (f *Fpdf) writeCompressedFileObject(content []byte) {
lenUncompressed := len(content)
sum := checksum(content)
Expand All @@ -48,29 +48,31 @@ func (f *Fpdf) writeCompressedFileObject(content []byte) {
f.out("endobj")
}

// Embed includes the content of `a`,
// and update its internal reference.
// Embed includes the content of `a`, and update its internal reference.
func (f *Fpdf) embed(a *Attachment) {
if a.objectNumber != 0 { // already embeded (objectNumber start at 2)
if a.objectNumber != 0 { // already embedded (objectNumber start at 2)
return
}
oldState := f.state
f.state = 1 // we write file content in the main buffer
f.writeCompressedFileObject(a.Content)
streamId := f.n
streamID := f.n
f.newobj()
f.outf("<< /Type /Filespec /F () /UF %s /EF << /F %d 0 R >> /Desc %s\n>>",
f.textstring(utf8toutf16(a.Filename)),
streamId,
streamID,
f.textstring(utf8toutf16(a.Description)))
f.out("endobj")
a.objectNumber = f.n
f.state = oldState
}

// Write attachments as embedded files (document attachment).
// These attachments are global, see AddAttachmentAnnotation() for a link anchored in a page.
// Note that only the last call of SetAttachments is usefull, previous calls are discarded.
// SetAttachments writes attachments as embedded files (document attachment).
// These attachments are global, see AddAttachmentAnnotation() for a link
// anchored in a page. Note that only the last call of SetAttachments is
// useful, previous calls are discarded. Be aware that not all PDF readers
// support document attachments. See the SetAttachment example for a
// demonstration of this method.
func (f *Fpdf) SetAttachments(as []Attachment) {
f.attachments = as
}
Expand Down Expand Up @@ -102,14 +104,16 @@ type annotationAttach struct {
x, y, w, h float64 // fpdf coordinates (y diff and scaling done)
}

// AddAttachmentAnnotation puts a link on the current page, on the rectangle defined
// by `x`, `y`, `w`, `h`. This link points towards the content defined in `a`, which is embedded
// in the document.
// Note than no drawing is done by this method : a method like `Cell()` or `Rect()`
// should be called to indicate the reader there is a link here.
// Requiring a pointer to an Attachment avoids useless copies in the resulting pdf:
// attachment pointing to the same data will have their content only be included once,
// and be shared amongst all links.
// AddAttachmentAnnotation puts a link on the current page, on the rectangle
// defined by `x`, `y`, `w`, `h`. This link points towards the content defined
// in `a`, which is embedded in the document. Note than no drawing is done by
// this method : a method like `Cell()` or `Rect()` should be called to
// indicate to the reader that there is a link here. Requiring a pointer to an
// Attachment avoids useless copies in the resulting pdf: attachment pointing
// to the same data will have their content only be included once, and be
// shared amongst all links. Be aware that not all PDF readers support
// annotated attachments. See the AddAttachmentAnnotation example for a
// demonstration of this method.
func (f *Fpdf) AddAttachmentAnnotation(a *Attachment, x, y, w, h float64) {
if a == nil {
return
Expand Down
2 changes: 1 addition & 1 deletion def.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ type Fpdf struct {
aliasMap map[string]string // map of alias->replacement
pageLinks [][]linkType // pageLinks[page][link], both 1-based
links []intLinkType // array of internal links
attachments []Attachment // slice of content to embed globaly
attachments []Attachment // slice of content to embed globally
pageAttachments [][]annotationAttach // 1-based array of annotation for file attachments (per page)
outlines []outlineType // array of outlines
outlineRoot int // root of outlines
Expand Down
4 changes: 3 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ printing PDF documents. Wojciech Matusiak added supported for word
spacing. Artem Korotkiy added support of UTF-8 fonts. Dave Barnes added
support for imported objects and templates. Brigham Thompson added
support for rounded rectangles. Joe Westcott added underline
functionality and optimized image storage.
functionality and optimized image storage. Benoit KUGLER contributed
support for rectangles with corners of unequal radius, and for file
attachments and annotations.
Roadmap
Expand Down
4 changes: 3 additions & 1 deletion doc/document.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ also added support for page boxes used in printing PDF documents. Wojciech
Matusiak added supported for word spacing. Artem Korotkiy added support of
UTF-8 fonts. Dave Barnes added support for imported objects and templates.
Brigham Thompson added support for rounded rectangles. Joe Westcott added
underline functionality and optimized image storage.
underline functionality and optimized image storage. Benoit KUGLER contributed
support for rectangles with corners of unequal radius, and for file attachments
and annotations.

## Roadmap

Expand Down
4 changes: 2 additions & 2 deletions fpdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4641,7 +4641,7 @@ func (f *Fpdf) putcatalog() {
}
// Layers
f.layerPutCatalog()
// Name dictionnary :
// Name dictionary :
// -> Javascript
// -> Embedded files
f.out("/Names <<")
Expand Down Expand Up @@ -4740,7 +4740,7 @@ func (f *Fpdf) enddoc() {
}
f.layerEndDoc()
f.putheader()
// Embeded files
// Embedded files
f.putAttachments()
f.putAnnotationsAttachments()
f.putpages()
Expand Down

0 comments on commit 9fbe8c0

Please sign in to comment.