From 3652fc093a1963aa2ccefd018fb70b75bb9ee7d1 Mon Sep 17 00:00:00 2001 From: Benoit KUGLER Date: Fri, 6 Nov 2020 11:57:47 +0100 Subject: [PATCH 1/2] fix: use indirect object for appearance stream fix pdfcpu read error --- attachments.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/attachments.go b/attachments.go index 6e397b73..d436fd25 100644 --- a/attachments.go +++ b/attachments.go @@ -101,7 +101,8 @@ func (f Fpdf) getEmbeddedFiles() string { type annotationAttach struct { *Attachment - x, y, w, h float64 // fpdf coordinates (y diff and scaling done) + x, y, w, h float64 // fpdf coordinates (y diff and scaling done) + appearanceObjectNumber int } // AddAttachmentAnnotation puts a link on the current page, on the rectangle @@ -131,7 +132,17 @@ func (f *Fpdf) putAnnotationsAttachments() { // avoid duplication m := map[*Attachment]bool{} for _, l := range f.pageAttachments { - for _, an := range l { + for i, an := range l { + // always add appearance stream + x1, y1, x2, y2 := an.x, an.y, an.x+an.w, an.y-an.h + f.newobj() + f.outf("<< /Type /XObject /Subtype /Form /BBox [%.2f %.2f %.2f %.2f] /Length 0 >>", + x1, y1, x2, y2) + f.out("stream") + f.out("endstream") + f.out("endobj") + // an.appearanceObjectNumber = f.n + l[i].appearanceObjectNumber = f.n if m[an.Attachment] { // already embedded continue } @@ -143,15 +154,11 @@ func (f *Fpdf) putAnnotationsAttachments() { func (f *Fpdf) putAttachmentAnnotationLinks(out *fmtBuffer, page int) { for _, an := range f.pageAttachments[page] { x1, y1, x2, y2 := an.x, an.y, an.x+an.w, an.y-an.h - as := fmt.Sprintf("<< /Type /XObject /Subtype /Form /BBox [%.2f %.2f %.2f %.2f] /Length 0 >>", - x1, y1, x2, y2) - as += "\nstream\nendstream" - out.printf("<< /Type /Annot /Subtype /FileAttachment /Rect [%.2f %.2f %.2f %.2f] /Border [0 0 0]\n", x1, y1, x2, y2) out.printf("/Contents %s ", f.textstring(utf8toutf16(an.Description))) out.printf("/T %s ", f.textstring(utf8toutf16(an.Filename))) - out.printf("/AP << /N %s>>", as) + out.printf("/AP << /N %d 0 R >> ", an.appearanceObjectNumber) out.printf("/FS %d 0 R >>\n", an.objectNumber) } } From ec4b52cb39f655ab61fee0d862c29803a821c64b Mon Sep 17 00:00:00 2001 From: Benoit KUGLER Date: Fri, 6 Nov 2020 11:58:10 +0100 Subject: [PATCH 2/2] cleanup --- attachments.go | 1 - 1 file changed, 1 deletion(-) diff --git a/attachments.go b/attachments.go index d436fd25..cbac7ad4 100644 --- a/attachments.go +++ b/attachments.go @@ -141,7 +141,6 @@ func (f *Fpdf) putAnnotationsAttachments() { f.out("stream") f.out("endstream") f.out("endobj") - // an.appearanceObjectNumber = f.n l[i].appearanceObjectNumber = f.n if m[an.Attachment] { // already embedded continue